29#ifndef EXT_CONSTRAINT_HELPER_H
30#define EXT_CONSTRAINT_HELPER_H
32#include "foundation/PxAssert.h"
33#include "foundation/PxTransform.h"
34#include "foundation/PxMat33.h"
35#include "foundation/PxSIMDHelpers.h"
36#include "extensions/PxD6Joint.h"
37#include "ExtJointData.h"
38#include "foundation/PxVecMath.h"
46 PX_INLINE void computeJointFrames(PxTransform& cA2w, PxTransform& cB2w,
const JointData& data,
const PxTransform& bA2w,
const PxTransform& bB2w)
48 PX_ASSERT(bA2w.isValid() && bB2w.isValid());
50 cA2w = bA2w.transform(data.c2b[0]);
51 cB2w = bB2w.transform(data.c2b[1]);
53 PX_ASSERT(cA2w.isValid() && cB2w.isValid());
56 PX_INLINE void computeDerived(
const JointData& data,
57 const PxTransform& bA2w,
const PxTransform& bB2w,
58 PxTransform& cA2w, PxTransform& cB2w, PxTransform& cB2cA,
59 bool useShortestPath=
true)
61 computeJointFrames(cA2w, cB2w, data, bA2w, bB2w);
65 if(cA2w.q.dot(cB2w.q)<0.0f)
69 cB2cA = cA2w.transformInv(cB2w);
70 PX_ASSERT(cB2cA.isValid());
73 PX_INLINE PxVec3 truncateLinear(
const PxVec3& in, PxReal tolerance,
bool& truncated)
75 const PxReal m = in.magnitudeSquared();
76 truncated = m>tolerance * tolerance;
77 return truncated ? in *
PxRecipSqrt(m) * tolerance : in;
80 PX_INLINE PxQuat truncateAngular(
const PxQuat& in, PxReal sinHalfTol, PxReal cosHalfTol,
bool& truncated)
84 if(sinHalfTol > 0.9999f)
87 const PxQuat q = in.w>=0.0f ? in : -in;
89 const PxVec3 im = q.getImaginaryPart();
90 const PxReal m = im.magnitudeSquared();
91 truncated = m>sinHalfTol*sinHalfTol;
95 const PxVec3 outV = im * sinHalfTol *
PxRecipSqrt(m);
96 return PxQuat(outV.x, outV.y, outV.z, cosHalfTol);
99 PX_FORCE_INLINE void projectTransforms(PxTransform& bA2w, PxTransform& bB2w,
100 const PxTransform& cA2w,
const PxTransform& cB2w,
101 const PxTransform& cB2cA,
const JointData& data,
bool projectToA)
103 PX_ASSERT(cB2cA.isValid());
115 bB2w = cA2w.transform(cB2cA.transform(data.c2b[1].getInverse()));
120 bA2w = cB2w.transform(cB2cA.transformInv(data.c2b[0].getInverse()));
124 PX_ASSERT(bA2w.isValid());
125 PX_ASSERT(bB2w.isValid());
128 PX_INLINE void computeJacobianAxes(PxVec3 row[3],
const PxQuat& qa,
const PxQuat& qb)
134 const PxReal wa = qa.w, wb = qb.w;
135 const PxVec3 va(qa.x,qa.y,qa.z), vb(qb.x,qb.y,qb.z);
137 const PxVec3 c = vb*wa + va*wb;
138 const PxReal d0 = wa*wb;
139 const PxReal d1 = va.dot(vb);
140 const PxReal d = d0 - d1;
142 row[0] = (va * vb.x + vb * va.x + PxVec3(d, c.z, -c.y)) * 0.5f;
143 row[1] = (va * vb.y + vb * va.y + PxVec3(-c.z, d, c.x)) * 0.5f;
144 row[2] = (va * vb.z + vb * va.z + PxVec3(c.y, -c.x, d)) * 0.5f;
146 if((d0 + d1) != 0.0f)
150 row[0].x += PX_EPS_F32;
151 row[1].y += PX_EPS_F32;
152 row[2].z += PX_EPS_F32;
158 c->solveHint = PxU16(hint);
160 c->angular0 = ra.cross(axis);
162 c->angular1 = rb.cross(axis);
163 c->geometricError = posErr;
164 PX_ASSERT(c->linear0.isFinite());
165 PX_ASSERT(c->linear1.isFinite());
166 PX_ASSERT(c->angular0.isFinite());
167 PX_ASSERT(c->angular1.isFinite());
173 c->solveHint = PxU16(hint);
174 c->linear0 = PxVec3(0.0f);
176 c->linear1 = PxVec3(0.0f);
178 c->geometricError = posErr;
192 : mConstraints(c), mCurrent(c), mRa(ra), mRb(rb) {}
197 : mConstraints(c), mCurrent(c)
201 V4StoreA(V4LoadA(&data.invMassScale.linear0), &invMassScale.
linear0);
203 computeJointFrames(cA2w, cB2w, data, bA2w, bB2w);
205 const PxVec3 ra = cB2w.p - bA2w.p;
206 body0WorldOffset = ra;
209 mRb = cB2w.p - bB2w.p;
236 if(ordinate + pad > limitValue)
245 if(ordinate + pad > limitValue)
256 PX_ASSERT(lower<upper);
260 if(angle < lower+pad)
261 angularLimit(-axis, -(lower - angle), limit);
262 if(angle > upper-pad)
263 angularLimit(axis, (upper - angle), limit);
275 addDrive(angular(axis, error, hint), velTarget, drive);
278 PX_FORCE_INLINE PxU32 getCount()
const {
return PxU32(mCurrent - mConstraints); }
292 if(lin&1) errorVector -= axes.column0 * cB2cAp.x;
293 if(lin&2) errorVector -= axes.column1 * cB2cAp.y;
294 if(lin&4) errorVector -= axes.column2 * cB2cAp.z;
305 const PxQuat qB2qA = qA.getConjugate() * qB;
308 computeJacobianAxes(row, qA, qB);
331 return _linear(axis, mRa, mRb, posErr, hint, mCurrent++);
336 return _angular(axis, posErr, hint, mCurrent++);
346 c->mods.spring.stiffness = limit.
stiffness;
347 c->mods.spring.damping = limit.
damping;
354 if(c->geometricError>0.0f)
361 c->minImpulse = 0.0f;
366 c->velocityTarget = velTarget;
372 c->mods.spring.stiffness = drive.
stiffness;
373 c->mods.spring.damping = drive.
damping;
378 PX_ASSERT(c->linear0.isFinite());
379 PX_ASSERT(c->angular0.isFinite());
Definition ExtConstraintHelper.h:184
parameters for configuring the drive model of a PxD6Joint
Definition PxD6Joint.h:145
PxReal forceLimit
the force limit of the drive - may be an impulse or a force depending on PxConstraintFlag::eDRIVE_LIM...
Definition PxD6Joint.h:154
PxD6JointDriveFlags flags
the joint drive flags
Definition PxD6Joint.h:155
Describes the parameters for a joint limit.
Definition PxJointLimit.h:52
PxReal bounceThreshold
Definition PxJointLimit.h:84
PxReal restitution
Controls the amount of bounce when the joint hits a limit.
Definition PxJointLimit.h:79
PxReal damping
if spring is greater than zero, this is the damping of the limit spring
Definition PxJointLimit.h:100
PxReal stiffness
if greater than zero, the limit is soft, i.e. a spring pulls the joint back to the limit
Definition PxJointLimit.h:92
PxReal contactDistance_deprecated
The distance inside the limit value at which the limit will be considered to be active by the solver....
Definition PxJointLimit.h:119
A padded version of PxMat33, to safely load its data using SIMD.
Definition PxSIMDHelpers.h:41
This is a quaternion class. For more information on quaternion mathematics consult a mathematics sour...
Definition PxQuat.h:50
float x
Definition PxQuat.h:395
PxReal stiffness
the spring strength of the drive: that is, the force proportional to the position error
Definition PxJoint.h:394
PxReal damping
the damping strength of the drive: that is, the force proportional to the velocity error
Definition PxJoint.h:395
A padded version of PxVec3, to safely load its data using SIMD.
Definition PxVec3.h:392
3 Element vector class.
Definition PxVec3.h:50
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
#define PX_INLINE
Definition PxPreprocessor.h:320
GLM_FUNC_DECL genType::row_type row(genType const &m, length_t index)
Definition matrix_access.inl:23
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxRecipSqrt(float a)
reciprocal square root.
Definition PxMath.h:158
Definition ExtJointData.h:39
@ eOUTPUT_FORCE
whether to accumulate the force value from this constraint in the force total that is reported for th...
Definition PxConstraintDesc.h:69
@ 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
@ eKEEPBIAS
whether to keep the error term when solving for velocity. Ignored if restitution generates bounce,...
Definition PxConstraintDesc.h:68
@ 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
@ eHAS_DRIVE_LIMIT
whether the constraint has a drive force limit (which will be scaled by dt unless PxConstraintFlag::e...
Definition PxConstraintDesc.h:70
A one-dimensional constraint.
Definition PxConstraintDesc.h:134
Struct for specifying mass scaling for a pair of rigids.
Definition PxConstraintDesc.h:184
PxReal linear0
multiplier for inverse mass of body0
Definition PxConstraintDesc.h:192
Enum
Definition PxConstraintDesc.h:85
@ eNONE
no special properties
Definition PxConstraintDesc.h:86
@ eINEQUALITY
inequality constraints with (0, PX_MAX_FLT) force limits
Definition PxConstraintDesc.h:94
@ eEQUALITY
equality constraints with no force limit and no velocity target
Definition PxConstraintDesc.h:93
@ eACCELERATION
drive spring is for the acceleration at the joint (rather than the force)
Definition PxD6Joint.h:133