physicsShape.h

Engine/source/T3D/physics/physicsShape.h

More...

Classes:

class

A simple single body dynamic physics object.

Public Typedefs

Public Functions

Detailed Description

Public Typedefs

typedef PhysicsShapeData::SimType PhysicsSimType 

Public Functions

DefineEnumType(PhysicsSimType )

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