Adding an enemy model

From IrrWizard

Jump to: navigation, search

Introduction

This short tutorial will show you how to add an enemy game model with collision detection



Adding an enemy model

To add an enemy game model we must put the code in the CGameStateLevel01 class. We only want this enemy to exist in this level. For this example a Quake2 MD2 file is used, however other formats can also be used, 3d Studio Max, Milkshape, DirectX file, etc. Make sure your model and texture is in the media folder of your project.


Add the following code to the void CGameStateLevel01::Init(CGameManager* pManager) function

...
//load in enemy model
scene::IAnimatedMeshSceneNode* enemyNode = 0;
scene::IAnimatedMesh* enemyModel = pManager->getSceneManager()->getMesh("media/enemy.md2");
if (enemyModel)
{         
   enemyNode = pManager->getSceneManager()->addAnimatedMeshSceneNode(enemyModel);
   enemyNode->setMaterialFlag(EMF_LIGHTING, false);
   enemyNode->setMaterialTexture( 0, pManager->getDriver()->getTexture("media/enemy.jpg") );
   enemyNode->setPosition(core::vector3df(700,-5,15)); 
   enemyNode->setRotation(core::vector3df(0,0,0));
   enemyNode->setMD2Animation(scene::EMAT_STAND);
   enemyNode->setScale(core::vector3df(1.5,1.5,1.5));	
   irr::scene::ITriangleSelector* enemySelector = 
         pManager->getSceneManager()->createTriangleSelectorFromBoundingBox(enemyNode); 
   if(enemySelector) 
         getMetaSelector()->addTriangleSelector(enemySelector); 
} 


The CGamePlayState class holds a pointer to a MetaTriangleSelector, which is basically the main 'collision detection' pot. Each model has it's own TrinagleSelector which can be put in this pot for the Collision detection Manager to detect when collisions occur.

game00.jpg

Adding a weapon to the enemy model

The weapon is usually a separate model, this allows the enemy to carry different weapons, i.e. Quake2/3 style pick-ups. Also it allows it to be dropped when killed. To attach the MD2 weapon model to the enemy we previously created is just a matter of making the weapon a child of the enemy, here is the code:

...
// load weapon for model
scene::IAnimatedMesh* weapon = pManager->getSceneManager()->getMesh("media/weapon.md2"); 
scene::IAnimatedMeshSceneNode* wepNode = pManager->getSceneManager()->
    addAnimatedMeshSceneNode(weapon); 
wepNode->setMaterialTexture( 0, pManager->getDriver()->getTexture("media/weapon.pcx") );
wepNode->setMaterialFlag(EMF_LIGHTING, false);
wepNode->setMD2Animation(scene::EMAT_STAND);
wepNode->setParent(enemyNode); 
 

The weapon model will be created with the model, so it should be positioned in relation to the main model.

game01.jpg

Personal tools