RavEngine
Loading...
Searching...
No Matches
CustomSuspension.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#pragma once
30
31#include "vehicle2/PxVehicleAPI.h"
32#include "PxPhysicsAPI.h"
33
34#include "../snippetvehicle2common/directdrivetrain/DirectDrivetrain.h"
35
36namespace snippetvehicle2
37{
38
39using namespace physx;
40using namespace physx::vehicle2;
41
43{
44 PxReal phase;
45 PxReal frequency;
46 PxReal amplitude;
47};
48
50{
51 PxReal theta;
52
53 PX_FORCE_INLINE void setToDefault()
54 {
55 PxMemZero(this, sizeof(CustomSuspensionState));
56 }
57};
58
59void addCustomSuspensionForce
60(const PxReal dt,
61 const PxVehicleSuspensionParams& suspParams,
62 const CustomSuspensionParams& customParams,
63 const PxVehicleRoadGeometryState& roadGeomState, const PxVehicleSuspensionComplianceState& suspComplianceState, const PxVehicleRigidBodyState& rigidBodyState,
64 PxVehicleSuspensionForce& suspForce, CustomSuspensionState& customState);
65
67{
68public:
69
71
72 virtual void getDataForCustomSuspensionComponent(
73 const PxVehicleAxleDescription*& axleDescription,
74 const PxVehicleRigidBodyParams*& rigidBodyParams,
75 const PxVehicleSuspensionStateCalculationParams*& suspensionStateCalculationParams,
76 const PxReal*& steerResponseStates,
77 const PxVehicleRigidBodyState*& rigidBodyState,
78 const PxVehicleWheelParams*& wheelParams,
79 const PxVehicleSuspensionParams*& suspensionParams,
80 const CustomSuspensionParams*& customSuspensionParams,
81 const PxVehicleSuspensionComplianceParams*& suspensionComplianceParams,
82 const PxVehicleSuspensionForceParams*& suspensionForceParams,
83 const PxVehicleRoadGeometryState*& wheelRoadGeomStates,
84 PxVehicleSuspensionState*& suspensionStates,
85 CustomSuspensionState*& customSuspensionStates,
86 PxVehicleSuspensionComplianceState*& suspensionComplianceStates,
87 PxVehicleSuspensionForce*& suspensionForces) = 0;
88
89 virtual bool update(const PxReal dt, const PxVehicleSimulationContext& context)
90 {
91 const PxVehicleAxleDescription* axleDescription;
92 const PxVehicleRigidBodyParams* rigidBodyParams;
93 const PxVehicleSuspensionStateCalculationParams* suspensionStateCalculationParams;
94 const PxReal* steerResponseStates;
95 const PxVehicleRigidBodyState* rigidBodyState;
96 const PxVehicleWheelParams* wheelParams;
97 const PxVehicleSuspensionParams* suspensionParams;
98 const CustomSuspensionParams* customSuspensionParams;
99 const PxVehicleSuspensionComplianceParams* suspensionComplianceParams;
100 const PxVehicleSuspensionForceParams* suspensionForceParams;
101 const PxVehicleRoadGeometryState* wheelRoadGeomStates;
102 PxVehicleSuspensionState* suspensionStates;
103 CustomSuspensionState* customSuspensionStates;
104 PxVehicleSuspensionComplianceState* suspensionComplianceStates;
105 PxVehicleSuspensionForce* suspensionForces;
106
107 getDataForCustomSuspensionComponent(axleDescription, rigidBodyParams, suspensionStateCalculationParams,
108 steerResponseStates, rigidBodyState, wheelParams, suspensionParams, customSuspensionParams,
109 suspensionComplianceParams, suspensionForceParams, wheelRoadGeomStates,
110 suspensionStates, customSuspensionStates, suspensionComplianceStates, suspensionForces);
111
112 for (PxU32 i = 0; i < axleDescription->nbWheels; i++)
113 {
114 const PxU32 wheelId = axleDescription->wheelIdsInAxleOrder[i];
115
116 //Update the suspension state (jounce, jounce speed)
117 PxVehicleSuspensionStateUpdate(
118 wheelParams[wheelId], suspensionParams[wheelId], *suspensionStateCalculationParams,
119 suspensionForceParams[wheelId].stiffness, suspensionForceParams[wheelId].damping,
120 steerResponseStates[wheelId], wheelRoadGeomStates[wheelId],
121 *rigidBodyState,
122 dt, context.frame, context.gravity,
123 suspensionStates[wheelId]);
124
125 //Update the compliance from the suspension state.
126 PxVehicleSuspensionComplianceUpdate(
127 suspensionParams[wheelId], suspensionComplianceParams[wheelId],
128 suspensionStates[wheelId],
129 suspensionComplianceStates[wheelId]);
130
131 //Compute the suspension force from the suspension and compliance states.
132 PxVehicleSuspensionForceUpdate(
133 suspensionParams[wheelId], suspensionForceParams[wheelId],
134 wheelRoadGeomStates[wheelId], suspensionStates[wheelId],
135 suspensionComplianceStates[wheelId], *rigidBodyState,
136 context.gravity, rigidBodyParams->mass,
137 suspensionForces[wheelId]);
138
139 addCustomSuspensionForce(dt,
140 suspensionParams[wheelId],
141 customSuspensionParams[wheelId],
142 wheelRoadGeomStates[wheelId], suspensionComplianceStates[wheelId], *rigidBodyState,
143 suspensionForces[wheelId], customSuspensionStates[wheelId]);
144 }
145
146 return true;
147 }
148};
149
150
151//
152//This class holds the parameters, state and logic needed to implement a vehicle that
153//is using a custom component for the suspension logic.
154//
155//See BaseVehicle for more details on the snippet code design.
156//
158 : public DirectDriveVehicle
160{
161public:
162 bool initialize(PxPhysics& physics, const PxCookingParams& params, PxMaterial& defaultMaterial, bool addPhysXBeginEndComponents = true);
163 virtual void destroy();
164
165 virtual void initComponentSequence(bool addPhysXBeginEndComponents);
166
167 virtual void getDataForCustomSuspensionComponent(
168 const PxVehicleAxleDescription*& axleDescription,
169 const PxVehicleRigidBodyParams*& rigidBodyParams,
170 const PxVehicleSuspensionStateCalculationParams*& suspensionStateCalculationParams,
171 const PxReal*& steerResponseStates,
172 const PxVehicleRigidBodyState*& rigidBodyState,
173 const PxVehicleWheelParams*& wheelParams,
174 const PxVehicleSuspensionParams*& suspensionParams,
175 const CustomSuspensionParams*& customSuspensionParams,
176 const PxVehicleSuspensionComplianceParams*& suspensionComplianceParams,
177 const PxVehicleSuspensionForceParams*& suspensionForceParams,
178 const PxVehicleRoadGeometryState*& wheelRoadGeomStates,
179 PxVehicleSuspensionState*& suspensionStates,
180 CustomSuspensionState*& customSuspensionStates,
181 PxVehicleSuspensionComplianceState*& suspensionComplianceStates,
182 PxVehicleSuspensionForce*& suspensionForces)
183 {
184 axleDescription = &mBaseParams.axleDescription;
185 rigidBodyParams = &mBaseParams.rigidBodyParams;
186 suspensionStateCalculationParams = &mBaseParams.suspensionStateCalculationParams;
187 steerResponseStates = mBaseState.steerCommandResponseStates;
188 rigidBodyState = &mBaseState.rigidBodyState;
189 wheelParams = mBaseParams.wheelParams;
190 suspensionParams = mBaseParams.suspensionParams;
191 customSuspensionParams = mCustomSuspensionParams;
192 suspensionComplianceParams = mBaseParams.suspensionComplianceParams;
193 suspensionForceParams = mBaseParams.suspensionForceParams;
194 wheelRoadGeomStates = mBaseState.roadGeomStates;
195 suspensionStates = mBaseState.suspensionStates;
196 customSuspensionStates = mCustomSuspensionStates;
197 suspensionComplianceStates = mBaseState.suspensionComplianceStates;
198 suspensionForces = mBaseState.suspensionForces;
199 }
200
201
202 //Parameters and states of the vehicle's custom suspension.
203 CustomSuspensionParams mCustomSuspensionParams[4];
204 CustomSuspensionState mCustomSuspensionStates[4];
205};
206
207}//namespace snippetvehicle2
Definition base.h:1940
Material class to represent a set of surface properties.
Definition PxMaterial.h:149
Abstract singleton factory class used for instancing objects in the Physics SDK.
Definition PxPhysics.h:91
Definition PxVehicleComponent.h:45
Definition CustomSuspension.h:67
virtual bool update(const PxReal dt, const PxVehicleSimulationContext &context)
Update function for a vehicle component.
Definition CustomSuspension.h:89
Definition CustomSuspension.h:160
Definition DirectDrivetrain.h:78
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
PX_FORCE_INLINE void * PxMemZero(void *dest, PxU32 count)
Sets the bytes of the provided buffer to zero.
Definition PxMemory.h:53
Structure describing parameters affecting mesh cooking.
Definition PxCooking.h:185
Definition PxVehicleParams.h:53
PxU32 wheelIdsInAxleOrder[PxVehicleLimits::eMAX_NB_WHEELS]
The list of all wheel ids on the vehicle.
Definition PxVehicleParams.h:149
PxU32 nbWheels
The number of wheels on the vehicle.
Definition PxVehicleParams.h:150
The properties of the rigid body.
Definition PxVehicleRigidBodyParams.h:50
PxReal mass
The mass of the rigid body.
Definition PxVehicleRigidBodyParams.h:57
Definition PxVehicleRigidBodyStates.h:47
Definition PxVehicleRoadGeometryState.h:46
Definition PxVehicleParams.h:672
Compliance describes how toe and camber angle and force application points are affected by suspension...
Definition PxVehicleSuspensionParams.h:155
The effect of suspension compliance on toe and camber angle and on the tire and suspension force appl...
Definition PxVehicleSuspensionStates.h:97
Suspension force is computed by converting suspenson state to suspension force under the assumption o...
Definition PxVehicleSuspensionParams.h:236
The force and torque for a single suspension to apply to the vehicle's rigid body.
Definition PxVehicleSuspensionStates.h:132
Definition PxVehicleSuspensionParams.h:51
Definition PxVehicleSuspensionParams.h:113
Definition PxVehicleSuspensionStates.h:53
Definition PxVehicleWheelParams.h:47
Definition CustomSuspension.h:43
Definition CustomSuspension.h:50