29#ifndef DY_CONTACT_PREP_SHARED_H
30#define DY_CONTACT_PREP_SHARED_H
32#include "foundation/PxPreprocessor.h"
33#include "PxSceneDesc.h"
34#include "foundation/PxVecMath.h"
35#include "DyContactPrep.h"
36#include "DyCorrelationBuffer.h"
37#include "DyArticulationContactPrep.h"
38#include "PxsContactManager.h"
39#include "PxsContactManagerState.h"
45template<
class PxSolverContactDescT>
46PX_FORCE_INLINE Sc::ShapeInteraction* getInteraction(
const PxSolverContactDescT& desc)
48 return reinterpret_cast<Sc::ShapeInteraction*
>(desc.shapeInteraction);
52 const PxVec3& localAnchor0,
const PxVec3& localAnchor1,
53 const PxVec3& axis,
float correlDist)
55 const PxVec3 body0PatchPoint1 = body1ToBody0.transform(localAnchor1);
57 return PxAbs((localAnchor0 - body0PatchPoint1).dot(axis))<correlDist;
60PX_FORCE_INLINE bool isSeparated(
const FrictionPatch& patch,
const PxTransform& body1ToBody0,
const PxReal correlationDistance)
62 PX_ASSERT(patch.anchorCount <= 2);
63 for(PxU32 a = 0; a < patch.anchorCount; ++a)
65 if(!pointsAreClose(body1ToBody0, patch.body0Anchors[a], patch.body1Anchors[a], patch.body0Normal, correlationDistance))
72inline bool getFrictionPatches(CorrelationBuffer& c,
73 const PxU8* frictionCookie,
74 PxU32 frictionPatchCount,
75 const PxTransform& bodyFrame0,
76 const PxTransform& bodyFrame1,
77 PxReal correlationDistance)
79 PX_UNUSED(correlationDistance);
80 if(frictionCookie == NULL || frictionPatchCount == 0)
84 const FrictionPatch* patches =
reinterpret_cast<const FrictionPatch*
>(frictionCookie);
87 bool evaluated =
false;
88 PxTransform body1ToBody0;
90 while(frictionPatchCount--)
93 const FrictionPatch& patch = *patches++;
94 PX_ASSERT (patch.broken == 0 || patch.broken == 1);
101 PX_ASSERT(patch.anchorCount <= 2);
106 body1ToBody0 = bodyFrame0.transformInv(bodyFrame1);
111 if(patch.body0Normal.dot(body1ToBody0.rotate(patch.body1Normal)) > PXC_SAME_NORMAL)
113 if(!isSeparated(patch, body1ToBody0, correlationDistance))
115 if(c.frictionPatchCount == CorrelationBuffer::MAX_FRICTION_PATCHES)
118 c.contactID[c.frictionPatchCount][0] = 0xffff;
119 c.contactID[c.frictionPatchCount][1] = 0xffff;
121 c.frictionPatchWorldNormal[c.frictionPatchCount] = bodyFrame0.rotate(patch.body0Normal);
122 c.frictionPatchContactCounts[c.frictionPatchCount] = 0;
123 c.patchBounds[c.frictionPatchCount].setEmpty();
124 c.correlationListHeads[c.frictionPatchCount] = CorrelationBuffer::LIST_END;
125 PxMemCopy(&c.frictionPatches[c.frictionPatchCount++], &patch,
sizeof(FrictionPatch));
135PX_FORCE_INLINE PxU32 extractContacts(PxContactBuffer& buffer, PxsContactManagerOutput& npOutput,
bool& hasMaxImpulse,
bool& hasTargetVelocity,
136 PxReal& invMassScale0, PxReal& invMassScale1, PxReal& invInertiaScale0, PxReal& invInertiaScale1, PxReal defaultMaxImpulse)
138 PxContactStreamIterator iter(npOutput.contactPatches, npOutput.contactPoints, npOutput.getInternalFaceIndice(), npOutput.nbPatches, npOutput.nbContacts);
140 PxU32 numContacts = buffer.count, origContactCount = buffer.count;
141 if(!iter.forceNoResponse)
143 invMassScale0 = iter.getInvMassScale0();
144 invMassScale1 = iter.getInvMassScale1();
145 invInertiaScale0 = iter.getInvInertiaScale0();
146 invInertiaScale1 = iter.getInvInertiaScale1();
150 while(iter.hasNextPatch())
153 while(iter.hasNextContact())
158 PxReal maxImpulse = hasMaxImpulse ? iter.getMaxImpulse() : defaultMaxImpulse;
159 if(maxImpulse != 0.f)
161 PX_ASSERT(numContacts < PxContactBuffer::MAX_CONTACTS);
162 buffer.contacts[numContacts].normal = iter.getContactNormal();
163 PX_ASSERT(
PxAbs(buffer.contacts[numContacts].normal.magnitude() - 1) < 1e-3f);
164 buffer.contacts[numContacts].point = iter.getContactPoint();
165 buffer.contacts[numContacts].separation = iter.getSeparation();
167 buffer.contacts[numContacts].materialFlags = PxU8(iter.getMaterialFlags());
168 buffer.contacts[numContacts].maxImpulse = maxImpulse;
169 buffer.contacts[numContacts].staticFriction = iter.getStaticFriction();
170 buffer.contacts[numContacts].dynamicFriction = iter.getDynamicFriction();
171 buffer.contacts[numContacts].restitution = iter.getRestitution();
172 buffer.contacts[numContacts].damping = iter.getDamping();
173 const PxVec3& targetVel = iter.getTargetVel();
174 buffer.contacts[numContacts].targetVel = targetVel;
180 const PxU32 contactCount = numContacts - origContactCount;
181 buffer.count = numContacts;
194 PxU32 newPatch = startPatch, newContact = 0;
196 while(newPatch != CorrelationBuffer::LIST_END && newContact == buffer.contactPatches[newPatch].count)
198 newPatch = buffer.contactPatches[newPatch].next;
202 currPatch = newPatch;
203 currContact = newContact;
209 return (currPatch != CorrelationBuffer::LIST_END && currContact < buffer.contactPatches[currPatch].count);
212 inline void nextContact(PxU32& patch, PxU32& contact)
214 PX_ASSERT(currPatch != CorrelationBuffer::LIST_END);
215 PX_ASSERT(currContact < buffer.contactPatches[currPatch].count);
218 contact = currContact;
219 PxU32 newPatch = currPatch, newContact = currContact + 1;
221 while(newPatch != CorrelationBuffer::LIST_END && newContact == buffer.contactPatches[newPatch].count)
223 newPatch = buffer.contactPatches[newPatch].next;
227 currPatch = newPatch;
228 currContact = newContact;
237 PX_FORCE_INLINE void constructContactConstraint(
const Mat33V& invSqrtInertia0,
const Mat33V& invSqrtInertia1,
const FloatVArg invMassNorLenSq0,
238 const FloatVArg invMassNorLenSq1,
const FloatVArg angD0,
const FloatVArg angD1,
const Vec3VArg bodyFrame0p,
const Vec3VArg bodyFrame1p,
239 const Vec3VArg normal,
const FloatVArg norVel,
const VecCrossV& norCross,
const Vec3VArg angVel0,
const Vec3VArg angVel1,
240 const FloatVArg invDt,
const FloatVArg invDtp8,
const FloatVArg dt,
const FloatVArg restDistance,
const FloatVArg maxPenBias,
const FloatVArg restitution,
242 const FloatVArg ccdMaxSeparation,
const Vec3VArg solverOffsetSlop,
const FloatVArg damping)
244 const FloatV zero = FZero();
245 const Vec3V point = V3LoadA(contact.point);
246 const FloatV separation = FLoad(contact.separation);
248 const FloatV cTargetVel = V3Dot(normal, V3LoadA(contact.targetVel));
250 const Vec3V ra = V3Sub(point, bodyFrame0p);
251 const Vec3V rb = V3Sub(point, bodyFrame1p);
256 Vec3V raXn = V3Cross(ra, norCross);
257 Vec3V rbXn = V3Cross(rb, norCross);
259 FloatV vRelAng = FSub(V3Dot(raXn, angVel0), V3Dot(rbXn, angVel1));
261 const Vec3V slop = V3Scale(solverOffsetSlop, FMax(FSel(FIsEq(norVel, zero), FMax(), FDiv(vRelAng, norVel)), FOne()));
263 raXn = V3Sel(V3IsGrtr(slop, V3Abs(raXn)), V3Zero(), raXn);
264 rbXn = V3Sel(V3IsGrtr(slop, V3Abs(rbXn)), V3Zero(), rbXn);
266 vRelAng = FSub(V3Dot(raXn, angVel0), V3Dot(rbXn, angVel1));
268 const FloatV vrel = FAdd(norVel, vRelAng);
271 const Vec3V raXnSqrtInertia = M33MulV3(invSqrtInertia0, raXn);
272 const Vec3V rbXnSqrtInertia = M33MulV3(invSqrtInertia1, rbXn);
274 const FloatV resp0 = FAdd(invMassNorLenSq0, FMul(V3Dot(raXnSqrtInertia, raXnSqrtInertia), angD0));
275 const FloatV resp1 = FSub(FMul(V3Dot(rbXnSqrtInertia, rbXnSqrtInertia), angD1), invMassNorLenSq1);
277 const FloatV unitResponse = FAdd(resp0, resp1);
279 const FloatV penetration = FSub(separation, restDistance);
280 const FloatV penetrationInvDt = FMul(penetration, invDt);
282 FloatV velMultiplier, scaledBias, impulseMultiplier;
283 const FloatV sumVRel(vrel);
285 const BoolV isGreater2 = BAnd(BAnd(FIsGrtr(restitution, zero), FIsGrtr(bounceThreshold, vrel)), FIsGrtr(FNeg(vrel), penetrationInvDt));
288 FloatV targetVelocity = FAdd(cTargetVel, FSel(isGreater2, FMul(FNeg(sumVRel), restitution), zero));
291 targetVelocity = FSub(targetVelocity, vrel);
293 FloatV biasedErr, unbiasedErr;
295 if (FAllGrtr(zero, restitution))
297 const FloatV nrdt = FMul(dt, restitution);
299 const FloatV a = FMul(dt, FSub(damping, nrdt));
300 const FloatV b = FMul(dt, FNeg(FMul(restitution, penetration)));
302 const FloatV x = FRecip(FScaleAdd(a, unitResponse, FOne()));
304 velMultiplier = FMul(x, a);
306 scaledBias = FMul(x, b);
307 impulseMultiplier = FSub(FOne(), x);
309 unbiasedErr = biasedErr = FScaleAdd(targetVelocity, velMultiplier, FNeg(scaledBias));
313 velMultiplier = FSel(FIsGrtr(unitResponse, zero), FRecip(unitResponse), zero);
315 const FloatV penetrationInvDtPt8 = FMax(maxPenBias, FMul(penetration, invDtp8));
317 scaledBias = FMul(velMultiplier, penetrationInvDtPt8);
319 const BoolV ccdSeparationCondition = FIsGrtrOrEq(ccdMaxSeparation, penetration);
321 scaledBias = FSel(BAnd(ccdSeparationCondition, isGreater2), zero, scaledBias);
323 impulseMultiplier = FLoad(1.f);
325 biasedErr = FScaleAdd(targetVelocity, velMultiplier, FNeg(scaledBias));
326 unbiasedErr = FScaleAdd(targetVelocity, velMultiplier, FSel(isGreater2, zero, FNeg(FMax(scaledBias, zero))));
336 FStore(biasedErr, &solverContact.biasedErr);
337 FStore(unbiasedErr, &solverContact.unbiasedErr);
338 FStore(impulseMultiplier, &solverContact.impulseMultiplier);
340 solverContact.raXn_velMultiplierW = V4SetW(Vec4V_From_Vec3V(raXnSqrtInertia), velMultiplier);
341 solverContact.rbXn_maxImpulseW = V4SetW(Vec4V_From_Vec3V(rbXnSqrtInertia), FLoad(contact.maxImpulse));
#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_CUDA_CALLABLE PX_FORCE_INLINE float PxAbs(float a)
abs returns the absolute value of its argument.
Definition PxMath.h:109
PX_FORCE_INLINE void * PxMemCopy(void *dest, const void *src, PxU32 count)
Copies the bytes of one memory block to another. The memory blocks must not overlap.
Definition PxMemory.h:83
PX_FORCE_INLINE void PxPrefetchLine(const void *ptr, uint32_t offset=0)
Definition PxUnixIntrinsics.h:83
Definition DyCorrelationBuffer.h:51
Definition DyContactPrepShared.h:186
@ eDISABLE_STRONG_FRICTION
Whether to use strong friction. The difference between "normal" and "strong" friction is that the str...
Definition PxMaterial.h:78
Definition PxVecMathAoSScalar.h:90
Definition PxVecMathAoSScalar.h:52
Definition PxVecMathAoSScalar.h:101
Definition PxVecMathAoSScalar.h:77
Definition PxUnixSse2AoS.h:115