bullet 物理引擎的角色控制器(character controller)

1277 篇文章 12 订阅
204 篇文章 4 订阅

转载 :http://hi.baidu.com/ae8506/item/693a7975d4fd502ed6a89c56

作者:CYM  http://hi.baidu.com/ae8506/


角色控制器(character controller)是物理引擎必不可少的一部分

在bullet中可以通过btKinematicCharacterController和btPairCachingGhostObject来实现角色控制

其中btKinematicCharacterController类是致力于解决角色控制的类,他实现了滑动,爬梯子,跳跃等常见的动作.

在使用角色控制器之前先要包含以下几个文件

  1. #include <BulletDynamics\Character\btKinematicCharacterController.h>   
  2. #include <BulletCollision\CollisionDispatch\btGhostObject.h>  
#include <BulletDynamics\Character\btKinematicCharacterController.h>
#include <BulletCollision\CollisionDispatch\btGhostObject.h>


在头文件中声明以下两个对象

  1. btKinematicCharacterController* m_character;  
  2. btPairCachingGhostObject* m_ghostObject;  
btKinematicCharacterController* m_character;
btPairCachingGhostObject* m_ghostObject;

m_character就死角色控制器,m_ghostObject则是一个幽灵对象.

创建角色控制器(假设已经创建了物理世界并且可以通过physics对象调用)

  1. //创建胶囊对象以及角色控制器   
  2. {  
  3. m_trans.setIdentity ();  
  4. m_trans.setOrigin (btVector3(0.0, 500.0, 0.0));  
  5.    
  6. m_ghostObject = new btPairCachingGhostObject();  
  7. m_ghostObject->setWorldTransform(m_trans);  
  8. physics->m_overlappingPairCache->getOverlappingPairCache()->setInternalGhostPairCallback(new btGhostPairCallback());  
  9. btScalar characterHeight=2.0f;  
  10. btScalar characterWidth =2.0f;  
  11. btConvexShape* capsule = new btCapsuleShape(characterWidth,characterHeight);  
  12. m_ghostObject->setCollisionShape (capsule);  
  13. m_ghostObject->setCollisionFlags (btCollisionObject::CF_CHARACTER_OBJECT);  
  14.    
  15. btScalar stepHeight = btScalar(0.35);  
  16. m_character = new btKinematicCharacterController (m_ghostObject,capsule,stepHeight);  
  17.    
  18. //向世界中添加碰撞对象   
  19. physics->m_dynamicsWorld->addCollisionObject(  
  20. m_ghostObject,  
  21. btBroadphaseProxy::CharacterFilter,   
  22. btBroadphaseProxy::StaticFilter|btBroadphaseProxy::DefaultFilter);  
  23. physics->m_dynamicsWorld->addAction(m_character);  
  24. }  
//创建胶囊对象以及角色控制器
{
m_trans.setIdentity ();
m_trans.setOrigin (btVector3(0.0, 500.0, 0.0));
 
m_ghostObject = new btPairCachingGhostObject();
m_ghostObject->setWorldTransform(m_trans);
physics->m_overlappingPairCache->getOverlappingPairCache()->setInternalGhostPairCallback(new btGhostPairCallback());
btScalar characterHeight=2.0f;
btScalar characterWidth =2.0f;
btConvexShape* capsule = new btCapsuleShape(characterWidth,characterHeight);
m_ghostObject->setCollisionShape (capsule);
m_ghostObject->setCollisionFlags (btCollisionObject::CF_CHARACTER_OBJECT);
 
btScalar stepHeight = btScalar(0.35);
m_character = new btKinematicCharacterController (m_ghostObject,capsule,stepHeight);
 
//向世界中添加碰撞对象
physics->m_dynamicsWorld->addCollisionObject(
m_ghostObject,
btBroadphaseProxy::CharacterFilter, 
btBroadphaseProxy::StaticFilter|btBroadphaseProxy::DefaultFilter);
physics->m_dynamicsWorld->addAction(m_character);
}


