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