RavEngine
Loading...
Searching...
No Matches
PxVehiclePhysXConstraintStates.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
34#include "foundation/PxAssert.h"
35#include "foundation/PxTransform.h"
36#include "extensions/PxConstraintExt.h"
37
38#include "vehicle2/PxVehicleLimits.h"
39#include "vehicle2/tire/PxVehicleTireStates.h"
40
41#include "PxConstraint.h"
42#include "PxConstraintDesc.h"
43
44#if !PX_DOXYGEN
45namespace physx
46{
47
48class PxConstraint;
49
50namespace vehicle2
51{
52#endif
53
54
60{
61 enum Enum
62 {
63 eNB_DOFS_PER_PXCONSTRAINT = 12,
64 eNB_DOFS_PER_WHEEL = 3,
65 eNB_WHEELS_PER_PXCONSTRAINT = eNB_DOFS_PER_PXCONSTRAINT / eNB_DOFS_PER_WHEEL,
66 eNB_CONSTRAINTS_PER_VEHICLE = (PxVehicleLimits::eMAX_NB_WHEELS + (eNB_WHEELS_PER_PXCONSTRAINT - 1)) / (eNB_WHEELS_PER_PXCONSTRAINT)
67 };
68};
69
76{
80 bool tireActiveStatus[PxVehicleTireDirectionModes::eMAX_NB_PLANAR_DIRECTIONS];
84 PxVec3 tireLinears[PxVehicleTireDirectionModes::eMAX_NB_PLANAR_DIRECTIONS];
88 PxVec3 tireAngulars[PxVehicleTireDirectionModes::eMAX_NB_PLANAR_DIRECTIONS];
96 PxReal tireDamping[PxVehicleTireDirectionModes::eMAX_NB_PLANAR_DIRECTIONS];
97
98
126
127 PX_FORCE_INLINE void setToDefault()
128 {
130 }
131};
132
133//TAG:solverprepshader
134PX_FORCE_INLINE PxU32 vehicleConstraintSolverPrep
135(Px1DConstraint* constraints,
136 PxVec3p& body0WorldOffset,
137 PxU32 maxConstraints,
139 const void* constantBlock,
140 const PxTransform& bodyAToWorld,
141 const PxTransform& bodyBToWorld,
142 bool,
143 PxVec3p& cA2w, PxVec3p& cB2w)
144{
145 PX_UNUSED(maxConstraints);
146 PX_UNUSED(body0WorldOffset);
147 PX_UNUSED(bodyBToWorld);
148 PX_ASSERT(bodyAToWorld.isValid()); PX_ASSERT(bodyBToWorld.isValid());
149
150 const PxVehiclePhysXConstraintState* data = static_cast<const PxVehiclePhysXConstraintState*>(constantBlock);
151 PxU32 numActive = 0;
152
153 //KS - the TGS solver will use raXn to try to add to the angular part of the linear constraints.
154 //We overcome this by setting the ra and rb offsets to be 0.
155 cA2w = bodyAToWorld.p;
156 cB2w = bodyBToWorld.p;
157 // note: this is only needed for PxSolverType::eTGS and even then it should not have an effect as
158 // long as a constraint raises Px1DConstraintFlag::eANGULAR_CONSTRAINT
159
160 //Susp limit constraints.
161 for (PxU32 i = 0; i < PxVehiclePhysXConstraintLimits::eNB_WHEELS_PER_PXCONSTRAINT; i++)
162 {
163 if (data[i].suspActiveStatus)
164 {
165 // Going beyond max suspension compression should be treated similar to rigid body contacts.
166 // Thus setting up constraints that try to emulate such contacts.
167 //
168 // linear l = contact normal = n
169 // angular a = suspension force application offset x contact normal = cross(r, n)
170 //
171 // velocity at contact:
172 // vl: part from linear vehicle velocity v
173 // vl = dot(n, v) = dot(l, v)
174 //
175 // va: part from angular vehicle velocity w
176 // va = dot(n, cross(w, r)) = dot(w, cross(r, n)) = dot(w, a)
177 //
178 // ve: part from excess suspension compression
179 // ve = (geomError / dt) (note: geomError is expected to be negative here)
180 //
181 // velocity target vt = vl + va + ve
182 // => should become 0 by applying positive impulse along l. If vt is positive,
183 // nothing will happen as a negative impulse would have to be applied (but
184 // min impulse is set to 0). If vt is negative, a positive impulse will get
185 // applied to push vt towards 0.
186 //
187
188 Px1DConstraint& p = constraints[numActive];
189 p.linear0 = data[i].suspLinear;
190 p.angular0 = data[i].suspAngular;
191 p.geometricError = data[i].suspGeometricError;
192 p.linear1 = PxVec3(0);
193 p.angular1 = PxVec3(0);
194 p.minImpulse = 0;
195 p.maxImpulse = FLT_MAX;
196 p.velocityTarget = 0;
198
199 // note: this is only needed for PxSolverType::eTGS to not have the angular part
200 // be modified based on the linear part during substeps. Basically, it will
201 // disable the constraint re-projection etc. to emulate PxSolverType::ePGS.
203
204 if (data[i].restitution > 0.0f)
205 {
207 p.mods.bounce.restitution = data[i].restitution;
208 p.mods.bounce.velocityThreshold = -FLT_MAX;
209 }
210 numActive++;
211 }
212 }
213
214 //Sticky tire friction constraints.
215 for (PxU32 i = 0; i < PxVehiclePhysXConstraintLimits::eNB_WHEELS_PER_PXCONSTRAINT; i++)
216 {
217 if (data[i].tireActiveStatus[PxVehicleTireDirectionModes::eLONGITUDINAL])
218 {
219 Px1DConstraint& p = constraints[numActive];
220 p.linear0 = data[i].tireLinears[PxVehicleTireDirectionModes::eLONGITUDINAL];
221 p.angular0 = data[i].tireAngulars[PxVehicleTireDirectionModes::eLONGITUDINAL];
222 p.geometricError = 0.0f;
223 p.linear1 = PxVec3(0);
224 p.angular1 = PxVec3(0);
225 p.minImpulse = -FLT_MAX;
226 p.maxImpulse = FLT_MAX;
227 p.velocityTarget = 0.0f;
228 p.mods.spring.damping = data[i].tireDamping[PxVehicleTireDirectionModes::eLONGITUDINAL];
229 // note: no stiffness specified as this will have no effect with geometricError=0
231 p.flags |= Px1DConstraintFlag::eANGULAR_CONSTRAINT; // see explanation of same flag usage further above
232 numActive++;
233 }
234 }
235
236 //Sticky tire friction constraints.
237 for (PxU32 i = 0; i < PxVehiclePhysXConstraintLimits::eNB_WHEELS_PER_PXCONSTRAINT; i++)
238 {
239 if (data[i].tireActiveStatus[PxVehicleTireDirectionModes::eLATERAL])
240 {
241 Px1DConstraint& p = constraints[numActive];
242 p.linear0 = data[i].tireLinears[PxVehicleTireDirectionModes::eLATERAL];
243 p.angular0 = data[i].tireAngulars[PxVehicleTireDirectionModes::eLATERAL];
244 p.geometricError = 0.0f;
245 p.linear1 = PxVec3(0);
246 p.angular1 = PxVec3(0);
247 p.minImpulse = -FLT_MAX;
248 p.maxImpulse = FLT_MAX;
249 p.velocityTarget = 0.0f;
250 p.mods.spring.damping = data[i].tireDamping[PxVehicleTireDirectionModes::eLATERAL];
251 // note: no stiffness specified as this will have no effect with geometricError=0
253 p.flags |= Px1DConstraintFlag::eANGULAR_CONSTRAINT; // see explanation of same flag usage further above
254 numActive++;
255 }
256 }
257
258 return numActive;
259}
260
261PX_FORCE_INLINE void visualiseVehicleConstraint
262(PxConstraintVisualizer &viz,
263 const void* constantBlock,
264 const PxTransform& body0Transform,
265 const PxTransform& body1Transform,
266 PxU32 flags)
267{
268 PX_UNUSED(&viz);
269 PX_UNUSED(constantBlock);
270 PX_UNUSED(body0Transform);
271 PX_UNUSED(body1Transform);
272 PX_UNUSED(flags);
273 PX_ASSERT(body0Transform.isValid());
274 PX_ASSERT(body1Transform.isValid());
275}
276
278{
279public:
280
281 PxVehicleConstraintConnector() : mVehicleConstraintState(NULL) {}
282 PxVehicleConstraintConnector(PxVehiclePhysXConstraintState* vehicleConstraintState) : mVehicleConstraintState(vehicleConstraintState) {}
284
285 void setConstraintState(PxVehiclePhysXConstraintState* constraintState) { mVehicleConstraintState = constraintState; }
286
287 virtual void* prepareData() { return mVehicleConstraintState; }
288 virtual const void* getConstantBlock() const { return mVehicleConstraintState; }
289 virtual PxConstraintSolverPrep getPrep() const { return vehicleConstraintSolverPrep; }
290
291 //Is this necessary if physx no longer supports double-buffering?
292 virtual void onConstraintRelease() { }
293
294 //Can be empty functions.
295 virtual bool updatePvdProperties(physx::pvdsdk::PvdDataStream& pvdConnection, const PxConstraint* c, PxPvdUpdateType::Enum updateType) const
296 {
297 PX_UNUSED(pvdConnection);
298 PX_UNUSED(c);
299 PX_UNUSED(updateType);
300 return true;
301 }
302 virtual void updateOmniPvdProperties() const { }
303 virtual void onComShift(PxU32 actor) { PX_UNUSED(actor); }
304 virtual void onOriginShift(const PxVec3& shift) { PX_UNUSED(shift); }
305 virtual void* getExternalReference(PxU32& typeID) { typeID = PxConstraintExtIDs::eVEHICLE_JOINT; return this; }
306 virtual PxBase* getSerializable() { return NULL; }
307
308private:
309
310 PxVehiclePhysXConstraintState* mVehicleConstraintState;
311};
312
317{
323 PxVehiclePhysXConstraintState constraintStates[PxVehicleLimits::eMAX_NB_WHEELS];
324
330 PxConstraint* constraints[PxVehiclePhysXConstraintLimits::eNB_CONSTRAINTS_PER_VEHICLE];
331
335 PxVehicleConstraintConnector* constraintConnectors[PxVehiclePhysXConstraintLimits::eNB_CONSTRAINTS_PER_VEHICLE];
336
337 PX_FORCE_INLINE void setToDefault()
338 {
339 for (PxU32 i = 0; i < PxVehicleLimits::eMAX_NB_WHEELS; i++)
340 {
341 constraintStates[i].setToDefault();
342 }
343 for(PxU32 i = 0; i < PxVehiclePhysXConstraintLimits::eNB_CONSTRAINTS_PER_VEHICLE; i++)
344 {
345 constraints[i] = NULL;
346 constraintConnectors[i] = NULL;
347 }
348 }
349};
350
351#if !PX_DOXYGEN
352} // namespace vehicle2
353} // namespace physx
354#endif
355
Base class for objects that can be members of a PxCollection.
Definition PxBase.h:73
This class connects a custom constraint to the SDK.
Definition PxConstraintDesc.h:354
A plugin class for implementing constraints.
Definition PxConstraint.h:100
class representing a rigid euclidean transform as a quaternion and a vector
Definition PxTransform.h:49
PX_CUDA_CALLABLE bool isValid() const
returns true if finite and q is a unit quaternion
Definition PxTransform.h:148
A padded version of PxVec3, to safely load its data using SIMD.
Definition PxVec3.h:392
3 Element vector class.
Definition PxVec3.h:50
Definition PxPvdDataStream.h:241
Definition PxVehiclePhysXConstraintStates.h:278
virtual PxConstraintSolverPrep getPrep() const
Obtain the shader function pointer used to prep rows for this constraint.
Definition PxVehiclePhysXConstraintStates.h:289
virtual void updateOmniPvdProperties() const
this function is called by the SDK to update OmniPVD's view of it
Definition PxVehiclePhysXConstraintStates.h:302
virtual void onOriginShift(const PxVec3 &shift)
Origin shift callback.
Definition PxVehiclePhysXConstraintStates.h:304
virtual void onConstraintRelease()
Constraint release callback.
Definition PxVehiclePhysXConstraintStates.h:292
virtual void * prepareData()
Pre-simulation data preparation when the constraint is marked dirty, this function is called at the s...
Definition PxVehiclePhysXConstraintStates.h:287
virtual const void * getConstantBlock() const
Obtain the pointer to the constraint's constant data.
Definition PxVehiclePhysXConstraintStates.h:288
virtual bool updatePvdProperties(physx::pvdsdk::PvdDataStream &pvdConnection, const PxConstraint *c, PxPvdUpdateType::Enum updateType) const
this function is called by the SDK to update PVD's view of it
Definition PxVehiclePhysXConstraintStates.h:295
virtual void onComShift(PxU32 actor)
Center-of-mass shift callback.
Definition PxVehiclePhysXConstraintStates.h:303
virtual void * getExternalReference(PxU32 &typeID)
Fetches external data for a constraint.
Definition PxVehiclePhysXConstraintStates.h:305
virtual PxBase * getSerializable()
Obtain a reference to a PxBase interface if the constraint has one.
Definition PxVehiclePhysXConstraintStates.h:306
#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
PxU32(* PxConstraintSolverPrep)(Px1DConstraint *constraints, PxVec3p &bodyAWorldOffset, PxU32 maxConstraints, PxConstraintInvMassScale &invMassScale, const void *constantBlock, const PxTransform &bodyAToWorld, const PxTransform &bodyBToWorld, bool useExtendedLimits, PxVec3p &cAtW, PxVec3p &cBtW)
Solver constraint generation shader.
Definition PxConstraintDesc.h:223
PX_FORCE_INLINE void * PxMemZero(void *dest, PxU32 count)
Sets the bytes of the provided buffer to zero.
Definition PxMemory.h:53
@ eANGULAR_CONSTRAINT
whether this is an angular or linear constraint
Definition PxConstraintDesc.h:71
@ eRESTITUTION
whether the restitution model should be applied to generate the target velocity. Mutually exclusive w...
Definition PxConstraintDesc.h:67
@ eACCELERATION_SPRING
whether the constraint is a force or acceleration spring. Only valid if eSPRING is set.
Definition PxConstraintDesc.h:66
@ eSPRING
whether the constraint is a spring. Mutually exclusive with eRESTITUTION. If set, eKEEPBIAS is ignore...
Definition PxConstraintDesc.h:65
A one-dimensional constraint.
Definition PxConstraintDesc.h:134
Struct for specifying mass scaling for a pair of rigids.
Definition PxConstraintDesc.h:184
@ eINEQUALITY
inequality constraints with (0, PX_MAX_FLT) force limits
Definition PxConstraintDesc.h:94
Enum
Definition PxConstraintDesc.h:337
A description of the number of PxConstraintConnector instances per vehicle required to maintain suspe...
Definition PxVehiclePhysXConstraintStates.h:60
PxVehiclePhysXConstraintState is a data structure used to write constraint data to the internal state...
Definition PxVehiclePhysXConstraintStates.h:76
PxVec3 suspAngular
angular component of velocity jacobian in the world frame.
Definition PxVehiclePhysXConstraintStates.h:110
bool tireActiveStatus[PxVehicleTireDirectionModes::eMAX_NB_PLANAR_DIRECTIONS]
a boolean describing whether to trigger a low speed constraint along the tire longitudinal and latera...
Definition PxVehiclePhysXConstraintStates.h:80
PxVec3 suspLinear
linear component of velocity jacobian in the world frame.
Definition PxVehiclePhysXConstraintStates.h:106
bool suspActiveStatus
a boolean describing whether to trigger a suspension limit constraint.
Definition PxVehiclePhysXConstraintStates.h:102
PxReal tireDamping[PxVehicleTireDirectionModes::eMAX_NB_PLANAR_DIRECTIONS]
damping coefficient applied to the tire's longitudinal and lateral velocities.
Definition PxVehiclePhysXConstraintStates.h:96
PxVec3 tireAngulars[PxVehicleTireDirectionModes::eMAX_NB_PLANAR_DIRECTIONS]
angular component of velocity jacobian in world space for the tire's longitudinal and lateral directi...
Definition PxVehiclePhysXConstraintStates.h:88
PxReal restitution
restitution value of the restitution model used to generate a target velocity that will resolve the g...
Definition PxVehiclePhysXConstraintStates.h:125
PxReal suspGeometricError
the excess suspension compression to be resolved by the constraint that cannot be resolved due to the...
Definition PxVehiclePhysXConstraintStates.h:118
PxVec3 tireLinears[PxVehicleTireDirectionModes::eMAX_NB_PLANAR_DIRECTIONS]
linear component of velocity jacobian in world space for the tire's longitudinal and lateral directio...
Definition PxVehiclePhysXConstraintStates.h:84
A mapping between constraint state data and the associated PxConstraint instances.
Definition PxVehiclePhysXConstraintStates.h:317
PxVehicleConstraintConnector * constraintConnectors[PxVehiclePhysXConstraintLimits::eNB_CONSTRAINTS_PER_VEHICLE]
A constraint connector is necessary to connect each PxConstraint to a portion of the constraintStates...
Definition PxVehiclePhysXConstraintStates.h:335
PxVehiclePhysXConstraintState constraintStates[PxVehicleLimits::eMAX_NB_WHEELS]
PxVehiclePhysXConstraintComponent writes to the constraintStates array and a callback invoked by PxSc...
Definition PxVehiclePhysXConstraintStates.h:323
PxConstraint * constraints[PxVehiclePhysXConstraintLimits::eNB_CONSTRAINTS_PER_VEHICLE]
PxVehiclePhysXConstraintComponent writes to the constraintStates array and a callback invoked by PxSc...
Definition PxVehiclePhysXConstraintStates.h:330