1/*
2 * Copyright (c) 2008-2015, NVIDIA CORPORATION. All rights reserved.
3 *
4 * NVIDIA CORPORATION and its licensors retain all intellectual property
5 * and proprietary rights in and to this software, related documentation
6 * and any modifications thereto. Any use, reproduction, disclosure or
7 * distribution of this software and related documentation without an express
8 * license agreement from NVIDIA CORPORATION is strictly prohibited.
9 */
10// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
11// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
12
13#ifndef PX_VEHICLE_DRIVE_H
14#define PX_VEHICLE_DRIVE_H
15/** \addtogroup vehicle
16 @{
17*/
18
19#include "vehicle/PxVehicleWheels.h"
20#include "vehicle/PxVehicleComponents.h"
21
22#ifndef PX_DOXYGEN
23namespace physx
24{
25#endif
26
27struct PxFilterData;
28class PxGeometry;
29class PxPhysics;
30class PxBatchQuery;
31class PxVehicleDrivableSurfaceToTireFrictionPairs;
32class PxShape;
33class PxMaterial;
34class PxRigidDynamic;
35
36/**
37\brief Data structure describing non-wheel configuration data of a vehicle that has engine, gears, clutch, and auto-box.
38@see PxVehicleWheelsSimData for wheels configuration data.
39*/
40class PxVehicleDriveSimData
41{
42//= ATTENTION! =====================================================================================
43// Changing the data layout of this class breaks the binary serialization format. See comments for
44// PX_BINARY_SERIAL_VERSION. If a modification is required, please adjust the getBinaryMetaData
45// function. If the modification is made on a custom branch, please change PX_BINARY_SERIAL_VERSION
46// accordingly.
47//==================================================================================================
48public:
49
50 friend class PxVehicleDriveTank;
51
52 /**
53 \brief Return the engine data
54 */
55 PX_FORCE_INLINE const PxVehicleEngineData& getEngineData() const
56 {
57 return mEngine;
58 }
59
60 /**
61 \brief Set the engine data
62 \param[in] engine - the data stored in engine is copied to the vehicle's engine.
63 */
64 void setEngineData(const PxVehicleEngineData& engine);
65
66 /**
67 \brief Return the gears data
68 */
69 PX_FORCE_INLINE const PxVehicleGearsData& getGearsData() const
70 {
71 return mGears;
72 }
73
74 /**
75 \brief Set the gears data
76 \param[in] gears - the data stored in gears is copied to the vehicle's gears.
77 */
78 void setGearsData(const PxVehicleGearsData& gears);
79
80 /**
81 \brief Return the clutch data
82 */
83 PX_FORCE_INLINE const PxVehicleClutchData& getClutchData() const
84 {
85 return mClutch;
86 }
87
88 /**
89 \brief Set the clutch data
90 \param[in] clutch - the data stored in clutch is copied to the vehicle's clutch.
91 */
92 void setClutchData(const PxVehicleClutchData& clutch);
93
94 /**
95 \brief Return the autobox data
96 */
97 PX_FORCE_INLINE const PxVehicleAutoBoxData& getAutoBoxData() const
98 {
99 return mAutoBox;
100 }
101
102 /**
103 \brief Set the autobox data
104 \param[in] autobox - the data stored in autobox is copied to the vehicle's autobox.
105 */
106 void setAutoBoxData(const PxVehicleAutoBoxData& autobox);
107
108protected:
109 /*
110 \brief Engine simulation data
111 @see setEngineData, getEngineData
112 */
113 PxVehicleEngineData mEngine;
114
115 /*
116 \brief Gear simulation data
117 @see setGearsData, getGearsData
118 */
119 PxVehicleGearsData mGears;
120
121 /*
122 \brief Clutch simulation data
123 @see setClutchData, getClutchData
124 */
125 PxVehicleClutchData mClutch;
126
127 /*
128 \brief Autobox simulation data
129 @see setAutoboxData, getAutoboxData
130 */
131 PxVehicleAutoBoxData mAutoBox;
132
133 /**
134 \brief Test that a PxVehicleDriveSimData instance has been configured with legal data.
135 Call only after setting all components with setEngineData,setGearsData,setClutchData,setAutoBoxData
136 @see PxVehicleDrive4W::setup, PxVehicleDriveTank::setup
137 */
138 bool isValid() const;
139
140
141//serialization
142public:
143 PxVehicleDriveSimData() {}
144 PxVehicleDriveSimData(const PxEMPTY&) : mEngine(PxEmpty), mGears(PxEmpty), mClutch(PxEmpty), mAutoBox(PxEmpty) {}
145 static void getBinaryMetaData(PxOutputStream& stream);
146//~serialization
147};
148PX_COMPILE_TIME_ASSERT(0==(sizeof(PxVehicleDriveSimData) & 15));
149
150
151/**
152\brief Data structure with instanced dynamics data for vehicle with engine, clutch, gears, autobox
153@see PxVehicleWheelsDynData for wheels dynamics data.
154*/
155class PxVehicleDriveDynData
156{
157public:
158
159 enum
160 {
161 eMAX_NB_ANALOG_INPUTS=16
162 };
163
164 friend class PxVehicleDrive;
165
166 /**
167 \brief Set all dynamics data to zero to bring the vehicle to rest.
168 */
169 void setToRestState();
170
171 /**
172 \brief Set an analog control value to drive the vehicle.
173 \param[in] type describes the type of analog control being modified
174 \param[in] analogVal is the new value of the specific analog control.
175 @see PxVehicleDrive4WControl, PxVehicleDriveNWControl, PxVehicleDriveTankControl
176 */
177 void setAnalogInput(const PxU32 type, const PxReal analogVal);
178
179 /**
180 \brief Get the analog control value that has been applied to the vehicle.
181 \return The value of the specified analog control value.
182 @see PxVehicleDrive4WControl, PxVehicleDriveNWControl, PxVehicleDriveTankControl
183 */
184 PxReal getAnalogInput(const PxU32 type) const;
185
186 /**
187 \brief Inform the vehicle that the gear-up button has been pressed.
188
189 \param[in] digitalVal is the state of the gear-up button.
190
191 \note If digitalVal is true the vehicle will attempt to initiate a gear change at the next call to PxVehicleUpdates.
192
193 \note The value of mGearUpPressed is not reset by PxVehicleUpdates
194 */
195 void setGearUp(const bool digitalVal)
196 {
197 mGearUpPressed = digitalVal;
198 }
199
200 /**
201 \brief Set that the gear-down button has been pressed.
202
203 \param[in] digitalVal is the state of the gear-down button.
204
205 \note If digitalVal is true the vehicle will attempt to initiate a gear change at the next call to PxVehicleUpdates.
206
207 \note The value of mGearDownPressed is not reset by PxVehicleUpdates
208 */
209 void setGearDown(const bool digitalVal)
210 {
211 mGearDownPressed = digitalVal;
212 }
213
214 /**
215 \brief Check if the gear-up button has been pressed
216 \return The state of the gear-up button.
217 */
218 bool getGearUp() const
219 {
220 return mGearUpPressed;
221 }
222
223 /**
224 \brief Check if the gear-down button has been pressed
225 \return The state of the gear-down button.
226 */
227 bool getGearDown() const
228 {
229 return mGearDownPressed;
230 }
231
232 /**
233 \brief Set the flag that will be used to select auto-gears
234 If useAutoGears is true the auto-box will be active.
235 \param[in] useAutoGears is the active state of the auto-box.
236 */
237 PX_FORCE_INLINE void setUseAutoGears(const bool useAutoGears)
238 {
239 mUseAutoGears=useAutoGears;
240 }
241
242 /**
243 \brief Get the flag status that is used to select auto-gears
244 \return The active status of the auto-box.
245 */
246 PX_FORCE_INLINE bool getUseAutoGears() const
247 {
248 return mUseAutoGears;
249 }
250
251 /**
252 \brief Toggle the auto-gears flag
253 If useAutoGears is true the auto-box will be active.
254 */
255 PX_FORCE_INLINE void toggleAutoGears()
256 {
257 mUseAutoGears = !mUseAutoGears;
258 }
259
260 /**
261 \brief Set the current gear.
262
263 \param[in] currentGear is the vehicle's gear.
264
265 \note If the target gear is different from the current gear the vehicle will
266 attempt to start a gear change from the current gear that has just been set
267 towards the target gear at the next call to PxVehicleUpdates.
268
269 @see setTargetGear, PxVehicleGearsData
270 */
271 PX_FORCE_INLINE void setCurrentGear(PxU32 currentGear)
272 {
273 mCurrentGear = currentGear;
274 }
275
276 /**
277 \brief Get the current gear.
278
279 \return The vehicle's current gear.
280
281 @see getTargetGear, PxVehicleGearsData
282 */
283 PX_FORCE_INLINE PxU32 getCurrentGear() const
284 {
285 return mCurrentGear;
286 }
287
288 /**
289 \brief Set the target gear.
290
291 \param[in] targetGear is the vehicle's target gear.
292
293 \note If the target gear is different from the current gear the vehicle will
294 attempt to start a gear change towards the target gear at the next call to
295 PxVehicleUpdates.
296
297 @see PxVehicleGearsData
298 */
299 PX_FORCE_INLINE void setTargetGear(PxU32 targetGear)
300 {
301 mTargetGear = targetGear;
302 }
303
304 /**
305 \brief Get the target gear.
306
307 \return The vehicle's target gear.
308
309 @see setTargetGear, PxVehicleGearsData
310 */
311 PX_FORCE_INLINE PxU32 getTargetGear() const
312 {
313 return mTargetGear;
314 }
315
316 /**
317 \brief Start a gear change to a target gear.
318
319 \param[in] targetGear is the gear the vehicle will begin a transition towards.
320
321 \note The gear change will begin at the next call to PxVehicleUpadates.
322
323 @see PxVehicleGearsData
324 */
325 PX_FORCE_INLINE void startGearChange(const PxU32 targetGear)
326 {
327 mTargetGear=targetGear;
328 }
329
330 /**
331 \brief Force an immediate gear change to a target gear
332
333 \param[in] targetGear is the gear the vehicle will be given immediately.
334
335 @see PxVehicleGearsData
336 */
337 PX_FORCE_INLINE void forceGearChange(const PxU32 targetGear)
338 {
339 mTargetGear=targetGear;
340 mCurrentGear=targetGear;
341 }
342
343 /**
344 \brief Set the rotation speed of the engine (radians per second)
345
346 \param[in] speed is the rotational speed (radians per second) to apply to the engine.
347 */
348 PX_FORCE_INLINE void setEngineRotationSpeed(const PxF32 speed)
349 {
350 mEnginespeed = speed;
351 }
352
353 /**
354 \brief Return the rotation speed of the engine (radians per second)
355
356 \return The rotational speed (radians per second) of the engine.
357 */
358 PX_FORCE_INLINE PxReal getEngineRotationSpeed() const
359 {
360 return mEnginespeed;
361 }
362
363 /**
364 \brief Return the time that has passed since the current gear change was initiated.
365
366 \return The time that has passed since the current gear change was initiated.
367
368 \note If no gear change is in process the gear switch time will be zero.
369
370 @see PxVehicleGearsData.mSwitchTime
371 */
372 PX_FORCE_INLINE PxReal getGearSwitchTime() const
373 {
374 return mGearSwitchTime;
375 }
376
377 /**
378 \brief Return the time that has passed since the autobox last initiated a gear change.
379
380 \return The time that has passed since the autobox last initiated a gear change.
381
382 @see PxVehicleAutoBoxData::setLatency, PxVehicleAutoBoxData::getLatency
383 */
384 PX_FORCE_INLINE PxReal getAutoBoxSwitchTime() const
385 {
386 return mAutoBoxSwitchTime;
387 }
388
389 /**
390 \brief All dynamic data values are public for fast access.
391 */
392
393
394 /**
395 \brief Analog control values used by vehicle simulation.
396 @see setAnalogInput, getAnalogInput, PxVehicleDrive4WControl, PxVehicleDriveNWControl, PxVehicleDriveTankControl
397 */
398 PxReal mControlAnalogVals[eMAX_NB_ANALOG_INPUTS];
399
400 /**
401 \brief Auto-gear flag used by vehicle simulation. Set true to enable the autobox, false to disable the autobox.
402 @see setUseAutoGears, setUseAutoGears, toggleAutoGears, PxVehicleAutoBoxData
403 */
404 bool mUseAutoGears;
405
406 /**
407 \brief Gear-up digital control value used by vehicle simulation.
408
409 \note If true a gear change will be initiated towards currentGear+1 (or to first gear if in reverse).
410
411 @see setDigitalInput, getDigitalInput
412 */
413 bool mGearUpPressed;
414
415 /**
416 \brief Gear-down digital control value used by vehicle simulation.
417
418 \note If true a gear change will be initiated towards currentGear-1 (or to reverse if in first).
419
420 @see setDigitalInput, getDigitalInput
421 */
422 bool mGearDownPressed;
423
424 /**
425 \brief Current gear
426 @see startGearChange, forceGearChange, getCurrentGear, PxVehicleGearsData
427 */
428 PxU32 mCurrentGear;
429
430 /**
431 \brief Target gear (different from current gear if a gear change is underway)
432 @see startGearChange, forceGearChange, getTargetGear, PxVehicleGearsData
433 */
434 PxU32 mTargetGear;
435
436 /**
437 \brief Rotation speed of engine
438 @see setToRestState, getEngineRotationSpeed
439 */
440 PxReal mEnginespeed;
441
442 /**
443 \brief Reported time that has passed since gear change started.
444 @see setToRestState, startGearChange, PxVehicleGearsData::mSwitchTime
445 */
446 PxReal mGearSwitchTime;
447
448 /**
449 \brief Reported time that has passed since last autobox gearup/geardown decision.
450 @see setToRestState, PxVehicleAutoBoxData::setLatency
451 */
452 PxReal mAutoBoxSwitchTime;
453
454private:
455 PxU32 mPad[2];
456
457 /**
458 \brief Test that a PxVehicleDriveDynData instance has legal values.
459 @see setToRestState
460 */
461 bool isValid() const;
462
463//serialization
464public:
465 PxVehicleDriveDynData();
466 PxVehicleDriveDynData(const PxEMPTY&) {}
467 PxU32 getNbAnalogInput() const { return eMAX_NB_ANALOG_INPUTS; }
468 PX_FORCE_INLINE void setGearChange(const PxU32 gearChange) { mTargetGear= gearChange; }
469 PX_FORCE_INLINE PxU32 getGearChange() const { return mTargetGear; }
470 PX_FORCE_INLINE void setGearSwitchTime(const PxReal switchTime) { mGearSwitchTime = switchTime; }
471 PX_FORCE_INLINE void setAutoBoxSwitchTime(const PxReal autoBoxSwitchTime) { mAutoBoxSwitchTime = autoBoxSwitchTime; }
472//~serialization
473};
474PX_COMPILE_TIME_ASSERT(0==(sizeof(PxVehicleDriveDynData) & 15));
475
476/**
477\brief A complete vehicle with instance dynamics data and configuration data for wheels and engine,clutch,gears,autobox.
478@see PxVehicleDrive4W, PxVehicleDriveTank
479*/
480class PxVehicleDrive : public PxVehicleWheels
481{
482//= ATTENTION! =====================================================================================
483// Changing the data layout of this class breaks the binary serialization format. See comments for
484// PX_BINARY_SERIAL_VERSION. If a modification is required, please adjust the getBinaryMetaData
485// function. If the modification is made on a custom branch, please change PX_BINARY_SERIAL_VERSION
486// accordingly.
487//==================================================================================================
488public:
489
490 friend class PxVehicleUpdate;
491
492 /**
493 \brief Dynamics data of vehicle instance.
494 @see setup
495 */
496 PxVehicleDriveDynData mDriveDynData;
497
498protected:
499
500 /**
501 \brief Test that all instanced dynamics data and configuration data have legal values.
502 */
503 bool isValid() const;
504
505 /**
506 \brief Set vehicle to rest.
507 */
508 void setToRestState();
509
510 /**
511 @see PxVehicleDrive4W::allocate, PxVehicleDriveTank::allocate
512 */
513 static PxU32 computeByteSize(const PxU32 nbWheels4);
514
515 /**
516 @see PxVehicleDrive4W::allocate, PxVehicleDriveTank::allocate
517 */
518 static PxU8* patchupPointers(PxVehicleDrive* vehDrive, PxU8* ptr, const PxU32 nbWheels4, const PxU32 nbWheels);
519
520 /**
521 \brief Deallocate a PxVehicle4WDrive instance.
522 @see PxVehicleDrive4W::free, PxVehicleDriveTank::free
523 */
524 void free();
525
526 /**
527 @see PxVehicleDrive4W::setup, PxVehicleDriveTank::setup
528 */
529 void setup
530 (PxPhysics* physics, PxRigidDynamic* vehActor,
531 const PxVehicleWheelsSimData& wheelsData,
532 const PxU32 nbDrivenWheels, const PxU32 nbNonDrivenWheels);
533
534//serialization
535public:
536 static void getBinaryMetaData(PxOutputStream& stream);
537 PxVehicleDrive(PxBaseFlags baseFlags) : PxVehicleWheels(baseFlags), mDriveDynData(PxEmpty) {}
538 virtual const char* getConcreteTypeName() const { return "PxVehicleDrive"; }
539protected:
540 PxVehicleDrive(PxType concreteType, PxBaseFlags baseFlags) : PxVehicleWheels(concreteType, baseFlags) {}
541 ~PxVehicleDrive() {}
542 virtual bool isKindOf(const char* name) const { return !strcmp("PxVehicleDrive", name) || PxBase::isKindOf(name); }
543//~serialization
544};
545PX_COMPILE_TIME_ASSERT(0==(sizeof(PxVehicleDrive) & 15));
546
547#ifndef PX_DOXYGEN
548} // namespace physx
549#endif
550
551/** @} */
552#endif //PX_VEHICLE_DRIVE_H
553