pxMultiActor.h

Engine/source/T3D/physics/physx/pxMultiActor.h

More...

Classes:

Namespaces:

namespace

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _PXMULTIACTOR_H
 25#define _PXMULTIACTOR_H
 26
 27#ifndef _GAMEBASE_H_
 28#include "T3D/gameBase/gameBase.h"
 29#endif
 30#ifndef __RESOURCE_H__
 31#include "core/resource.h"
 32#endif
 33#ifndef _T3D_PHYSICS_PHYSICSPLUGIN_H_
 34#include "T3D/physics/physicsPlugin.h"
 35#endif
 36#ifndef _PHYSX_H_
 37#include "T3D/physics/physx/px.h"
 38#endif
 39#ifndef _STRINGUNIT_H_
 40#include "core/strings/stringUnit.h"
 41#endif
 42#ifndef _PHYSICS_PHYSICSUSERDATA_H_
 43#include "T3D/physics/physicsUserData.h"
 44#endif
 45#ifndef _TSSHAPE_H_
 46#include "ts/tsShape.h"
 47#endif
 48
 49
 50class TSShapeInstance;
 51class BaseMatInstance;
 52class PxMultiActor;
 53class PxWorld;
 54class PxMaterial;
 55class NxScene;
 56class NxActor;
 57class NxShape;
 58class NxCompartment;
 59class NxJoint;
 60class NxMat34;
 61class NxVec3;
 62class ParticleEmitterData;
 63
 64
 65namespace NXU
 66{
 67   class NxuPhysicsCollection;
 68}
 69
 70
 71class PxUserData : public PhysicsUserData
 72{
 73public:
 74
 75   /// The constructor.
 76   PxUserData()
 77      :  PhysicsUserData(),
 78         mIsBroken( false ),
 79         mParticleEmitterData( NULL )
 80   {
 81   }
 82
 83   static PxUserData* getData( const NxActor &actor )
 84   {
 85      PxUserData *result = (PxUserData*)actor.userData;
 86
 87      AssertFatal( !result || typeid( *result ) == typeid( PxUserData ),
 88          "PxUserData::getData - The pointer is the wrong type!" );
 89
 90      return result;
 91   }
 92
 93   static PxUserData* getData( const NxJoint &joint )
 94   {
 95      PxUserData *result = (PxUserData*)joint.userData;
 96
 97      AssertFatal( !result || typeid( *result ) == typeid( PxUserData ),
 98          "PxUserData::getData - The pointer is the wrong type!" );
 99
100      return result;
101   }
102
103   typedef Signal<void(NxReal, NxJoint&)> JointBreakSignal;
104
105   JointBreakSignal& getOnJointBreakSignal() { return mOnJointBreakSignal; }
106
107   // Breakable stuff...
108   Vector<NxActor*> mUnbrokenActors;
109   Vector<NxActor*> mBrokenActors;
110   Vector<NxMat34> mRelXfm;
111   ParticleEmitterData *mParticleEmitterData;  
112   bool mIsBroken;
113   JointBreakSignal mOnJointBreakSignal;
114};
115
116
117class ParticleEmitterData;
118
119class PxMultiActorData : public GameBaseData
120{
121   typedef GameBaseData Parent;
122
123public:
124
125   PxMultiActorData();
126   virtual ~PxMultiActorData();
127
128   DECLARE_CONOBJECT(PxMultiActorData);
129   
130   static void initPersistFields();
131   
132   void packData(BitStream* stream);
133   void unpackData(BitStream* stream);
134   
135   bool preload( bool server, String &errorBuffer );
136   //bool onAdd();
137   
138   void allocPrimBuffer( S32 overrideSize = -1 );
139
140   bool _loadCollection( const UTF8 *path, bool isBinary );
141
142   void _onFileChanged( const Torque::Path &path );
143
144   void reload();
145
146   void dumpModel();
147
148   Signal<void(void)> mReloadSignal;
149
150public:
151
152   // Rendering
153   StringTableEntry shapeName;
154   Resource<TSShape> shape;
155
156   PxMaterial *material;
157
158   /// Filename to load the physics actor from.
159   StringTableEntry physXStream;
160
161   enum
162   {      
163      NumMountPoints = 32,
164      MaxCorrectionNodes = 2
165   };
166
167   StringTableEntry correctionNodeNames[MaxCorrectionNodes];
168   StringTableEntry mountNodeNames[NumMountPoints];
169   S32 correctionNodes[MaxCorrectionNodes];
170   S32 mountPointNode[NumMountPoints];  ///< Node index of mountPoint
171
172   /// If true no network corrections will 
173   /// be done during gameplay.
174   bool noCorrection;
175
176   /// Physics collection that holds the actor
177   /// and all associated shapes and data.
178   NXU::NxuPhysicsCollection *collection;
179
180   bool createActors(   NxScene *scene,
181                        NxCompartment *compartment,
182                        const NxMat34 *nxMat, 
183                        const Point3F& scale, 
184                        Vector<NxActor*> *outActors,
185                        Vector<NxShape*> *outShapes,
186                        Vector<NxJoint*> *outJoints,
187                        Vector<String> *outActorUserProperties,
188                        Vector<String> *outJointUserProperties );
189
190   /// Angular and Linear Drag (dampening) is scaled by this when in water.
191   F32 waterDragScale;
192
193   /// The density of this object (for purposes of buoyancy calculation only).
194   F32 buoyancyDensity;
195
196   F32 angularDrag;
197   F32 linearDrag;
198
199   /// If this flag is set to true,
200   /// the physics actors will only be
201   /// created on the client, and the server
202   /// object is only responsible for ghosting.
203   /// Objects with this flag set will never stop
204   /// the physics player from moving through them.
205   bool clientOnly;
206
207   bool singlePlayerOnly;
208
209   /// When applyImpulse is passed a force of this magnitude or greater
210   /// any actors hit by the force vector that have broken versions 
211   /// will become 'broken'.
212   F32 breakForce;
213};
214
215
216class PxMultiActor : public GameBase
217{
218   typedef GameBase Parent;
219
220   enum MaskBits 
221   {
222      MoveMask          = Parent::NextFreeMask << 0,
223      WarpMask          = Parent::NextFreeMask << 1,
224      LightMask         = Parent::NextFreeMask << 2,
225      SleepMask         = Parent::NextFreeMask << 3,
226      ForceSleepMask    = Parent::NextFreeMask << 4,
227      ImpulseMask       = Parent::NextFreeMask << 5,
228      UpdateMask        = Parent::NextFreeMask << 6,
229      MountedMask       = Parent::NextFreeMask << 7,
230      NextFreeMask      = Parent::NextFreeMask << 8
231   };  
232
233public:
234
235   PxMultiActor();
236
237   DECLARE_CONOBJECT( PxMultiActor );
238   static void initPersistFields();
239
240   // SimObject
241   bool onAdd();
242   void onRemove();
243   void inspectPostApply();
244   void onPhysicsReset( PhysicsResetEvent reset );
245   void onStaticModified( const char *slotName, const char *newValue );
246   void onDeleteNotify( SimObject *obj );
247
248   // NetObject
249   U32 packUpdate( NetConnection *conn, U32 mask, BitStream *stream );
250   void unpackUpdate( NetConnection *conn, BitStream *stream );
251
252   // SceneObject
253   void prepRenderImage( SceneRenderState *state );
254   void setScale( const VectorF &scale );
255   void setTransform( const MatrixF &mat );
256   virtual void mountObject( SceneObject *obj, U32 node );
257   virtual void unmountObject( SceneObject *obj );   
258   virtual void getMountTransform( U32 mountPoint, MatrixF *mat );
259   virtual void getRenderMountTransform( U32 index, MatrixF *mat );
260
261   // GameBase
262   virtual bool onNewDataBlock( GameBaseData *dptr, bool reload );
263   virtual void processTick( const Move *move );
264   virtual void interpolateTick( F32 delta );
265   virtual void applyImpulse( const Point3F &pos, const VectorF &vec );
266   virtual void applyRadialImpulse( const Point3F &origin, F32 radius, F32 magnitude );
267
268   /// PxMultiActor
269   /// @{  
270
271   /// Set visibility of all broken/unbroken meshes to match this state.
272   void setAllBroken( bool isBroken );
273
274   /// Sets up actors and meshes associated with the passed joint to reflect
275   /// the desired state.
276   void setBroken(   const NxMat34 &parentPose, 
277                     const NxVec3 &parentVel,
278                     PxUserData *userData, 
279                     bool isBroken );
280
281   /// 
282   void setMeshHidden( String namePrefix, bool hidden );
283   
284   void setAllHidden( bool hide );
285
286   void listMeshes( const String &state ) const;
287   
288   void _onJointBreak( NxReal breakForce, NxJoint &brokenJoint );
289
290   void _onContact(  PhysicsUserData *us,
291                     PhysicsUserData *them,
292                     const Point3F &hitPoint,
293                     const Point3F &hitForce );
294
295   void applyWarp( const MatrixF& mat, bool interpRender, bool sweep );
296
297   void getDynamicXfms( PxMultiActor *srcObj, F32 dt );        
298
299   /// @}
300
301protected:
302
303   /// This creates the physics objects.
304   bool _createActors( const MatrixF &xfm );
305
306   /// Creates a PxUserData for a joint and parses userProperties into it.
307   PxUserData* _createJointUserData( NxJoint *joint, String &userProperties );
308
309   /// Creates a PxUserData and parses userProperties into it.
310   PxUserData* _createActorUserData( NxActor *actor, String &userProperties );
311
312   /// Called to cleanup the physics objects.
313   void _destroyActors();
314
315   NxActor* _findActor( const String &actorName ) const;
316
317   /// Get the corresponding meshName for a given actor.   
318   String _getMeshName( const NxActor *actor ) const;   
319
320   ///
321   void _updateBounds();
322
323   void _updateContainerForces();
324
325   void _debugRender( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );
326
327   void onFileNotify();
328
329   void _applyActorRadialForce( NxActor *inActor, const NxVec3 &origin, F32 radius, F32 magnitude );
330
331   void _updateDeltas( bool clearDelta );
332
333   bool _getNodeTransform( U32 nodeIdx, MatrixF *outXfm );
334
335protected:
336
337   PxMultiActorData *mDataBlock;
338
339   PxWorld *mWorld;
340
341   Vector<NxActor*> mActors;
342   Vector<NxActor*> mMappedActors;
343   Vector<S32> mMappedToActorIndex;
344   Vector<S32> mMappedActorDL;
345   Vector<NxJoint*> mJoints;
346   Vector<NxShape*> mShapes;
347
348   /// This is the root actor whose transform is the
349   /// transform of this SceneObject.
350   NxActor *mRootActor;  
351
352   TSShapeInstance *mShapeInstance;
353   Resource<TSShape> mDebrisShape;
354
355   struct Delta
356   {
357      Point3F pos;
358      Point3F lastPos;
359      QuatF rot;
360      QuatF lastRot;
361   };
362
363   Delta mDelta;
364
365   Vector<Delta> mActorDeltas;
366
367   /// The transform of this actor when it was first
368   /// created.  It is used to reset the physics state
369   /// when the editor is enabled.
370   MatrixF mResetXfm;
371
372
373   /// The userdata object assigned to all actors
374   /// and joints of this multi-actor.
375   //PxUserData mUserData;
376
377   ///
378   //Vector<MatrixF> mRelXfms;
379
380   /// This is the scale the actors were built at and
381   /// is used to decide if we need to recreate them.
382   VectorF mActorScale;
383   //F32 mBuildAngDrag;
384   //F32 mBuildLinDrag;
385
386   VectorF mStartImpulse;
387
388   bool mDebugRender;
389
390   /// A helper set to true if is a client object and
391   /// is a singlePlayerOnly object.
392   bool mIsDummy;
393
394   /// Helper for 
395   bool mBroken;
396};
397
398#endif // _PXMULTIACTOR_H
399
400