rigidShape.h

Engine/source/T3D/rigidShape.h

More...

Classes:

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#ifndef _RIGIDSHAPE_H_
 24#define _RIGIDSHAPE_H_
 25
 26#ifndef _SHAPEBASE_H_
 27#include "T3D/shapeBase.h"
 28#endif
 29#ifndef _RIGID_H_
 30#include "T3D/rigid.h"
 31#endif
 32#ifndef _BOXCONVEX_H_
 33#include "collision/boxConvex.h"
 34#endif
 35#ifndef _T3D_PHYSICS_PHYSICSBODY_H_
 36#include "T3D/physics/physicsBody.h"
 37#endif
 38
 39class ParticleEmitter;
 40class ParticleEmitterData;
 41class ClippedPolyList;
 42class RigidShape;
 43class RigidShapeData : public ShapeBaseData
 44{
 45   typedef ShapeBaseData Parent;
 46
 47  protected:
 48   bool onAdd();
 49
 50   //-------------------------------------- Console set variables
 51  public:
 52
 53   struct Body 
 54   {
 55      enum Sounds 
 56      {
 57         SoftImpactSound,
 58         HardImpactSound,
 59         MaxSounds,
 60      };
 61      SFXTrack* sound[MaxSounds];
 62      F32 restitution;
 63      F32 friction;
 64   } body;
 65
 66   enum RigidShapeConsts
 67   {
 68      VC_NUM_DUST_EMITTERS = 1,
 69      VC_NUM_BUBBLE_EMITTERS = 1,
 70      VC_NUM_SPLASH_EMITTERS = 2,
 71      VC_BUBBLE_EMITTER = VC_NUM_BUBBLE_EMITTERS,
 72   };
 73
 74  enum Sounds 
 75  {
 76      ExitWater,
 77      ImpactSoft,
 78      ImpactMedium,
 79      ImpactHard,
 80      Wake,
 81      MaxSounds
 82   };
 83   SFXTrack* waterSound[MaxSounds];
 84
 85   F32 exitSplashSoundVel;
 86   F32 softSplashSoundVel;
 87   F32 medSplashSoundVel;
 88   F32 hardSplashSoundVel;
 89 
 90   F32 minImpactSpeed;
 91   F32 softImpactSpeed;
 92   F32 hardImpactSpeed;
 93   F32 minRollSpeed;
 94   
 95   bool cameraRoll;           ///< Roll the 3rd party camera
 96   F32 cameraLag;             ///< Amount of camera lag (lag += car velocity * lag)
 97   F32 cameraDecay;           ///< Rate at which camera returns to target pos.
 98   F32 cameraOffset;          ///< Vertical offset
 99
100   F32 minDrag;
101   F32 maxDrag;
102   S32 integration;           ///< # of physics steps per tick
103   F32 collisionTol;          ///< Collision distance tolerance
104   F32 contactTol;            ///< Contact velocity tolerance
105   Point3F massCenter;        ///< Center of mass for rigid body
106   Point3F massBox;           ///< Size of inertial box
107
108   ParticleEmitterData * dustEmitter;
109   S32 dustID;
110   F32 triggerDustHeight;  ///< height shape has to be under to kick up dust
111   F32 dustHeight;         ///< dust height above ground
112
113   ParticleEmitterData* splashEmitterList[VC_NUM_SPLASH_EMITTERS];
114   S32 splashEmitterIDList[VC_NUM_SPLASH_EMITTERS];
115   F32 splashFreqMod;
116   F32 splashVelEpsilon;
117
118   bool enablePhysicsRep;
119
120
121   F32 dragForce;
122   F32 vertFactor;
123   
124   ParticleEmitterData * dustTrailEmitter;
125   S32                   dustTrailID;
126
127   //-------------------------------------- load set variables
128
129  public:
130   RigidShapeData();
131   ~RigidShapeData();
132
133   static void initPersistFields();
134   void packData(BitStream*);
135   void unpackData(BitStream*);
136   bool preload(bool server, String &errorStr);
137
138   DECLARE_CONOBJECT(RigidShapeData);
139
140   DECLARE_CALLBACK(void, onEnterLiquid, (RigidShape* obj, F32 coverage, const char* type));
141   DECLARE_CALLBACK(void, onLeaveLiquid, (RigidShape* obj, const char* type));
142
143};
144
145
146//----------------------------------------------------------------------------
147
148class RigidShape: public ShapeBase
149{
150   typedef ShapeBase Parent;
151
152  private:
153   RigidShapeData* mDataBlock;
154   SimObjectPtr<ParticleEmitter> mDustTrailEmitter;
155
156  protected:
157   enum MaskBits {
158      PositionMask   = Parent::NextFreeMask << 0,
159      EnergyMask     = Parent::NextFreeMask << 1,
160      FreezeMask     = Parent::NextFreeMask << 2,
161      ForceMoveMask  = Parent::NextFreeMask << 3,
162      NextFreeMask = Parent::NextFreeMask << 4
163   };
164
165   void updateDustTrail( F32 dt );
166
167
168   struct StateDelta 
169   {
170      Move move;                    ///< Last move from server
171      F32 dt;                       ///< Last interpolation time
172      // Interpolation data
173      Point3F pos;
174      Point3F posVec;
175      QuatF rot[2];
176      // Warp data
177      S32 warpTicks;                ///< Number of ticks to warp
178      S32 warpCount;                ///< Current pos in warp
179      Point3F warpOffset;
180      QuatF warpRot[2];
181      //
182      Point3F cameraOffset;
183      Point3F cameraVec;
184      Point3F cameraRot;
185      Point3F cameraRotVec;
186   };
187
188   PhysicsBody* mPhysicsRep;
189
190   StateDelta mDelta;
191   S32 mPredictionCount;            ///< Number of ticks to predict
192   bool inLiquid;
193
194   Point3F mCameraOffset; ///< 3rd person camera
195
196   // Rigid Body
197   bool mDisableMove;
198
199   CollisionList mCollisionList;
200   CollisionList mContacts;
201   ShapeBaseConvex mConvex;
202   S32 restCount;
203
204   SimObjectPtr<ParticleEmitter> mDustEmitterList[RigidShapeData::VC_NUM_DUST_EMITTERS];
205   SimObjectPtr<ParticleEmitter> mSplashEmitterList[RigidShapeData::VC_NUM_SPLASH_EMITTERS];
206
207   GFXStateBlockRef  mSolidSB;
208
209   Box3F         mWorkingQueryBox;
210   S32           mWorkingQueryBoxCountDown;
211
212   //
213   bool onNewDataBlock( GameBaseData *dptr, bool reload );
214   void updatePos(F32 dt);
215   bool updateCollision(F32 dt);
216   bool resolveCollision(Rigid& ns,CollisionList& cList);
217   bool resolveContacts(Rigid& ns,CollisionList& cList,F32 dt);
218   bool resolveDisplacement(Rigid& ns,CollisionState *state,F32 dt);
219   void checkTriggers();
220   static void findCallback(SceneObject* obj,void * key);
221
222   void setPosition(const Point3F& pos,const QuatF& rot);
223   void setRenderPosition(const Point3F& pos,const QuatF& rot);
224   void setTransform(const MatrixF& mat);
225
226//   virtual bool collideBody(const MatrixF& mat,Collision* info) = 0;
227   void updateMove(const Move* move);
228
229   void writePacketData(GameConnection * conn, BitStream *stream);
230   void readPacketData (GameConnection * conn, BitStream *stream);
231
232   void updateLiftoffDust( F32 dt );
233
234   void updateWorkingCollisionSet(const U32 mask);
235   U32 getCollisionMask();
236
237   void updateFroth( F32 dt );
238   bool collidingWithWater( Point3F &waterHeight );
239
240   void _renderMassAndContacts( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );
241
242   void updateForces(F32 dt);
243
244public:
245   // Test code...
246   static ClippedPolyList* sPolyList;
247   Rigid mRigid;
248
249   //
250   RigidShape();
251   ~RigidShape();
252
253   static void consoleInit();
254   static void initPersistFields();
255   void processTick(const Move *move);
256   bool onAdd();
257   void onRemove();
258   void _createPhysics();
259
260   /// Interpolates between move ticks @see processTick
261   /// @param   dt   Change in time between the last call and this call to the function
262   void interpolateTick(F32 dt);
263   void advanceTime(F32 dt);
264
265   /// Disables collisions for this shape
266   void disableCollision();
267   
268   /// Enables collisions for this shape
269   void enableCollision();
270
271   /// Returns the velocity of the shape
272   Point3F getVelocity() const;
273
274   void setEnergyLevel(F32 energy);
275   
276   void prepBatchRender(  SceneRenderState *state, S32 mountedImageIndex );
277
278   // xgalaxy cool hacks
279   void reset();
280   void freezeSim(bool frozen);
281
282   ///@name Rigid body methods
283   ///@{
284   
285   /// This method will get the velocity of the object, taking into account
286   /// angular velocity.
287   /// @param   r   Point on the object you want the velocity of, relative to Center of Mass
288   /// @param   vel   Velocity (out)
289   void getVelocity(const Point3F& r, Point3F* vel);
290   
291   /// Applies an impulse force 
292   /// @param   r   Point on the object to apply impulse to, r is relative to Center of Mass
293   /// @param   impulse   Impulse vector to apply.
294   void applyImpulse(const Point3F &r, const Point3F &impulse);
295
296   /// Forces the client to jump to the RigidShape's transform rather
297   /// then warp to it.
298   void forceClientTransform();
299
300   void getCameraParameters(F32 *min, F32* max, Point3F* offset, MatrixF* rot);
301   void getCameraTransform(F32* pos, MatrixF* mat);
302   ///@}
303
304   U32  packUpdate  (NetConnection *conn, U32 mask, BitStream *stream);
305   void unpackUpdate(NetConnection *conn,           BitStream *stream);
306
307   DECLARE_CONOBJECT(RigidShape);
308};
309
310
311#endif
312