Torque3D Documentation / _generateds / wheeledVehicle.h

wheeledVehicle.h

Engine/source/T3D/vehicles/wheeledVehicle.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 _WHEELEDVEHICLE_H_
 25#define _WHEELEDVEHICLE_H_
 26
 27#ifndef _VEHICLE_H_
 28#include "T3D/vehicles/vehicle.h"
 29#endif
 30
 31#ifndef _CLIPPEDPOLYLIST_H_
 32#include "collision/clippedPolyList.h"
 33#endif
 34
 35class ParticleEmitter;
 36class ParticleEmitterData;
 37
 38
 39//----------------------------------------------------------------------------
 40
 41struct WheeledVehicleTire: public SimDataBlock 
 42{
 43   typedef SimDataBlock Parent;
 44
 45   //
 46   StringTableEntry shapeName;// Max shape to render
 47
 48   // Physical properties
 49   F32 mass;                  // Mass of the whole wheel
 50   F32 kineticFriction;       // Tire friction coefficient
 51   F32 staticFriction;        // Tire friction coefficient
 52   F32 restitution;           // Currently not used
 53
 54   // Tires act as springs and generate lateral and longitudinal
 55   // forces to move the vehicle. These distortion/spring forces
 56   // are what convert wheel angular velocity into forces that
 57   // act on the rigid body.
 58   F32 lateralForce;          // Spring force
 59   F32 lateralDamping;        // Damping force
 60   F32 lateralRelaxation;     // The tire will relax if left alone
 61   F32 longitudinalForce;
 62   F32 longitudinalDamping;
 63   F32 longitudinalRelaxation;
 64
 65   // Shape information initialized in the preload
 66   Resource<TSShape> shape;   // The loaded shape
 67   F32 radius;                // Tire radius
 68
 69   //
 70   WheeledVehicleTire();
 71   DECLARE_CONOBJECT(WheeledVehicleTire);
 72   static void initPersistFields();
 73   bool preload(bool, String &errorStr);
 74   virtual void packData(BitStream* stream);
 75   virtual void unpackData(BitStream* stream);
 76};
 77
 78
 79//----------------------------------------------------------------------------
 80
 81struct WheeledVehicleSpring: public SimDataBlock 
 82{
 83   typedef SimDataBlock Parent;
 84
 85   F32 length;                // Travel distance from root hub position
 86   F32 force;                 // Spring force
 87   F32 damping;               // Damping force
 88   F32 antiSway;              // Opposite wheel anti-sway
 89
 90   //
 91   WheeledVehicleSpring();
 92   DECLARE_CONOBJECT(WheeledVehicleSpring);
 93   static void initPersistFields();
 94   virtual void packData(BitStream* stream);
 95   virtual void unpackData(BitStream* stream);
 96};
 97
 98
 99//----------------------------------------------------------------------------
