physicsShape.h 7.5 KB

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