1 | // |
2 | // Redistribution and use in source and binary forms, with or without |
3 | // modification, are permitted provided that the following conditions |
4 | // are met: |
5 | // * Redistributions of source code must retain the above copyright |
6 | // notice, this list of conditions and the following disclaimer. |
7 | // * Redistributions in binary form must reproduce the above copyright |
8 | // notice, this list of conditions and the following disclaimer in the |
9 | // documentation and/or other materials provided with the distribution. |
10 | // * Neither the name of NVIDIA CORPORATION nor the names of its |
11 | // contributors may be used to endorse or promote products derived |
12 | // from this software without specific prior written permission. |
13 | // |
14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY |
15 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
17 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
18 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
19 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
20 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
21 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
22 | // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
24 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | // |
26 | // Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved. |
27 | // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved. |
28 | // Copyright (c) 2001-2004 NovodeX AG. All rights reserved. |
29 | |
30 | #ifndef PX_SOLVER_DEFS_H |
31 | #define PX_SOLVER_DEFS_H |
32 | |
33 | #include "PxPhysXConfig.h" |
34 | #include "foundation/PxVec3.h" |
35 | #include "foundation/PxMat33.h" |
36 | #include "foundation/PxTransform.h" |
37 | #include "PxConstraintDesc.h" |
38 | #include "geomutils/GuContactPoint.h" |
39 | |
40 | #if PX_VC |
41 | #pragma warning(push) |
42 | #pragma warning(disable : 4324) // structure was padded due to alignment |
43 | #endif |
44 | |
45 | #if !PX_DOXYGEN |
46 | namespace physx |
47 | { |
48 | #endif |
49 | |
50 | struct PxTGSSolverBodyVel; |
51 | |
52 | namespace Dy |
53 | { |
54 | class ArticulationV; |
55 | typedef size_t ArticulationLinkHandle; |
56 | } |
57 | |
58 | namespace Sc |
59 | { |
60 | class ShapeInteraction; |
61 | } |
62 | |
63 | struct PxSolverBody |
64 | { |
65 | PX_ALIGN(16, PxVec3) linearVelocity; //!< Delta linear velocity computed by the solver |
66 | PxU16 maxSolverNormalProgress; //!< Progress counter used by constraint batching and parallel island solver. |
67 | PxU16 maxSolverFrictionProgress; //!< Progress counter used by constraint batching and parallel island solver. |
68 | |
69 | PxVec3 angularState; //!< Delta angular velocity state computed by the solver. |
70 | |
71 | PxU32 solverProgress; //!< Progress counter used by constraint batching and parallel island solver |
72 | |
73 | PxSolverBody() : linearVelocity(0.f), maxSolverNormalProgress(0), maxSolverFrictionProgress(0), angularState(0), solverProgress(0) |
74 | { |
75 | } |
76 | }; |
77 | |
78 | PX_COMPILE_TIME_ASSERT(sizeof(PxSolverBody) == 32); |
79 | |
80 | struct PxSolverBodyData |
81 | { |
82 | PX_ALIGN(16, PxVec3 linearVelocity); //!< 12 Pre-solver linear velocity |
83 | PxReal invMass; //!< 16 inverse mass |
84 | PxVec3 angularVelocity; //!< 28 Pre-solver angular velocity |
85 | PxReal reportThreshold; //!< 32 contact force threshold |
86 | PxMat33 sqrtInvInertia; //!< 68 inverse inertia in world space |
87 | PxReal penBiasClamp; //!< 72 the penetration bias clamp |
88 | PxU32 nodeIndex; //!< 76 the node idx of this solverBodyData. Used by solver to reference between solver bodies and island bodies. Not required by immediate mode |
89 | PxReal maxContactImpulse; //!< 80 the max contact impulse |
90 | PxTransform body2World; //!< 108 the body's transform |
91 | PxU16 lockFlags; //!< 110 lock flags |
92 | PxU16 pad; //!< 112 pad |
93 | |
94 | PX_FORCE_INLINE PxReal projectVelocity(const PxVec3& lin, const PxVec3& ang) const |
95 | { |
96 | return linearVelocity.dot(v: lin) + angularVelocity.dot(v: ang); |
97 | } |
98 | }; |
99 | |
100 | //---------------------------------- |
101 | /* |
102 | * A header that defines the size of a specific batch of constraints (of same type and without dependencies) |
103 | */ |
104 | struct |
105 | { |
106 | PxU32 ; //!< Start index for this batch |
107 | PxU16 ; //!< Number of constraints in this batch (range: 1-4) |
108 | PxU16 ; //!< The type of constraint this batch references |
109 | }; |
110 | |
111 | struct PxSolverConstraintDesc |
112 | { |
113 | static const PxU16 NO_LINK = 0xffff; |
114 | |
115 | enum ConstraintType |
116 | { |
117 | eCONTACT_CONSTRAINT, //!< Defines this pair is a contact constraint |
118 | eJOINT_CONSTRAINT //!< Defines this pair is a joint constraint |
119 | }; |
120 | |
121 | union |
122 | { |
123 | PxSolverBody* bodyA; //!< bodyA pointer |
124 | PxTGSSolverBodyVel* tgsBodyA; //!< bodyA pointer |
125 | Dy::ArticulationV* articulationA; //!< Articulation pointer for body A |
126 | |
127 | }; |
128 | |
129 | union |
130 | { |
131 | PxSolverBody* bodyB; //!< BodyB pointer |
132 | PxTGSSolverBodyVel* tgsBodyB; //!< BodyB pointer |
133 | Dy::ArticulationV* articulationB; //!< Articulation pointer for body B |
134 | }; |
135 | PxU16 linkIndexA; //!< Link index defining which link in Articulation A this constraint affects. If not an articulation, must be NO_LINK |
136 | PxU16 linkIndexB; //!< Link index defining which link in Articulation B this constraint affects. If not an articulation, must be NO_LINK. |
137 | PxU32 bodyADataIndex; //!< Body A's index into the SolverBodyData array |
138 | PxU32 bodyBDataIndex; //!< Body B's index into the SolverBodyData array |
139 | PxU16 writeBackLengthOver4; //!< writeBackLength/4, max writeback length is 256K, allows PxSolverConstraintDesc to fit in 32 bytes |
140 | PxU16 constraintLengthOver16; //!< constraintLength/16, max constraint length is 1MB, allows PxSolverConstraintDesc to fit in 32 bytes |
141 | PxU8* constraint; //!< Pointer to the constraint rows to be solved |
142 | void* writeBack; //!< Pointer to the writeback structure results for this given constraint are to be written to |
143 | }; |
144 | |
145 | struct PxSolverConstraintPrepDescBase |
146 | { |
147 | enum BodyState |
148 | { |
149 | eDYNAMIC_BODY = 1 << 0, |
150 | eSTATIC_BODY = 1 << 1, |
151 | eKINEMATIC_BODY = 1 << 2, |
152 | eARTICULATION = 1 << 3 |
153 | }; |
154 | |
155 | PxConstraintInvMassScale invMassScales; //!< In: The local mass scaling for this pair. |
156 | |
157 | PxSolverConstraintDesc* desc; //!< Output: The PxSolverConstraintDesc filled in by contact prep |
158 | |
159 | const PxSolverBody* body0; //!< In: The first body. Stores velocity information. Unused unless contact involves articulations. |
160 | const PxSolverBody* body1; //!< In: The second body. Stores velocity information. Unused unless contact involves articulations. |
161 | |
162 | const PxSolverBodyData* data0; //!< In: The first PxSolverBodyData. Stores mass and miscellaneous information for the first body. |
163 | const PxSolverBodyData* data1; //!< In: The second PxSolverBodyData. Stores mass and miscellaneous information for the second body |
164 | |
165 | PxTransform bodyFrame0; //!< In: The world-space transform of the first body. |
166 | PxTransform bodyFrame1; //!< In: The world-space transform of the second body. |
167 | |
168 | BodyState bodyState0; //!< In: Defines what kind of actor the first body is |
169 | BodyState bodyState1; //!< In: Defines what kind of actor the second body is |
170 | }; |
171 | |
172 | struct PxSolverConstraintPrepDesc : public PxSolverConstraintPrepDescBase |
173 | { |
174 | PX_ALIGN(16, Px1DConstraint* rows); //!< The start of the constraint rows |
175 | PxU32 numRows; //!< The number of rows |
176 | |
177 | PxReal linBreakForce, angBreakForce; //!< Break forces |
178 | PxReal minResponseThreshold; //!< The minimum response threshold |
179 | void* writeback; //!< Pointer to constraint writeback structure. Reports back joint breaking. If not required, set to NULL. |
180 | bool disablePreprocessing; //!< Disable joint pre-processing. Pre-processing can improve stability but under certain circumstances, e.g. when some invInertia rows are zero/almost zero, can cause instabilities. |
181 | bool improvedSlerp; //!< Use improved slerp model |
182 | bool driveLimitsAreForces; //!< Indicates whether drive limits are forces |
183 | bool extendedLimits; //!< Indicates whether we want to use extended limits |
184 | |
185 | PxVec3 body0WorldOffset; //!< Body0 world offset |
186 | }; |
187 | |
188 | struct PxSolverContactDesc : public PxSolverConstraintPrepDescBase |
189 | { |
190 | PX_ALIGN(16, Sc::ShapeInteraction* shapeInteraction); //!< Pointer to share interaction. Used for force threshold reports in solver. Set to NULL if using immediate mode. |
191 | Gu::ContactPoint* contacts; //!< The start of the contacts for this pair |
192 | PxU32 numContacts; //!< The total number of contacs this pair references. |
193 | |
194 | bool hasMaxImpulse; //!< Defines whether this pairs has maxImpulses clamping enabled |
195 | bool disableStrongFriction; //!< Defines whether this pair disables strong friction (sticky friction correlation) |
196 | bool hasForceThresholds; //!< Defines whether this pair requires force thresholds |
197 | |
198 | PxReal restDistance; //!< A distance at which the solver should aim to hold the bodies separated. Default is 0 |
199 | PxReal maxCCDSeparation; //!< A distance used to configure speculative CCD behavior. Default is PX_MAX_F32. Set internally in PhysX for bodies with eENABLE_SPECULATIVE_CCD on. Do not set directly! |
200 | |
201 | PxU8* frictionPtr; //!< InOut: Friction patch correlation data. Set each frame by solver. Can be retained for improved behaviour or discarded each frame. |
202 | PxU8 frictionCount; //!< The total number of friction patches in this pair |
203 | |
204 | PxReal* contactForces; //!< Out: A buffer for the solver to write applied contact forces to. |
205 | |
206 | PxU32 startFrictionPatchIndex; //!< Start index of friction patch in the correlation buffer. Set by friction correlation |
207 | PxU32 numFrictionPatches; //!< Total number of friction patches in this pair. Set by friction correlation |
208 | |
209 | PxU32 startContactPatchIndex; //!< The start index of this pair's contact patches in the correlation buffer. For internal use only |
210 | PxU16 numContactPatches; //!< Total number of contact patches. |
211 | PxU16 axisConstraintCount; //!< Axis constraint count. Defines how many constraint rows this pair has produced. Useful for statistical purposes. |
212 | |
213 | PxU8 pad[16 - sizeof(void*)]; |
214 | }; |
215 | |
216 | class PxConstraintAllocator |
217 | { |
218 | public: |
219 | /** |
220 | \brief Allocates constraint data. It is the application's responsibility to release this memory after PxSolveConstraints has completed. |
221 | \param[in] byteSize Allocation size in bytes |
222 | \return the allocated memory. This address must be 16-byte aligned. |
223 | */ |
224 | virtual PxU8* reserveConstraintData(const PxU32 byteSize) = 0; |
225 | |
226 | /** |
227 | \brief Allocates friction data. Friction data can be retained by the application for a given pair and provided as an input to PxSolverContactDesc to improve simulation stability. |
228 | It is the application's responsibility to release this memory. If this memory is released, the application should ensure it does not pass pointers to this memory to PxSolverContactDesc. |
229 | \param[in] byteSize Allocation size in bytes |
230 | \return the allocated memory. This address must be 4-byte aligned. |
231 | */ |
232 | virtual PxU8* reserveFrictionData(const PxU32 byteSize) = 0; |
233 | |
234 | virtual ~PxConstraintAllocator() {} |
235 | }; |
236 | |
237 | struct PxArticulationAxis |
238 | { |
239 | enum Enum |
240 | { |
241 | eTWIST = 0, |
242 | eSWING1 = 1, |
243 | eSWING2 = 2, |
244 | eX = 3, |
245 | eY = 4, |
246 | eZ = 5, |
247 | eCOUNT = 6 |
248 | }; |
249 | }; |
250 | |
251 | PX_FLAGS_OPERATORS(PxArticulationAxis::Enum, PxU8) |
252 | |
253 | struct PxArticulationMotion |
254 | { |
255 | enum Enum |
256 | { |
257 | eLOCKED = 0, |
258 | eLIMITED = 1, |
259 | eFREE = 2 |
260 | }; |
261 | }; |
262 | |
263 | typedef PxFlags<PxArticulationMotion::Enum, PxU8> PxArticulationMotions; |
264 | PX_FLAGS_OPERATORS(PxArticulationMotion::Enum, PxU8) |
265 | |
266 | struct PxArticulationJointType |
267 | { |
268 | enum Enum |
269 | { |
270 | eFIX = 0, |
271 | ePRISMATIC = 1, |
272 | eREVOLUTE = 2, |
273 | eSPHERICAL = 3, |
274 | eUNDEFINED = 4 |
275 | }; |
276 | }; |
277 | |
278 | struct PxArticulationFlag |
279 | { |
280 | enum Enum |
281 | { |
282 | eFIX_BASE = (1 << 0), |
283 | eDRIVE_LIMITS_ARE_FORCES = (1<<1) |
284 | }; |
285 | }; |
286 | |
287 | typedef PxFlags<PxArticulationFlag::Enum, PxU8> PxArticulationFlags; |
288 | PX_FLAGS_OPERATORS(PxArticulationFlag::Enum, PxU8) |
289 | |
290 | struct PxArticulationDriveType |
291 | { |
292 | enum Enum |
293 | { |
294 | eFORCE = 0, |
295 | eACCELERATION = 1, |
296 | eTARGET = 2, |
297 | eVELOCITY = 3, |
298 | eNONE = 4 |
299 | }; |
300 | }; |
301 | |
302 | struct PxArticulationLimit |
303 | { |
304 | PxReal low, high; |
305 | }; |
306 | |
307 | struct PxArticulationDrive |
308 | { |
309 | PxReal stiffness, damping, maxForce; |
310 | PxArticulationDriveType::Enum driveType; |
311 | }; |
312 | |
313 | |
314 | struct PxTGSSolverBodyVel |
315 | { |
316 | PX_ALIGN(16, PxVec3) linearVelocity; //12 |
317 | PxU16 nbStaticInteractions; //14 Used to accumulate the number of static interactions |
318 | PxU16 maxDynamicPartition; //16 Used to accumualte the max partition of dynamic interactions |
319 | PxVec3 angularVelocity; //28 |
320 | PxU32 partitionMask; //32 Used in partitioning as a bit-field |
321 | PxVec3 deltaAngDt; //44 |
322 | PxReal maxAngVel; //48 |
323 | PxVec3 deltaLinDt; //60 |
324 | PxU16 lockFlags; //62 |
325 | bool isKinematic; //63 |
326 | PxU8 pad; //64 |
327 | |
328 | PX_FORCE_INLINE PxReal projectVelocity(const PxVec3& lin, const PxVec3& ang) const |
329 | { |
330 | return linearVelocity.dot(v: lin) + angularVelocity.dot(v: ang); |
331 | } |
332 | |
333 | }; |
334 | |
335 | //Needed only by prep, integration and 1D constraints |
336 | struct PxTGSSolverBodyTxInertia |
337 | { |
338 | PxTransform deltaBody2World; |
339 | PxMat33 sqrtInvInertia; |
340 | }; |
341 | |
342 | struct PxTGSSolverBodyData |
343 | { |
344 | PX_ALIGN(16, PxVec3) originalLinearVelocity; |
345 | PxReal maxContactImpulse; |
346 | PxVec3 originalAngularVelocity; |
347 | PxReal penBiasClamp; |
348 | |
349 | PxReal invMass; |
350 | PxU32 nodeIndex; |
351 | PxReal reportThreshold; |
352 | PxU32 pad; |
353 | |
354 | PxReal projectVelocity(const PxVec3& linear, const PxVec3& angular) const |
355 | { |
356 | return originalLinearVelocity.dot(v: linear) + originalAngularVelocity.dot(v: angular); |
357 | } |
358 | }; |
359 | |
360 | struct PxTGSSolverConstraintPrepDescBase |
361 | { |
362 | PxConstraintInvMassScale invMassScales; //!< In: The local mass scaling for this pair. |
363 | |
364 | PxSolverConstraintDesc* desc; //!< Output: The PxSolverConstraintDesc filled in by contact prep |
365 | |
366 | const PxTGSSolverBodyVel* body0; //!< In: The first body. Stores velocity information. Unused unless contact involves articulations. |
367 | const PxTGSSolverBodyVel *body1; //!< In: The second body. Stores velocity information. Unused unless contact involves articulations. |
368 | |
369 | const PxTGSSolverBodyTxInertia* body0TxI; |
370 | const PxTGSSolverBodyTxInertia* body1TxI; |
371 | |
372 | const PxTGSSolverBodyData* bodyData0; |
373 | const PxTGSSolverBodyData* bodyData1; |
374 | |
375 | PxTransform bodyFrame0; //!< In: The world-space transform of the first body. |
376 | PxTransform bodyFrame1; //!< In: The world-space transform of the second body. |
377 | |
378 | PxSolverContactDesc::BodyState bodyState0; //!< In: Defines what kind of actor the first body is |
379 | PxSolverContactDesc::BodyState bodyState1; //!< In: Defines what kind of actor the second body is |
380 | |
381 | }; |
382 | |
383 | struct PxTGSSolverConstraintPrepDesc : public PxTGSSolverConstraintPrepDescBase |
384 | { |
385 | Px1DConstraint* rows; //!< The start of the constraint rows |
386 | PxU32 numRows; //!< The number of rows |
387 | |
388 | PxReal linBreakForce, angBreakForce; //!< Break forces |
389 | PxReal minResponseThreshold; //!< The minimum response threshold |
390 | void* writeback; //!< Pointer to constraint writeback structure. Reports back joint breaking. If not required, set to NULL. |
391 | bool disablePreprocessing; //!< Disable joint pre-processing. Pre-processing can improve stability but under certain circumstances, e.g. when some invInertia rows are zero/almost zero, can cause instabilities. |
392 | bool improvedSlerp; //!< Use improved slerp model |
393 | bool driveLimitsAreForces; //!< Indicates whether drive limits are forces |
394 | bool extendedLimits; //!< Indiciates whether extended limits are used |
395 | |
396 | PxVec3 body0WorldOffset; //!< Body0 world offset |
397 | PxVec3 cA2w; //!< Location of anchor point A in world space |
398 | PxVec3 cB2w; //!< Location of anchor point B in world space |
399 | }; |
400 | |
401 | |
402 | struct PxTGSSolverContactDesc : public PxTGSSolverConstraintPrepDescBase |
403 | { |
404 | |
405 | Sc::ShapeInteraction* shapeInteraction; //!< Pointer to share interaction. Used for force threshold reports in solver. Set to NULL if using immediate mode. |
406 | Gu::ContactPoint* contacts; //!< The start of the contacts for this pair |
407 | PxU32 numContacts; //!< The total number of contacs this pair references. |
408 | |
409 | bool hasMaxImpulse; //!< Defines whether this pairs has maxImpulses clamping enabled |
410 | bool disableStrongFriction; //!< Defines whether this pair disables strong friction (sticky friction correlation) |
411 | bool hasForceThresholds; //!< Defines whether this pair requires force thresholds |
412 | |
413 | PxReal restDistance; //!< A distance at which the solver should aim to hold the bodies separated. Default is 0 |
414 | PxReal maxCCDSeparation; //!< A distance used to configure speculative CCD behavior. Default is PX_MAX_F32. Set internally in PhysX for bodies with eENABLE_SPECULATIVE_CCD on. Do not set directly! |
415 | |
416 | PxU8* frictionPtr; //!< InOut: Friction patch correlation data. Set each frame by solver. Can be retained for improved behaviour or discarded each frame. |
417 | PxU8 frictionCount; //!< The total number of friction patches in this pair |
418 | |
419 | PxReal* contactForces; //!< Out: A buffer for the solver to write applied contact forces to. |
420 | |
421 | PxU32 startFrictionPatchIndex; //!< Start index of friction patch in the correlation buffer. Set by friction correlation |
422 | PxU32 numFrictionPatches; //!< Total number of friction patches in this pair. Set by friction correlation |
423 | |
424 | PxU32 startContactPatchIndex; //!< The start index of this pair's contact patches in the correlation buffer. For internal use only |
425 | PxU16 numContactPatches; //!< Total number of contact patches. |
426 | PxU16 axisConstraintCount; //!< Axis constraint count. Defines how many constraint rows this pair has produced. Useful for statistical purposes. |
427 | |
428 | PxReal maxImpulse; |
429 | |
430 | PxReal torsionalPatchRadius; |
431 | PxReal minTorsionalPatchRadius; |
432 | }; |
433 | |
434 | #if !PX_DOXYGEN |
435 | } |
436 | #endif |
437 | |
438 | #if PX_VC |
439 | #pragma warning(pop) |
440 | #endif |
441 | |
442 | #endif |
443 | |
444 | |