174 linhas
5.7 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
  5. // (c) 2009-2012 Benjamin Huet <huet.benjamin@gmail.com>
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the Do What The Fuck You Want To
  8. // Public License, Version 2, as published by Sam Hocevar. See
  9. // http://sam.zoy.org/projects/COPYING.WTFPL for more details.
  10. //
  11. //
  12. // The BulletCharacterController class
  13. // ------------------
  14. // This class is a equivalent of btKinematicCharacterController, but more useful for Lol.
  15. //
  16. #if !defined __BULLETCHARACTERCONTROLLER_BULLETCHARACTERCONTROLLER_H__
  17. #define __BULLETCHARACTERCONTROLLER_BULLETCHARACTERCONTROLLER_H__
  18. #ifdef HAVE_PHYS_USE_BULLET
  19. #include "core.h"
  20. #include "EasyPhysics.h"
  21. //#include "BulletDynamics\Character\btCharacterControllerInterface.h"
  22. #endif
  23. namespace lol
  24. {
  25. namespace phys
  26. {
  27. #define 0
  28. #ifdef HAVE_PHYS_USE_BULLET
  29. ///BulletKinematicCharacterController is an object that supports a sliding motion in a world.
  30. ///It uses a ghost object and convex sweep test to test for upcoming collisions. This is combined with discrete collision detection to recover from penetrations.
  31. ///Interaction between btKinematicCharacterController and dynamic rigid bodies needs to be explicity implemented by the user.
  32. class BulletKinematicCharacterController : public btCharacterControllerInterface
  33. {
  34. protected:
  35. btScalar m_halfHeight;
  36. btPairCachingGhostObject* m_ghostObject;
  37. btConvexShape* m_convexShape;//is also in m_ghostObject, but it needs to be convex, so we store it here to avoid upcast
  38. btScalar m_verticalVelocity;
  39. btScalar m_verticalOffset;
  40. btScalar m_fallSpeed;
  41. btScalar m_jumpSpeed;
  42. btScalar m_maxJumpHeight;
  43. btScalar m_maxSlopeRadians; // Slope angle that is set (used for returning the exact value)
  44. btScalar m_maxSlopeCosine; // Cosine equivalent of m_maxSlopeRadians (calculated once when set, for optimization)
  45. btScalar m_gravity;
  46. btScalar m_turnAngle;
  47. btScalar m_stepHeight;
  48. btScalar m_addedMargin;//@todo: remove this and fix the code
  49. ///this is the desired walk direction, set by the user
  50. btVector3 m_walkDirection;
  51. btVector3 m_normalizedDirection;
  52. //some internal variables
  53. btVector3 m_currentPosition;
  54. btScalar m_currentStepOffset;
  55. btVector3 m_targetPosition;
  56. ///keep track of the contact manifolds
  57. btManifoldArray m_manifoldArray;
  58. bool m_touchingContact;
  59. btVector3 m_touchingNormal;
  60. bool m_wasOnGround;
  61. bool m_wasJumping;
  62. bool m_useGhostObjectSweepTest;
  63. bool m_useWalkDirection;
  64. btScalar m_velocityTimeInterval;
  65. int m_upAxis;
  66. static btVector3* getUpAxisDirections();
  67. btVector3 computeReflectionDirection (const btVector3& direction, const btVector3& normal);
  68. btVector3 parallelComponent (const btVector3& direction, const btVector3& normal);
  69. btVector3 perpindicularComponent (const btVector3& direction, const btVector3& normal);
  70. bool recoverFromPenetration ( btCollisionWorld* collisionWorld);
  71. void stepUp (btCollisionWorld* collisionWorld);
  72. void updateTargetPositionBasedOnCollision (const btVector3& hit_normal, btScalar tangentMag = btScalar(0.0), btScalar normalMag = btScalar(1.0));
  73. void stepForwardAndStrafe (btCollisionWorld* collisionWorld, const btVector3& walkMove);
  74. void stepDown (btCollisionWorld* collisionWorld, btScalar dt);
  75. public:
  76. BulletKinematicCharacterController (btPairCachingGhostObject* ghostObject,btConvexShape* convexShape,btScalar stepHeight, int upAxis = 1);
  77. ~BulletKinematicCharacterController ();
  78. ///btActionInterface interface
  79. virtual void updateAction( btCollisionWorld* collisionWorld,btScalar deltaTime)
  80. {
  81. preStep ( collisionWorld);
  82. playerStep (collisionWorld, deltaTime);
  83. }
  84. ///btActionInterface interface
  85. void debugDraw(btIDebugDraw* debugDrawer);
  86. void setUpAxis (int axis)
  87. {
  88. if (axis < 0)
  89. axis = 0;
  90. if (axis > 2)
  91. axis = 2;
  92. m_upAxis = axis;
  93. }
  94. /// This should probably be called setPositionIncrementPerSimulatorStep.
  95. /// This is neither a direction nor a velocity, but the amount to
  96. /// increment the position each simulation iteration, regardless
  97. /// of dt.
  98. /// This call will reset any velocity set by setVelocityForTimeInterval().
  99. virtual void setWalkDirection(const btVector3& walkDirection);
  100. /// Caller provides a velocity with which the character should move for
  101. /// the given time period. After the time period, velocity is reset
  102. /// to zero.
  103. /// This call will reset any walk direction set by setWalkDirection().
  104. /// Negative time intervals will result in no motion.
  105. virtual void setVelocityForTimeInterval(const btVector3& velocity,
  106. btScalar timeInterval);
  107. void reset ();
  108. void warp (const btVector3& origin);
  109. void preStep ( btCollisionWorld* collisionWorld);
  110. void playerStep ( btCollisionWorld* collisionWorld, btScalar dt);
  111. void setFallSpeed (btScalar fallSpeed);
  112. void setJumpSpeed (btScalar jumpSpeed);
  113. void setMaxJumpHeight (btScalar maxJumpHeight);
  114. bool canJump () const;
  115. void jump ();
  116. void setGravity(btScalar gravity);
  117. btScalar getGravity() const;
  118. /// The max slope determines the maximum angle that the controller can walk up.
  119. /// The slope angle is measured in radians.
  120. void setMaxSlope(btScalar slopeRadians);
  121. btScalar getMaxSlope() const;
  122. btPairCachingGhostObject* getGhostObject();
  123. void setUseGhostSweepTest(bool useGhostObjectSweepTest)
  124. {
  125. m_useGhostObjectSweepTest = useGhostObjectSweepTest;
  126. }
  127. bool onGround () const;
  128. };
  129. #endif // HAVE_PHYS_USE_BULLET
  130. #endif // 0
  131. } /* namespace phys */
  132. } /* namespace lol */
  133. #endif /* __BULLETCHARACTERCONTROLLER_BULLETCHARACTERCONTROLLER_H__ */