pxMultiActor.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 _PXMULTIACTOR_H
  23. #define _PXMULTIACTOR_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 _T3D_PHYSICS_PHYSICSPLUGIN_H_
  31. #include "T3D/physics/physicsPlugin.h"
  32. #endif
  33. #ifndef _PHYSX_H_
  34. #include "T3D/physics/physx/px.h"
  35. #endif
  36. #ifndef _STRINGUNIT_H_
  37. #include "core/strings/stringUnit.h"
  38. #endif
  39. #ifndef _PHYSICS_PHYSICSUSERDATA_H_
  40. #include "T3D/physics/physicsUserData.h"
  41. #endif
  42. #ifndef _TSSHAPE_H_
  43. #include "ts/tsShape.h"
  44. #endif
  45. class TSShapeInstance;
  46. class BaseMatInstance;
  47. class PxMultiActor;
  48. class PxWorld;
  49. class PxMaterial;
  50. class NxScene;
  51. class NxActor;
  52. class NxShape;
  53. class NxCompartment;
  54. class NxJoint;
  55. class NxMat34;
  56. class NxVec3;
  57. class ParticleEmitterData;
  58. namespace NXU
  59. {
  60. class NxuPhysicsCollection;
  61. }
  62. class PxUserData : public PhysicsUserData
  63. {
  64. public:
  65. /// The constructor.
  66. PxUserData()
  67. : PhysicsUserData(),
  68. mIsBroken( false ),
  69. mParticleEmitterData( NULL )
  70. {
  71. }
  72. static PxUserData* getData( const NxActor &actor )
  73. {
  74. PxUserData *result = (PxUserData*)actor.userData;
  75. AssertFatal( !result || typeid( *result ) == typeid( PxUserData ),
  76. "PxUserData::getData - The pointer is the wrong type!" );
  77. return result;
  78. }
  79. static PxUserData* getData( const NxJoint &joint )
  80. {
  81. PxUserData *result = (PxUserData*)joint.userData;
  82. AssertFatal( !result || typeid( *result ) == typeid( PxUserData ),
  83. "PxUserData::getData - The pointer is the wrong type!" );
  84. return result;
  85. }
  86. typedef Signal<void(NxReal, NxJoint&)> JointBreakSignal;
  87. JointBreakSignal& getOnJointBreakSignal() { return mOnJointBreakSignal; }
  88. // Breakable stuff...
  89. Vector<NxActor*> mUnbrokenActors;
  90. Vector<NxActor*> mBrokenActors;
  91. Vector<NxMat34> mRelXfm;
  92. ParticleEmitterData *mParticleEmitterData;
  93. bool mIsBroken;
  94. JointBreakSignal mOnJointBreakSignal;
  95. };
  96. class ParticleEmitterData;
  97. class PxMultiActorData : public GameBaseData
  98. {
  99. typedef GameBaseData Parent;
  100. public:
  101. PxMultiActorData();
  102. virtual ~PxMultiActorData();
  103. DECLARE_CONOBJECT(PxMultiActorData);
  104. static void initPersistFields();
  105. void packData(BitStream* stream);
  106. void unpackData(BitStream* stream);
  107. bool preload( bool server, String &errorBuffer );
  108. //bool onAdd();
  109. void allocPrimBuffer( S32 overrideSize = -1 );
  110. bool _loadCollection( const UTF8 *path, bool isBinary );
  111. void _onFileChanged( const Torque::Path &path );
  112. void reload();
  113. void dumpModel();
  114. Signal<void(void)> mReloadSignal;
  115. public:
  116. // Rendering
  117. StringTableEntry shapeName;
  118. Resource<TSShape> shape;
  119. PxMaterial *material;
  120. /// Filename to load the physics actor from.
  121. StringTableEntry physXStream;
  122. enum
  123. {
  124. NumMountPoints = 32,
  125. MaxCorrectionNodes = 2
  126. };
  127. StringTableEntry correctionNodeNames[MaxCorrectionNodes];
  128. StringTableEntry mountNodeNames[NumMountPoints];
  129. S32 correctionNodes[MaxCorrectionNodes];
  130. S32 mountPointNode[NumMountPoints]; ///< Node index of mountPoint
  131. /// If true no network corrections will
  132. /// be done during gameplay.
  133. bool noCorrection;
  134. /// Physics collection that holds the actor
  135. /// and all associated shapes and data.
  136. NXU::NxuPhysicsCollection *collection;
  137. bool createActors( NxScene *scene,
  138. NxCompartment *compartment,
  139. const NxMat34 *nxMat,
  140. const Point3F& scale,
  141. Vector<NxActor*> *outActors,
  142. Vector<NxShape*> *outShapes,
  143. Vector<NxJoint*> *outJoints,
  144. Vector<String> *outActorUserProperties,
  145. Vector<String> *outJointUserProperties );
  146. /// Angular and Linear Drag (dampening) is scaled by this when in water.
  147. F32 waterDragScale;
  148. /// The density of this object (for purposes of buoyancy calculation only).
  149. F32 buoyancyDensity;
  150. F32 angularDrag;
  151. F32 linearDrag;
  152. /// If this flag is set to true,
  153. /// the physics actors will only be
  154. /// created on the client, and the server
  155. /// object is only responsible for ghosting.
  156. /// Objects with this flag set will never stop
  157. /// the physics player from moving through them.
  158. bool clientOnly;
  159. bool singlePlayerOnly;
  160. /// When applyImpulse is passed a force of this magnitude or greater
  161. /// any actors hit by the force vector that have broken versions
  162. /// will become 'broken'.
  163. F32 breakForce;
  164. };
  165. class PxMultiActor : public GameBase
  166. {
  167. typedef GameBase Parent;
  168. enum MaskBits
  169. {
  170. MoveMask = Parent::NextFreeMask << 0,
  171. WarpMask = Parent::NextFreeMask << 1,
  172. LightMask = Parent::NextFreeMask << 2,
  173. SleepMask = Parent::NextFreeMask << 3,
  174. ForceSleepMask = Parent::NextFreeMask << 4,
  175. ImpulseMask = Parent::NextFreeMask << 5,
  176. UpdateMask = Parent::NextFreeMask << 6,
  177. MountedMask = Parent::NextFreeMask << 7,
  178. NextFreeMask = Parent::NextFreeMask << 8
  179. };
  180. public:
  181. PxMultiActor();
  182. DECLARE_CONOBJECT( PxMultiActor );
  183. static void initPersistFields();
  184. // SimObject
  185. bool onAdd();
  186. void onRemove();
  187. void inspectPostApply();
  188. void onPhysicsReset( PhysicsResetEvent reset );
  189. void onStaticModified( const char *slotName, const char *newValue );
  190. void onDeleteNotify( SimObject *obj );
  191. // NetObject
  192. U32 packUpdate( NetConnection *conn, U32 mask, BitStream *stream );
  193. void unpackUpdate( NetConnection *conn, BitStream *stream );
  194. // SceneObject
  195. void prepRenderImage( SceneRenderState *state );
  196. void setScale( const VectorF &scale );
  197. void setTransform( const MatrixF &mat );
  198. virtual void mountObject( SceneObject *obj, U32 node );
  199. virtual void unmountObject( SceneObject *obj );
  200. virtual void getMountTransform( U32 mountPoint, MatrixF *mat );
  201. virtual void getRenderMountTransform( U32 index, MatrixF *mat );
  202. // GameBase
  203. virtual bool onNewDataBlock( GameBaseData *dptr, bool reload );
  204. virtual void processTick( const Move *move );
  205. virtual void interpolateTick( F32 delta );
  206. virtual void applyImpulse( const Point3F &pos, const VectorF &vec );
  207. virtual void applyRadialImpulse( const Point3F &origin, F32 radius, F32 magnitude );
  208. /// PxMultiActor
  209. /// @{
  210. /// Set visibility of all broken/unbroken meshes to match this state.
  211. void setAllBroken( bool isBroken );
  212. /// Sets up actors and meshes associated with the passed joint to reflect
  213. /// the desired state.
  214. void setBroken( const NxMat34 &parentPose,
  215. const NxVec3 &parentVel,
  216. PxUserData *userData,
  217. bool isBroken );
  218. ///
  219. void setMeshHidden( String namePrefix, bool hidden );
  220. void setAllHidden( bool hide );
  221. void listMeshes( const String &state ) const;
  222. void _onJointBreak( NxReal breakForce, NxJoint &brokenJoint );
  223. void _onContact( PhysicsUserData *us,
  224. PhysicsUserData *them,
  225. const Point3F &hitPoint,
  226. const Point3F &hitForce );
  227. void applyWarp( const MatrixF& mat, bool interpRender, bool sweep );
  228. void getDynamicXfms( PxMultiActor *srcObj, F32 dt );
  229. /// @}
  230. protected:
  231. /// This creates the physics objects.
  232. bool _createActors( const MatrixF &xfm );
  233. /// Creates a PxUserData for a joint and parses userProperties into it.
  234. PxUserData* _createJointUserData( NxJoint *joint, String &userProperties );
  235. /// Creates a PxUserData and parses userProperties into it.
  236. PxUserData* _createActorUserData( NxActor *actor, String &userProperties );
  237. /// Called to cleanup the physics objects.
  238. void _destroyActors();
  239. NxActor* _findActor( const String &actorName ) const;
  240. /// Get the corresponding meshName for a given actor.
  241. String _getMeshName( const NxActor *actor ) const;
  242. ///
  243. void _updateBounds();
  244. void _updateContainerForces();
  245. void _debugRender( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );
  246. void onFileNotify();
  247. void _applyActorRadialForce( NxActor *inActor, const NxVec3 &origin, F32 radius, F32 magnitude );
  248. void _updateDeltas( bool clearDelta );
  249. bool _getNodeTransform( U32 nodeIdx, MatrixF *outXfm );
  250. protected:
  251. PxMultiActorData *mDataBlock;
  252. PxWorld *mWorld;
  253. Vector<NxActor*> mActors;
  254. Vector<NxActor*> mMappedActors;
  255. Vector<S32> mMappedToActorIndex;
  256. Vector<S32> mMappedActorDL;
  257. Vector<NxJoint*> mJoints;
  258. Vector<NxShape*> mShapes;
  259. /// This is the root actor whose transform is the
  260. /// transform of this SceneObject.
  261. NxActor *mRootActor;
  262. TSShapeInstance *mShapeInstance;
  263. Resource<TSShape> mDebrisShape;
  264. struct Delta
  265. {
  266. Point3F pos;
  267. Point3F lastPos;
  268. QuatF rot;
  269. QuatF lastRot;
  270. };
  271. Delta mDelta;
  272. Vector<Delta> mActorDeltas;
  273. /// The transform of this actor when it was first
  274. /// created. It is used to reset the physics state
  275. /// when the editor is enabled.
  276. MatrixF mResetXfm;
  277. /// The userdata object assigned to all actors
  278. /// and joints of this multi-actor.
  279. //PxUserData mUserData;
  280. ///
  281. //Vector<MatrixF> mRelXfms;
  282. /// This is the scale the actors were built at and
  283. /// is used to decide if we need to recreate them.
  284. VectorF mActorScale;
  285. //F32 mBuildAngDrag;
  286. //F32 mBuildLinDrag;
  287. VectorF mStartImpulse;
  288. bool mDebugRender;
  289. /// A helper set to true if is a client object and
  290. /// is a singlePlayerOnly object.
  291. bool mIsDummy;
  292. /// Helper for
  293. bool mBroken;
  294. };
  295. #endif // _PXMULTIACTOR_H