根据用户输入,更新角色


  1. //根据外部输入,更新角色   
  2. if (m_physics->m_dynamicsWorld)  
  3. {  
  4. ///设置行走方向   
  5. btTransform xform;  
  6. xform = m_ghostObject->getWorldTransform ();  
  7. //获取本地坐标向量,并且单位化   
  8. btVector3 forwardDir = xform.getBasis()[2];  
  9. btVector3 upDir = xform.getBasis()[1];  
  10. btVector3 strafeDir = xform.getBasis()[0];  
  11. forwardDir.normalize ();  
  12. upDir.normalize ();  
  13. strafeDir.normalize ();  
  14.    
  15. btVector3 walkDirection = btVector3(0.0, 0.0, 0.0);  
  16. btScalar walkVelocity = btScalar(1.1) * 4.0; // 4 km/h -> 1.1 m/s   
  17. btScalar walkSpeed = walkVelocity* deltaTime*2.0f;  
  18.   
  19. //控制前后行走,以及旋转方向,   
  20. if (m_render->m_keyboard->isKeyDown(OIS::KC_RIGHT))  
  21. {  
  22. float yaw=0.05f;  
  23. btMatrix3x3 orn = m_ghostObject->getWorldTransform().getBasis();  
  24. orn *= btMatrix3x3(btQuaternion(btVector3(0,1,0),yaw));  
  25. m_ghostObject->getWorldTransform ().setBasis(orn);  
  26. }  
  27.    
  28. if (m_render->m_keyboard->isKeyDown(OIS::KC_LEFT))  
  29. {  
  30. float yaw=-0.05f;  
  31. btMatrix3x3 orn = m_ghostObject->getWorldTransform().getBasis();  
  32. orn *= btMatrix3x3(btQuaternion(btVector3(0,1,0),yaw));  
  33. m_ghostObject->getWorldTransform ().setBasis(orn);  
  34. }  
  35.    
  36. if (m_render->m_keyboard->isKeyDown(OIS::KC_UP))  
  37. {  
  38. walkDirection += forwardDir;  
  39. }  
  40.    
  41. if (m_render->m_keyboard->isKeyDown(OIS::KC_DOWN))  
  42. {  
  43. walkDirection -= forwardDir;  
  44. }  
  45.    
  46. if(m_render->m_keyboard->isKeyDown(OIS::KC_SPACE) && m_character && m_character->canJump())  
  47. {  
  48. m_character->jump();  
  49. }  
  50.    
  51. //按照方向移动角色   
  52. m_character->setWalkDirection(walkDirection*walkSpeed);  
  53. }  
  54.    
  55. //更新渲染对象   
  56. m_trans=m_ghostObject->getWorldTransform();  
  57. m_node->setPosition(m_trans.getOrigin().getX(),m_trans.getOrigin().getY(),m_trans.getOrigin().getZ());  
  58. m_node->setOrientation(m_trans.getRotation().getW(),m_trans.getRotation().getX(),m_trans.getRotation().getY(),m_trans.getRotation().getZ());  
//根据外部输入,更新角色
if (m_physics->m_dynamicsWorld)
{
///设置行走方向
btTransform xform;
xform = m_ghostObject->getWorldTransform ();
//获取本地坐标向量,并且单位化
btVector3 forwardDir = xform.getBasis()[2];
btVector3 upDir = xform.getBasis()[1];
btVector3 strafeDir = xform.getBasis()[0];
forwardDir.normalize ();
upDir.normalize ();
strafeDir.normalize ();
 
btVector3 walkDirection = btVector3(0.0, 0.0, 0.0);
btScalar walkVelocity = btScalar(1.1) * 4.0; // 4 km/h -> 1.1 m/s
btScalar walkSpeed = walkVelocity* deltaTime*2.0f;

//控制前后行走,以及旋转方向,
if (m_render->m_keyboard->isKeyDown(OIS::KC_RIGHT))
{
float yaw=0.05f;
btMatrix3x3 orn = m_ghostObject->getWorldTransform().getBasis();
orn *= btMatrix3x3(btQuaternion(btVector3(0,1,0),yaw));
m_ghostObject->getWorldTransform ().setBasis(orn);
}
 
if (m_render->m_keyboard->isKeyDown(OIS::KC_LEFT))
{
float yaw=-0.05f;
btMatrix3x3 orn = m_ghostObject->getWorldTransform().getBasis();
orn *= btMatrix3x3(btQuaternion(btVector3(0,1,0),yaw));
m_ghostObject->getWorldTransform ().setBasis(orn);
}
 
if (m_render->m_keyboard->isKeyDown(OIS::KC_UP))
{
walkDirection += forwardDir;
}
 
if (m_render->m_keyboard->isKeyDown(OIS::KC_DOWN))
{
walkDirection -= forwardDir;
}
 
if(m_render->m_keyboard->isKeyDown(OIS::KC_SPACE) && m_character && m_character->canJump())
{
m_character->jump();
}
 
//按照方向移动角色
m_character->setWalkDirection(walkDirection*walkSpeed);
}
 
//更新渲染对象
m_trans=m_ghostObject->getWorldTransform();
m_node->setPosition(m_trans.getOrigin().getX(),m_trans.getOrigin().getY(),m_trans.getOrigin().getZ());
m_node->setOrientation(m_trans.getRotation().getW(),m_trans.getRotation().getX(),m_trans.getRotation().getY(),m_trans.getRotation().getZ());



优酷视频

http://v.youku.com/v_show/id_XMzc4NTgyODky.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值