100
101struct WheeledVehicleData: public VehicleData 
102{
103   typedef VehicleData Parent;
104
105   enum Constants 
106   {
107      MaxWheels = 8,
108      MaxWheelBits = 3
109   };
110
111   enum Sounds 
112   {
113      JetSound,
114      EngineSound,
115      SquealSound,
116      WheelImpactSound,
117      MaxSounds,
118   };
119   SFXTrack* sound[MaxSounds];
120
121   ParticleEmitterData* tireEmitter;
122
123   F32 maxWheelSpeed;            // Engine torque is scale based on wheel speed
124   F32 engineTorque;             // Engine force controlled through throttle
125   F32 engineBrake;              // Break force applied when throttle is 0
126   F32 brakeTorque;              // Force used when brakeing
127
128   // Initialized onAdd
129   struct Wheel 
130   {
131      S32 opposite;              // Opposite wheel on Y axis (or -1 for none)
132      Point3F pos;               // Root pos of spring
133      S32 springNode;            // Wheel spring/hub node
134      S32 springSequence;        // Suspension animation
135      F32 springLength;          // Suspension animation length
136   } wheel[MaxWheels];
137   U32 wheelCount;
138   ClippedPolyList rigidBody;    // Extracted from shape
139   S32 brakeLightSequence;       // Brakes
140   S32 steeringSequence;         // Steering animation
141
142   //
143   WheeledVehicleData();
144   DECLARE_CONOBJECT(WheeledVehicleData);
145   static void initPersistFields();
146   bool preload(bool, String &errorStr);
147   bool mirrorWheel(Wheel* we);
148   virtual void packData(BitStream* stream);
149   virtual void unpackData(BitStream* stream);
150};
151
152
153//----------------------------------------------------------------------------
154
155class WheeledVehicle: public Vehicle
156{
157   typedef Vehicle Parent;
158
159   enum MaskBits 
160   {
161      WheelMask    = Parent::NextFreeMask << 0,
162      NextFreeMask = Parent::NextFreeMask << 1
163   };
164
165   WheeledVehicleData* mDataBlock;
166
167   bool mBraking;
168   TSThread* mTailLightThread;
169   SFXSource* mJetSound;
170   SFXSource* mEngineSound;
171   SFXSource* mSquealSound;
172
173   struct Wheel 
174   {
175      WheeledVehicleTire *tire;
176      WheeledVehicleSpring *spring;
177      WheeledVehicleData::Wheel* data;
178
179      F32 extension;          // Spring extension (0-1)
180      F32 avel;               // Angular velocity
181      F32 apos;               // Anuglar position (client side only)
182      F32 Dy,Dx;              // Current tire deformation
183
184      struct Surface 
185      {
186         bool contact;        // Wheel is touching a surface
187         Point3F normal;      // Surface normal
188         BaseMatInstance* material; // Surface material
189         Point3F pos;         // Point of contact
190         SceneObject* object; // Object in contact with
191      } surface;
192
193      TSShapeInstance* shapeInstance;
194      TSThread* springThread;
195
196      F32 steering;           // Wheel steering scale
197      bool powered;           // Powered by engine
198      bool slipping;          // Traction on last tick
199      F32 torqueScale;        // Max torque % applied to wheel (0-1)
200      F32 slip;               // Amount of wheel slip (0-1)
201      SimObjectPtr<ParticleEmitter> emitter;
202   };
203   Wheel mWheel[WheeledVehicleData::MaxWheels];
204   TSThread* mSteeringThread;
205
206   //
207   bool onNewDataBlock( GameBaseData *dptr, bool reload );
208   void processTick(const Move *move);
209   void updateMove(const Move *move);
210   void updateForces(F32 dt);
211   void extendWheels(bool clientHack = false);
212   void prepBatchRender( SceneRenderState *state, S32 mountedImageIndex );
213
214   // Client sounds & particles
215   void updateWheelThreads();
216   void updateWheelParticles(F32 dt);
217   void updateEngineSound(F32 level);
218   void updateSquealSound(F32 level);
219   void updateJetSound();
220
221   virtual U32 getCollisionMask();
222
223public:
224   DECLARE_CONOBJECT(WheeledVehicle);
225   static void initPersistFields();
226
227   WheeledVehicle();
228   ~WheeledVehicle();
229
230   bool onAdd();
231   void onRemove();
232   void advanceTime(F32 dt);
233   bool buildPolyList(PolyListContext context, AbstractPolyList* polyList, const Box3F &box, const SphereF &sphere);
234
235   S32 getWheelCount();
236   Wheel *getWheel(U32 index) {return &mWheel[index];}
237   void setWheelSteering(S32 wheel,F32 steering);
238   void setWheelPowered(S32 wheel,bool powered);
239   void setWheelTire(S32 wheel,WheeledVehicleTire*);
240   void setWheelSpring(S32 wheel,WheeledVehicleSpring*);
241
242   void getWheelInstAndTransform( U32 wheel, TSShapeInstance** inst, MatrixF* xfrm ) const;
243
244   void writePacketData(GameConnection * conn, BitStream *stream);
245   void readPacketData(GameConnection * conn, BitStream *stream);
246   U32  packUpdate(NetConnection * conn, U32 mask, BitStream *stream);
247   void unpackUpdate(NetConnection * conn, BitStream *stream);
248};
249
250
251#endif
252