hkpCharacterRigidBody.h
上传用户:yisoukefu
上传日期:2020-08-09
资源大小:39506k
文件大小:11k
源码类别:

其他游戏

开发平台:

Visual C++

  1. /* 
  2.  * 
  3.  * Confidential Information of Telekinesys Research Limited (t/a Havok). Not for disclosure or distribution without Havok's
  4.  * prior written consent. This software contains code, techniques and know-how which is confidential and proprietary to Havok.
  5.  * Level 2 and Level 3 source code contains trade secrets of Havok. Havok Software (C) Copyright 1999-2009 Telekinesys Research Limited t/a Havok. All Rights Reserved. Use of this software is subject to the terms of an end user license agreement.
  6.  * 
  7.  */
  8. #ifndef HK_CHARACTER_RIGIDBODY__H
  9. #define HK_CHARACTER_RIGIDBODY__H
  10. #include <Common/Base/Types/Physics/hkStepInfo.h>
  11. #include <Physics/Utilities/CharacterControl/hkpCharacterControl.h>
  12. #include <Physics/Dynamics/Collide/hkpCollisionListener.h>
  13. #include <Physics/Dynamics/World/Listener/hkpWorldPostSimulationListener.h>
  14. #include <Physics/Dynamics/Entity/hkpEntityListener.h>
  15. class hkpRigidBody;
  16. class hkpPhantomCallbackShape;
  17. class hkpShape;
  18. class hkpCapsuleShape;
  19. class hkpCdPointCollector;
  20. struct hkpSurfaceInfo;
  21. /// Character controller cinfo
  22. struct hkpCharacterRigidBodyCinfo
  23. {
  24. HK_DECLARE_NONVIRTUAL_CLASS_ALLOCATOR( HK_MEMORY_CLASS_CHARACTER, hkpCharacterRigidBodyCinfo );
  25. //
  26. // Values used to seup rigid body
  27. //
  28. /// The collision filter info
  29. /// See hkRigidBodyCinfo for details
  30. hkUint32 m_collisionFilterInfo;
  31. /// The shape
  32. /// See hkRigidBodyCinfo for details
  33. hkpShape* m_shape;
  34. /// Initial position
  35. /// See hkRigidBodyCinfo for details
  36. hkVector4 m_position;
  37. /// Initial rotation
  38. /// See hkRigidBodyCinfo for details
  39. hkQuaternion m_rotation;
  40. /// The mass of character
  41. /// See hkRigidBodyCinfo for details
  42. hkReal m_mass;
  43. /// Set friction of character
  44. /// See hkRigidBodyCinfo for details
  45. hkReal m_friction;
  46. /// Set maximal linear velocity
  47. /// See hkRigidBodyCinfo for details
  48. hkReal m_maxLinearVelocity;
  49. /// Set maximal allowed penetration depth
  50. /// See hkRigidBodyCinfo for details
  51. hkReal m_allowedPenetrationDepth;
  52. //
  53. // Character controller specific values
  54. //
  55. /// Set up direction 
  56. hkVector4 m_up;
  57. /// Set maximal slope
  58. hkReal m_maxSlope;
  59. /// Set maximal force of character
  60. hkReal m_maxForce;
  61. //
  62. // Parameters used by checkSupport
  63. //
  64. /// Set maximal speed for simplex solver
  65. hkReal m_maxSpeedForSimplexSolver;
  66. /// Set penetration recovery speed
  67. hkReal m_penetrationRecoverySpeed;
  68. /// Internal use
  69. hkInt32 m_userPlane;
  70. /// Set limit support distance
  71. hkReal m_supportDistance;
  72. /// Set color of character for the visual debugger
  73. hkInt32 m_vdbColor;
  74. // Constructor. Sets some defaults.
  75. hkpCharacterRigidBodyCinfo()
  76. {
  77. m_mass = 100.0f;
  78. m_maxForce = 1000.0f;
  79. m_friction = 0.0f;
  80. m_maxSlope = HK_REAL_PI / 3.0f;;
  81. m_up.set( 0,1,0 );
  82. m_userPlane = 0;
  83. m_maxLinearVelocity = 20.0f;
  84. m_allowedPenetrationDepth = -0.1f;
  85. m_maxSpeedForSimplexSolver = 10.0f;
  86. m_penetrationRecoverySpeed = 1.0f;
  87. m_collisionFilterInfo = 0;
  88. m_position.setZero4();
  89. m_rotation.setIdentity();
  90. m_supportDistance = 0.1f;
  91. m_vdbColor = 0xA0FF0000;
  92. }
  93. };
  94. /// The character rigid body class is used to represent a rigid body that can move dynamically around 
  95. /// the scene. It is called character rigid body because it is usually used to represent game characters. 
  96. /// It is a rigid body analogy to hkpCharacterProxy controller. But opposite to hkpCharacterProxy, which 
  97. /// is represented by virtual phantom shape, hkpCharacterRigidBody controller is physically presented in the dynamic scene. 
  98. /// It's movement is controlled by direct setting its linear and angular velocities.
  99. /// It's an entity listener so it can add itself to the world as a hkpWorldPostSimulationListener.
  100. /// Doing so has two purposes:
  101. ///
  102. /// The first usage is the modification of a mass of rigid body character during collision with dynamic objects in the scene.
  103. /// This approach allows continuous regulation of "maximal force (power)" of character during interaction with other dynamics objects.
  104. /// (Modification factor decreases the mass of rigid body during response calculation)
  105. /// Modification factor is calculated in every step in contactProcessCallback() using the following formulas:
  106. ///
  107. ///  - Calculate angle alpha between contact normal and current acceleration
  108. ///  - Calculate current force applied to interaction object as F = mass*acceleration*cos(alpha)
  109. ///  - If (F > Fmax) calculate mass modification factor
  110. ///  - Apply mass modification factor to impulse calculation using hkpResponseModifier::setInvMassScalingForContact()
  111. ///
  112. /// The acceleration is calculated as difference between the required output velocity from state machine and current
  113. /// velocity of rigid body divided by timestep.
  114. ///
  115. /// The second usage is the addition of extra contact points to avoid movement of character on "steep" plane. "Steep" plane is defined
  116. /// by m_maxSlope parameter. It's a angle between UP direction and normal of the plane. So vertical plane has PI/2 (90 Deg) and horizontal plane 0.
  117. /// Default value is PI/3 (60 Deg).
  118. class hkpCharacterRigidBody : public hkReferencedObject, public hkpEntityListener, public hkpWorldPostSimulationListener
  119. {
  120. public:
  121. HK_DECLARE_CLASS_ALLOCATOR(HK_MEMORY_CLASS_CHARACTER);
  122. /// Constructor
  123. /// Creates a rigid body, and an associated contact point listener
  124. hkpCharacterRigidBody( const hkpCharacterRigidBodyCinfo& info );
  125. /// Destructor
  126. ~hkpCharacterRigidBody();
  127. /// Get rigid body of character
  128. hkpRigidBody* getRigidBody() const;
  129. /// Check and see if the character is supported in the given direction (and by what).
  130. /// This call checks the geometry immediately around the character, and does not take velocity into account.
  131. /// i.e. if the character is moving away from the ground, but is still touching the ground, this function
  132. /// will return SUPPORTED as the supported state.
  133. void checkSupport(const hkStepInfo& stepInfo, hkpSurfaceInfo& ground) const;
  134. /// As above, but provides support information to a collector.
  135. /// param collector A collector to which support information will be passed. Its addCdPoint callback will be 
  136. ///   called for each contact point which provides support to the character. The collidable providing support
  137. ///   to the character is always the m_cdBodyB member of the hkpCdPoint. Note that the reference passed to the
  138. ///   callback targets a temporary.
  139. void checkSupport(const hkStepInfo& stepInfo, hkpSurfaceInfo& ground, hkpCdPointCollector* collector ) const;
  140. /// Set linear acceleration for mass modifier
  141. void setLinearAccelerationToMassModifier(const hkVector4& newAcc);
  142. /// Set the maximum force the character is able to apply to another physical object
  143. void setMaxForce( hkReal maxForce );
  144. //
  145. // Simple wrappers for rigid body functions
  146. //
  147. /// Set character linear velocity.  The timestep argument should be given the same timestep that is passed
  148. /// to the physics simulation step.  Note, the timestep must be greater than 0.0f.  A value of 0.0f or less
  149. /// will set hkpCharacterRigidBodyCollisionListener::m_acceleration to be INF, which could cause crashes.
  150. void setLinearVelocity(const hkVector4& newVel, hkReal timestep );
  151. /// Get character linear velocity
  152. const hkVector4& getLinearVelocity() const;
  153. /// Set character angular velocity
  154. void setAngularVelocity(const hkVector4& newAVel);
  155. /// Get character angular velocity
  156. const hkVector4& getAngularVelocity() const;
  157. /// Get character position
  158. const hkVector4& getPosition() const;
  159. public:
  160. // Listener methods.
  161. virtual void entityAddedCallback( hkpEntity* entity );
  162. virtual void entityRemovedCallback( hkpEntity* entity );
  163. virtual void postSimulationCallback( hkpWorld* world );
  164. public:
  165. /// Traverse all the contact points in a collision entry, adding vertical planes as necessary.
  166. inline void considerCollisionEntryForSlope( const hkpWorld* world, const hkpLinkedCollidable::CollisionEntry& entry, hkpSimpleConstraintContactMgr* mgr, hkArray<hkContactPointId>& contactPointIds );
  167. /// For each collision entry, limit the responses involved based on the character's m_maxForce value.
  168. inline void considerCollisionEntryForMassModification( const hkpWorld* world, const hkpLinkedCollidable::CollisionEntry& entry, hkpSimpleConstraintContactMgr* mgr, const hkArray<hkContactPointId>& contactPointIds );
  169. public:
  170. void unweldContactPoints( hkpSimpleConstraintContactMgr* mgr, const hkArray<hkContactPointId>& contactPointIds, hkReal normalFactor );
  171. static void HK_CALL unweldContactPoints( const hkTransform& transform, const hkpCapsuleShape* capsule, hkpSimpleConstraintContactMgr* mgr, const hkArray<hkContactPointId>& contactPointIds, hkReal normalFactor );
  172. public:
  173. /// Remove the vertical points added by this object from any extant constraint manager. 
  174. void discardVerticalPoints();
  175. /// Consider contact points for maxSlope, maxForce and welding.
  176. void processActualPoints( hkpWorld* world );
  177. public:
  178. hkpRigidBody* m_character;
  179. hkVector4 m_up;
  180. hkReal m_maxSlopeCosine;
  181. hkInt32 m_userPlanes;
  182. hkReal m_penetrationRecoverySpeed;
  183. hkReal m_maxSpeedForSimplexSolver;
  184. hkReal m_supportDistance;
  185. public:
  186. // Internal use
  187. /// The information needed to delete vertical points in the next step.
  188. struct VertPointInfo
  189. {
  190. hkContactPoint m_vertPoint;
  191. hkpSimpleConstraintContactMgr* m_mgr;
  192. };
  193. /// The vertical points we added in the previous step.
  194. hkArray<VertPointInfo> m_verticalContactPoints;
  195. /// We set the w component of the position of vertical contact points to this value.
  196. static const int m_magicNumber = 0x008df4a7;
  197. /// When removing a contact point, we set the w component of its position to this value. 
  198. static const int m_notMagicNumber = 0x00fa2bb3;
  199. hkVector4 m_acceleration;
  200. hkReal m_maxForce;
  201. private:
  202. struct CollectorPair;
  203. };
  204. #endif //HK_CHARACTER_RIGIDBODY__H
  205. /*
  206. * Havok SDK - NO SOURCE PC DOWNLOAD, BUILD(#20090216)
  207. * Confidential Information of Havok.  (C) Copyright 1999-2009
  208. * Telekinesys Research Limited t/a Havok. All Rights Reserved. The Havok
  209. * Logo, and the Havok buzzsaw logo are trademarks of Havok.  Title, ownership
  210. * rights, and intellectual property rights in the Havok software remain in
  211. * Havok and/or its suppliers.
  212. * Use of this software for evaluation purposes is subject to and indicates
  213. * acceptance of the End User licence Agreement for this product. A copy of
  214. * the license is included with this software and is also available at www.havok.com/tryhavok.
  215. */