(C++) How to animate and move an entity - for newbies

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
TheWorstCoderEver
Posts: 47
Joined: Wed Feb 01, 2006 8:09 pm
Location: Wroclaw
Contact:

(C++) How to animate and move an entity - for newbies

Post by TheWorstCoderEver »

So far my contributions for this community had been rather miserable, so I decided it's time to make up for it by posting piece of code from my own test application. Maybe it's not as decent as I would like it to be, but I nonetheless decided to share it with you. It's a method for moving an entity through the scenery, setting appropiate animation loop and detecting whether it collides with the scenery or not. The method itself goes like this:

Code: Select all

void Character::Walk(float speed, ISceneCollisionManager* colmanger, ITriangleSelector* selector)
{
   core::vector3df OriginalPosition;
   core::vector3df TranslationVector;
   core::triangle3df triout;


   bool isfalling = true; //set it to "false" if you want the gravity off



   OriginalPosition=this->position;

   TranslationVector.X=((float)(cos(this->heading*PI/180))*speed);
   TranslationVector.Z=-((float)(sin(this->heading*PI/180))*speed);

   this->position = colmanger->getCollisionResultPosition(selector, OriginalPosition, vector3df(10,50,10), TranslationVector, triout, isfalling, 10);
   this->model->setPosition(position);

   if (this->state!=STATE_WALK)
   {
      this->state=STATE_WALK;
      this->model->setMD2Animation("run");
      this->model->setLoopMode(true);
   }


} 
"This" refers to character type object. "State" is an integer value that indicates in what state the entity is at the moment. For now I've only defined two states, namely STATE_WALK the character is in when walking and STATE_STAND the character type object is in when it's not moving. "Model" is an Irrlicht scene node representing the entity. For stopping characters' movement, I'm using this method:

Code: Select all

void Character::Stop()
{
	this->state=STATE_STAND;
	this->model->setMD2Animation("stand");
}
I hope this post will be of some help to newcomers who want to create their own Irrlicht-based 3-person type game without resorting to external libraries (like Newton) but don't know where to start.
Post Reply