physicsShape.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 _PHYSICSSHAPE_H_
  23. #define _PHYSICSSHAPE_H_
  24. #ifndef _GAMEBASE_H_
  25. #include "T3D/gameBase/gameBase.h"
  26. #endif
  27. #ifndef __RESOURCE_H__
  28. #include "core/resource.h"
  29. #endif
  30. #ifndef _TSSHAPE_H_
  31. #include "ts/tsShape.h"
  32. #endif
  33. #ifndef _T3D_PHYSICSCOMMON_H_
  34. #include "T3D/physics/physicsCommon.h"
  35. #endif
  36. #ifndef _DYNAMIC_CONSOLETYPES_H_
  37. #include "console/dynamicTypes.h"
  38. #endif
  39. #ifndef _SIMOBJECTREF_H_
  40. #include "console/simObjectRef.h"
  41. #endif
  42. #include "T3D/assets/ShapeAsset.h"
  43. class TSShapeInstance;
  44. class PhysicsBody;
  45. class PhysicsWorld;
  46. class PhysicsDebrisData;
  47. class ExplosionData;
  48. class PhysicsShapeData : public GameBaseData
  49. {
  50. typedef GameBaseData Parent;
  51. void _onResourceChanged( const Torque::Path &path );
  52. public:
  53. PhysicsShapeData();
  54. virtual ~PhysicsShapeData();
  55. DECLARE_CONOBJECT(PhysicsShapeData);
  56. static void initPersistFields();
  57. bool onAdd();
  58. void onRemove();
  59. // GameBaseData
  60. void packData(BitStream* stream);
  61. void unpackData(BitStream* stream);
  62. bool preload(bool server, String &errorBuffer );
  63. public:
  64. DECLARE_SHAPEASSET(PhysicsShapeData, Shape, onShapeChanged);
  65. DECLARE_ASSET_SETGET(PhysicsShapeData, Shape);
  66. /// The shared unscaled collision shape.
  67. PhysicsCollisionRef colShape;
  68. ///
  69. F32 mass;
  70. ///
  71. F32 dynamicFriction;
  72. ///
  73. F32 staticFriction;
  74. ///
  75. F32 restitution;
  76. ///
  77. F32 linearDamping;
  78. ///
  79. F32 angularDamping;
  80. ///
  81. F32 linearSleepThreshold;
  82. ///
  83. F32 angularSleepThreshold;
  84. // A scale applied to the normal linear and angular damping
  85. // when the object enters a water volume.
  86. F32 waterDampingScale;
  87. // The density of this object used for water buoyancy effects.
  88. F32 buoyancyDensity;
  89. enum SimType
  90. {
  91. /// This physics representation only exists on the client
  92. /// world and the server only does ghosting.
  93. SimType_ClientOnly,
  94. /// The physics representation only exists on the server world
  95. /// and the client gets delta updates for rendering.
  96. SimType_ServerOnly,
  97. /// The physics representation exists on the client and the server
  98. /// worlds with corrections occuring when the client gets out of sync.
  99. SimType_ClientServer,
  100. /// The bits used to pack the SimType field.
  101. SimType_Bits = 3,
  102. } simType;
  103. SimObjectRef< PhysicsDebrisData > debris;
  104. SimObjectRef< ExplosionData > explosion;
  105. SimObjectRef< PhysicsShapeData > destroyedShape;
  106. void onShapeChanged() {}
  107. };
  108. typedef PhysicsShapeData::SimType PhysicsSimType;
  109. DefineEnumType( PhysicsSimType );
  110. class TSThread;
  111. /// A simple single body dynamic physics object.
  112. class PhysicsShape : public GameBase
  113. {
  114. typedef GameBase Parent;
  115. protected:
  116. /// The abstracted physics actor.
  117. PhysicsBody *mPhysicsRep;
  118. ///
  119. PhysicsWorld *mWorld;
  120. /// The starting position to place the shape when
  121. /// the level begins or is reset.
  122. MatrixF mResetPos;
  123. //VectorF mBuildScale;
  124. //F32 mBuildAngDrag;
  125. //F32 mBuildLinDrag;
  126. /// The rendered shape.
  127. TSShapeInstance *mShapeInst;
  128. /// The current physics state.
  129. PhysicsState mState;
  130. /// The previous and current render states.
  131. PhysicsState mRenderState[2];
  132. /// True if the PhysicsShape has been destroyed ( gameplay ).
  133. bool mDestroyed;
  134. /// Enables automatic playing of the animation named "ambient" (if it exists)
  135. /// when the PhysicsShape is loaded.
  136. bool mPlayAmbient;
  137. S32 mAmbientSeq;
  138. TSThread* mAmbientThread;
  139. /// If a specified to create one in the PhysicsShape data, this is the
  140. /// subshape created when this PhysicsShape is destroyed.
  141. /// Is only assigned (non null) on the serverside PhysicsShape.
  142. SimObjectPtr< PhysicsShape > mDestroyedShape;
  143. ///
  144. enum MaskBits
  145. {
  146. StateMask = Parent::NextFreeMask << 0,
  147. ResetPosMask = Parent::NextFreeMask << 1,
  148. DamageMask = Parent::NextFreeMask << 2,
  149. NextFreeMask = Parent::NextFreeMask << 3
  150. };
  151. bool _createShape();
  152. void _initAmbient();
  153. ///
  154. void _applyCorrection( const MatrixF &mat );
  155. void _onPhysicsReset( PhysicsResetEvent reset );
  156. void _updateContainerForces();
  157. /// If true then no corrections are sent from the server
  158. /// and/or applied from the client.
  159. ///
  160. /// This is only ment for debugging.
  161. ///
  162. static bool smNoCorrections;
  163. /// If true then no smoothing is done on the client when
  164. /// applying server corrections.
  165. ///
  166. /// This is only ment for debugging.
  167. ///
  168. static bool smNoSmoothing;
  169. public:
  170. PhysicsShape();
  171. virtual ~PhysicsShape();
  172. DECLARE_CONOBJECT( PhysicsShape );
  173. /// Returns the PhysicsShapeData datablock.
  174. PhysicsShapeData* getDataBlock() { return static_cast<PhysicsShapeData*>( Parent::getDataBlock() ); }
  175. // SimObject
  176. static void consoleInit();
  177. static void initPersistFields();
  178. void inspectPostApply();
  179. bool onAdd();
  180. void onRemove();
  181. // SceneObject
  182. void prepRenderImage( SceneRenderState *state );
  183. void setTransform( const MatrixF &mat );
  184. F32 getMass() const;
  185. Point3F getVelocity() const { return mState.linVelocity; }
  186. void applyImpulse( const Point3F &pos, const VectorF &vec );
  187. void applyRadialImpulse( const Point3F &origin, F32 radius, F32 magnitude );
  188. void applyTorque( const Point3F &torque );
  189. void applyForce( const Point3F &force );
  190. void setScale(const VectorF & scale);
  191. // GameBase
  192. bool onNewDataBlock( GameBaseData *dptr, bool reload );
  193. void interpolateTick( F32 delta );
  194. void processTick( const Move *move );
  195. void advanceTime( F32 timeDelta );
  196. U32 packUpdate( NetConnection *conn, U32 mask, BitStream *stream );
  197. void unpackUpdate( NetConnection *conn, BitStream *stream );
  198. bool isDestroyed() const { return mDestroyed; }
  199. void destroy();
  200. void restore();
  201. /// Save the current transform as where we return to when a physics reset
  202. /// event occurs. This is automatically set in onAdd but some manipulators
  203. /// such as Prefab need to make use of this.
  204. void storeRestorePos();
  205. };
  206. #endif // _PHYSICSSHAPE_H_