Character.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 "Character.h"
  7. #include "MasterContainer.h"
  8. #include "MotionState.h"
  9. #include "RigidBody.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_,
  27. const Initializer& init)
  28. : masterContainer(masterContainer_)
  29. {
  30. ghostObject = new btPairCachingGhostObject();
  31. motionState = new MotionState(init.startTrf, init.sceneNode);
  32. btAxisSweep3* sweepBp =
  33. dynamic_cast<btAxisSweep3*>(masterContainer.broadphase);
  34. ASSERT(sweepBp != NULL);
  35. ghostPairCallback = new btGhostPairCallback();
  36. sweepBp->getOverlappingPairCache()->setInternalGhostPairCallback(
  37. ghostPairCallback);
  38. //collisionShape = new btCapsuleShape(init.characterWidth,
  39. // init.characterHeight);
  40. convexShape = new btCylinderShape(btVector3(init.characterWidth,
  41. init.characterHeight, init.characterWidth));
  42. ghostObject->setCollisionShape(convexShape);
  43. //ghostObject->setCollisionFlags(btCollisionObject::CF_CHARACTER_OBJECT);
  44. ghostObject->setWorldTransform(toBt(init.startTrf));
  45. character = new btKinematicCharacterController(ghostObject, convexShape,
  46. init.stepHeight);
  47. character->setJumpSpeed(init.jumpSpeed);
  48. character->setMaxJumpHeight(init.maxJumpHeight);
  49. // register
  50. masterContainer.dynamicsWorld->addCollisionObject(ghostObject,
  51. btBroadphaseProxy::CharacterFilter,
  52. btBroadphaseProxy::StaticFilter|btBroadphaseProxy::DefaultFilter);
  53. masterContainer.dynamicsWorld->addAction(character);
  54. masterContainer.characters.push_back(this);
  55. }
  56. //==============================================================================
  57. // Destructor =
  58. //==============================================================================
  59. Character::~Character()
  60. {
  61. masterContainer.characters.erase(std::find(
  62. masterContainer.characters.begin(),
  63. masterContainer.characters.end(), this));
  64. masterContainer.dynamicsWorld->removeAction(character);
  65. masterContainer.dynamicsWorld->removeCollisionObject(ghostObject);
  66. delete character;
  67. delete convexShape;
  68. delete ghostPairCallback;
  69. delete ghostObject;
  70. }
  71. //==============================================================================
  72. // rotate =
  73. //==============================================================================
  74. void Character::rotate(float angle)
  75. {
  76. btMatrix3x3 rot = ghostObject->getWorldTransform().getBasis();
  77. rot *= btMatrix3x3(btQuaternion(btVector3(0, 1, 0), angle));
  78. ghostObject->getWorldTransform().setBasis(rot);
  79. }
  80. //==============================================================================
  81. // moveForward =
  82. //==============================================================================
  83. void Character::moveForward(float distance)
  84. {
  85. btVector3 forward =
  86. -ghostObject->getWorldTransform().getBasis().getColumn(2);
  87. character->setWalkDirection(forward * distance);
  88. }
  89. //==============================================================================
  90. // jump =
  91. //==============================================================================
  92. void Character::jump()
  93. {
  94. character->jump();
  95. }
  96. } // end namespace