(C++) How to animate and move an entity

來源:互聯網
上載者:User
[http://irrlicht.sourceforge.net/phpBB2/viewtopic.php?t=12238]

 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: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: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.以上主要是調用ISceneCollisionManager::getCollisionResultPosition來得到人物與3d world碰撞後的新position。
This can be used for moving a character in a 3d world: The character will slide at walls and is able to walk up stairs.
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.