rigidBodyComponent.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef RIGID_BODY_COMPONENT_H
  23. #define RIGID_BODY_COMPONENT_H
  24. #ifndef COMPONENT_H
  25. #include "T3D/components/component.h"
  26. #endif
  27. #ifndef _T3D_PHYSICSCOMMON_H_
  28. #include "T3D/physics/physicsCommon.h"
  29. #endif
  30. #ifndef COLLISION_COMPONENT_H
  31. #include "T3D/components/collision/collisionComponent.h"
  32. #endif
  33. #ifndef PHYSICS_COMPONENT_INTERFACE_H
  34. #include "T3D/components/physics/physicsComponentInterface.h"
  35. #endif
  36. class PhysicsBody;
  37. //////////////////////////////////////////////////////////////////////////
  38. ///
  39. ///
  40. //////////////////////////////////////////////////////////////////////////
  41. class RigidBodyComponent : public Component, public PhysicsComponentInterface
  42. {
  43. typedef Component Parent;
  44. enum SimType
  45. {
  46. /// This physics representation only exists on the client
  47. /// world and the server only does ghosting.
  48. SimType_ClientOnly,
  49. /// The physics representation only exists on the server world
  50. /// and the client gets delta updates for rendering.
  51. SimType_ServerOnly,
  52. /// The physics representation exists on the client and the server
  53. /// worlds with corrections occuring when the client gets out of sync.
  54. SimType_ClientServer,
  55. /// The bits used to pack the SimType field.
  56. SimType_Bits = 3,
  57. } mSimType;
  58. //
  59. //
  60. /// The current physics state.
  61. PhysicsState mState;
  62. /// The previous and current render states.
  63. PhysicsState mRenderState[2];
  64. /// The abstracted physics actor.
  65. PhysicsBody *mPhysicsRep;
  66. PhysicsWorld *mWorld;
  67. /// The starting position to place the shape when
  68. /// the level begins or is reset.
  69. MatrixF mResetPos;
  70. //
  71. //
  72. /// If true then no corrections are sent from the server
  73. /// and/or applied from the client.
  74. ///
  75. /// This is only ment for debugging.
  76. ///
  77. static bool smNoCorrections;
  78. /// If true then no smoothing is done on the client when
  79. /// applying server corrections.
  80. ///
  81. /// This is only ment for debugging.
  82. ///
  83. static bool smNoSmoothing;
  84. ///
  85. F32 mMass;
  86. ///
  87. F32 mDynamicFriction;
  88. ///
  89. F32 mStaticFriction;
  90. ///
  91. F32 mRestitution;
  92. ///
  93. F32 mLinearDamping;
  94. ///
  95. F32 mAngularDamping;
  96. ///
  97. F32 mLinearSleepThreshold;
  98. ///
  99. F32 mAngularSleepThreshold;
  100. // A scale applied to the normal linear and angular damping
  101. // when the object enters a water volume.
  102. F32 mWaterDampingScale;
  103. // The density of this object used for water buoyancy effects.
  104. F32 mBuoyancyDensity;
  105. CollisionComponent* mOwnerColComponent;
  106. enum MaskBits {
  107. PositionMask = Parent::NextFreeMask << 0,
  108. FreezeMask = Parent::NextFreeMask << 1,
  109. StateMask = Parent::NextFreeMask << 2,
  110. VelocityMask = Parent::NextFreeMask << 3,
  111. NextFreeMask = Parent::NextFreeMask << 4
  112. };
  113. public:
  114. RigidBodyComponent();
  115. virtual ~RigidBodyComponent();
  116. DECLARE_CONOBJECT(RigidBodyComponent);
  117. virtual bool onAdd();
  118. virtual void onRemove();
  119. static void initPersistFields();
  120. virtual void onComponentAdd();
  121. virtual void onComponentRemove();
  122. virtual void componentAddedToOwner(Component *comp);
  123. virtual void componentRemovedFromOwner(Component *comp);
  124. virtual void ownerTransformSet(MatrixF *mat);
  125. inline F32 getMass() { return mMass; }
  126. Point3F getVelocity() const { return mState.linVelocity; }
  127. void applyImpulse(const Point3F &pos, const VectorF &vec);
  128. void applyRadialImpulse(const Point3F &origin, F32 radius, F32 magnitude);
  129. void updateContainerForces();
  130. virtual U32 packUpdate(NetConnection *con, U32 mask, BitStream *stream);
  131. virtual void unpackUpdate(NetConnection *con, BitStream *stream);
  132. virtual void processTick();
  133. void findContact();
  134. /// Save the current transform as where we return to when a physics reset
  135. /// event occurs. This is automatically set in onAdd but some manipulators
  136. /// such as Prefab need to make use of this.
  137. void storeRestorePos();
  138. void updatePhysics(PhysicsCollision *collision = NULL);
  139. void _onPhysicsReset(PhysicsResetEvent reset);
  140. };
  141. #endif // _RIGID_BODY_COMPONENT_H_