vehicle.h

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