PhysCharacter.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include <algorithm>
  2. #include <btBulletCollisionCommon.h>
  3. #include <btBulletDynamicsCommon.h>
  4. #include <BulletCollision/CollisionDispatch/btGhostObject.h>
  5. #include <BulletDynamics/Character/btKinematicCharacterController.h>
  6. #include "PhysCharacter.h"
  7. #include "PhysMasterContainer.h"
  8. #include "PhysMotionState.h"
  9. #include "PhysRigidBody.h"
  10. namespace Phys {
  11. //==============================================================================
  12. // Contructor =
  13. //==============================================================================
  14. inline Character::Initializer::Initializer():
  15. characterHeight(2.0),
  16. characterWidth(0.75),
  17. stepHeight(1.0),
  18. jumpSpeed(10.0),
  19. maxJumpHeight(0.0),
  20. sceneNode(NULL),
  21. startTrf(Transform::getIdentity())
  22. {}
  23. //==============================================================================
  24. // Contructor =
  25. //==============================================================================
  26. Character::Character(MasterContainer& masterContainer_, const Initializer& init):
  27. masterContainer(masterContainer_)
  28. {
  29. ghostObject = new btPairCachingGhostObject();
  30. motionState = new MotionState(init.startTrf, init.sceneNode);
  31. btAxisSweep3* sweepBp = dynamic_cast<btAxisSweep3*>(masterContainer.broadphase);
  32. ASSERT(sweepBp != NULL);
  33. ghostPairCallback = new btGhostPairCallback();
  34. sweepBp->getOverlappingPairCache()->setInternalGhostPairCallback(ghostPairCallback);
  35. //collisionShape = new btCapsuleShape(init.characterWidth, init.characterHeight);
  36. convexShape = new btCylinderShape(btVector3(init.characterWidth, init.characterHeight, init.characterWidth));
  37. ghostObject->setCollisionShape(convexShape);
  38. //ghostObject->setCollisionFlags(btCollisionObject::CF_CHARACTER_OBJECT);
  39. ghostObject->setWorldTransform(toBt(init.startTrf));
  40. character = new btKinematicCharacterController(ghostObject, convexShape, init.stepHeight);
  41. character->setJumpSpeed(init.jumpSpeed);
  42. character->setMaxJumpHeight(init.maxJumpHeight);
  43. // register
  44. masterContainer.dynamicsWorld->addCollisionObject(ghostObject, btBroadphaseProxy::CharacterFilter,
  45. btBroadphaseProxy::StaticFilter|btBroadphaseProxy::DefaultFilter);
  46. masterContainer.dynamicsWorld->addAction(character);
  47. masterContainer.characters.push_back(this);
  48. }
  49. //==============================================================================
  50. // Destructor =
  51. //==============================================================================
  52. Character::~Character()
  53. {
  54. masterContainer.characters.erase(std::find(masterContainer.characters.begin(),
  55. masterContainer.characters.end(), this));
  56. masterContainer.dynamicsWorld->removeAction(character);
  57. masterContainer.dynamicsWorld->removeCollisionObject(ghostObject);
  58. delete character;
  59. delete convexShape;
  60. delete ghostPairCallback;
  61. delete ghostObject;
  62. }
  63. //==============================================================================
  64. // rotate =
  65. //==============================================================================
  66. void Character::rotate(float angle)
  67. {
  68. btMatrix3x3 rot = ghostObject->getWorldTransform().getBasis();
  69. rot *= btMatrix3x3(btQuaternion(btVector3(0, 1, 0), angle));
  70. ghostObject->getWorldTransform().setBasis(rot);
  71. }
  72. //==============================================================================
  73. // moveForward =
  74. //==============================================================================
  75. void Character::moveForward(float distance)
  76. {
  77. btVector3 forward = -ghostObject->getWorldTransform().getBasis().getColumn(2);
  78. character->setWalkDirection(forward * distance);
  79. }
  80. //==============================================================================
  81. // jump =
  82. //==============================================================================
  83. void Character::jump()
  84. {
  85. character->jump();
  86. }
  87. } // end namespace