RavEngine
Loading...
Searching...
No Matches
DyContactPrepShared.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#ifndef DY_CONTACT_PREP_SHARED_H
30#define DY_CONTACT_PREP_SHARED_H
31
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"
40
41namespace physx
42{
43namespace Dy
44{
45template<class PxSolverContactDescT>
46PX_FORCE_INLINE Sc::ShapeInteraction* getInteraction(const PxSolverContactDescT& desc)
47{
48 return reinterpret_cast<Sc::ShapeInteraction*>(desc.shapeInteraction);
49}
50
51PX_FORCE_INLINE bool pointsAreClose(const PxTransform& body1ToBody0,
52 const PxVec3& localAnchor0, const PxVec3& localAnchor1,
53 const PxVec3& axis, float correlDist)
54{
55 const PxVec3 body0PatchPoint1 = body1ToBody0.transform(localAnchor1);
56
57 return PxAbs((localAnchor0 - body0PatchPoint1).dot(axis))<correlDist;
58}
59
60PX_FORCE_INLINE bool isSeparated(const FrictionPatch& patch, const PxTransform& body1ToBody0, const PxReal correlationDistance)
61{
62 PX_ASSERT(patch.anchorCount <= 2);
63 for(PxU32 a = 0; a < patch.anchorCount; ++a)
64 {
65 if(!pointsAreClose(body1ToBody0, patch.body0Anchors[a], patch.body1Anchors[a], patch.body0Normal, correlationDistance))
66 return true;
67 }
68 return false;
69}
70
71
72inline bool getFrictionPatches(CorrelationBuffer& c,
73 const PxU8* frictionCookie,
74 PxU32 frictionPatchCount,
75 const PxTransform& bodyFrame0,
76 const PxTransform& bodyFrame1,
77 PxReal correlationDistance)
78{
79 PX_UNUSED(correlationDistance);
80 if(frictionCookie == NULL || frictionPatchCount == 0)
81 return true;
82
83 //KS - this is now DMA'd inside the shader so we don't need to immediate DMA it here
84 const FrictionPatch* patches = reinterpret_cast<const FrictionPatch*>(frictionCookie);
85
86 //Try working out relative transforms! TODO - can we compute this lazily for the first friction patch
87 bool evaluated = false;
88 PxTransform body1ToBody0;
89
90 while(frictionPatchCount--)
91 {
92 PxPrefetchLine(patches,128);
93 const FrictionPatch& patch = *patches++;
94 PX_ASSERT (patch.broken == 0 || patch.broken == 1);
95 if(!patch.broken)
96 {
97 // if the eDISABLE_STRONG_FRICTION flag is there we need to blow away the previous frame's friction correlation, so
98 // that we can associate each friction anchor with a target velocity. So we lose strong friction.
99 if(patch.anchorCount != 0 && !(patch.materialFlags & PxMaterialFlag::eDISABLE_STRONG_FRICTION))
100 {
101 PX_ASSERT(patch.anchorCount <= 2);
102
103
104 if(!evaluated)
105 {
106 body1ToBody0 = bodyFrame0.transformInv(bodyFrame1);
107 evaluated = true;
108 }
109
110
111 if(patch.body0Normal.dot(body1ToBody0.rotate(patch.body1Normal)) > PXC_SAME_NORMAL)
112 {
113 if(!isSeparated(patch, body1ToBody0, correlationDistance))
114 {
115 if(c.frictionPatchCount == CorrelationBuffer::MAX_FRICTION_PATCHES)
116 return false;
117 {
118 c.contactID[c.frictionPatchCount][0] = 0xffff;
119 c.contactID[c.frictionPatchCount][1] = 0xffff;
120 //Rotate the contact normal into world space
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));
126 }
127 }
128 }
129 }
130 }
131 }
132 return true;
133}
134
135PX_FORCE_INLINE PxU32 extractContacts(PxContactBuffer& buffer, PxsContactManagerOutput& npOutput, bool& hasMaxImpulse, bool& hasTargetVelocity,
136 PxReal& invMassScale0, PxReal& invMassScale1, PxReal& invInertiaScale0, PxReal& invInertiaScale1, PxReal defaultMaxImpulse)
137{
138 PxContactStreamIterator iter(npOutput.contactPatches, npOutput.contactPoints, npOutput.getInternalFaceIndice(), npOutput.nbPatches, npOutput.nbContacts);
139
140 PxU32 numContacts = buffer.count, origContactCount = buffer.count;
141 if(!iter.forceNoResponse)
142 {
143 invMassScale0 = iter.getInvMassScale0();
144 invMassScale1 = iter.getInvMassScale1();
145 invInertiaScale0 = iter.getInvInertiaScale0();
146 invInertiaScale1 = iter.getInvInertiaScale1();
147 hasMaxImpulse = (iter.patch->internalFlags & PxContactPatch::eHAS_MAX_IMPULSE) != 0;
148 hasTargetVelocity = (iter.patch->internalFlags & PxContactPatch::eHAS_TARGET_VELOCITY) != 0;
149
150 while(iter.hasNextPatch())
151 {
152 iter.nextPatch();
153 while(iter.hasNextContact())
154 {
155 iter.nextContact();
156 PxPrefetchLine(iter.contact, 128);
157 PxPrefetchLine(&buffer.contacts[numContacts], 128);
158 PxReal maxImpulse = hasMaxImpulse ? iter.getMaxImpulse() : defaultMaxImpulse;
159 if(maxImpulse != 0.f)
160 {
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();
166 //KS - we use the face indices to cache the material indices and flags - avoids bloating the PxContact structure
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;
175 ++numContacts;
176 }
177 }
178 }
179 }
180 const PxU32 contactCount = numContacts - origContactCount;
181 buffer.count = numContacts;
182 return contactCount;
183}
184
186{
187 CorrelationBuffer& buffer;
188 PxU32 currPatch;
189 PxU32 currContact;
190
191 CorrelationListIterator(CorrelationBuffer& correlationBuffer, PxU32 startPatch) : buffer(correlationBuffer)
192 {
193 //We need to force us to advance the correlation buffer to the first available contact (if one exists)
194 PxU32 newPatch = startPatch, newContact = 0;
195
196 while(newPatch != CorrelationBuffer::LIST_END && newContact == buffer.contactPatches[newPatch].count)
197 {
198 newPatch = buffer.contactPatches[newPatch].next;
199 newContact = 0;
200 }
201
202 currPatch = newPatch;
203 currContact = newContact;
204 }
205
206 //Returns true if it has another contact pre-loaded. Returns false otherwise
207 PX_FORCE_INLINE bool hasNextContact()
208 {
209 return (currPatch != CorrelationBuffer::LIST_END && currContact < buffer.contactPatches[currPatch].count);
210 }
211
212 inline void nextContact(PxU32& patch, PxU32& contact)
213 {
214 PX_ASSERT(currPatch != CorrelationBuffer::LIST_END);
215 PX_ASSERT(currContact < buffer.contactPatches[currPatch].count);
216
217 patch = currPatch;
218 contact = currContact;
219 PxU32 newPatch = currPatch, newContact = currContact + 1;
220
221 while(newPatch != CorrelationBuffer::LIST_END && newContact == buffer.contactPatches[newPatch].count)
222 {
223 newPatch = buffer.contactPatches[newPatch].next;
224 newContact = 0;
225 }
226
227 currPatch = newPatch;
228 currContact = newContact;
229 }
230
231private:
233
234};
235
236
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,
241 const FloatVArg bounceThreshold, const PxContactPoint& contact, SolverContactPoint& solverContact,
242 const FloatVArg ccdMaxSeparation, const Vec3VArg solverOffsetSlop, const FloatVArg damping)
243 {
244 const FloatV zero = FZero();
245 const Vec3V point = V3LoadA(contact.point);
246 const FloatV separation = FLoad(contact.separation);
247
248 const FloatV cTargetVel = V3Dot(normal, V3LoadA(contact.targetVel));
249
250 const Vec3V ra = V3Sub(point, bodyFrame0p);
251 const Vec3V rb = V3Sub(point, bodyFrame1p);
252
253 /*ra = V3Sel(V3IsGrtr(solverOffsetSlop, V3Abs(ra)), V3Zero(), ra);
254 rb = V3Sel(V3IsGrtr(solverOffsetSlop, V3Abs(rb)), V3Zero(), rb);*/
255
256 Vec3V raXn = V3Cross(ra, norCross);
257 Vec3V rbXn = V3Cross(rb, norCross);
258
259 FloatV vRelAng = FSub(V3Dot(raXn, angVel0), V3Dot(rbXn, angVel1));
260
261 const Vec3V slop = V3Scale(solverOffsetSlop, FMax(FSel(FIsEq(norVel, zero), FMax(), FDiv(vRelAng, norVel)), FOne()));
262
263 raXn = V3Sel(V3IsGrtr(slop, V3Abs(raXn)), V3Zero(), raXn);
264 rbXn = V3Sel(V3IsGrtr(slop, V3Abs(rbXn)), V3Zero(), rbXn);
265
266 vRelAng = FSub(V3Dot(raXn, angVel0), V3Dot(rbXn, angVel1));
267
268 const FloatV vrel = FAdd(norVel, vRelAng);
269
270
271 const Vec3V raXnSqrtInertia = M33MulV3(invSqrtInertia0, raXn);
272 const Vec3V rbXnSqrtInertia = M33MulV3(invSqrtInertia1, rbXn);
273
274 const FloatV resp0 = FAdd(invMassNorLenSq0, FMul(V3Dot(raXnSqrtInertia, raXnSqrtInertia), angD0));
275 const FloatV resp1 = FSub(FMul(V3Dot(rbXnSqrtInertia, rbXnSqrtInertia), angD1), invMassNorLenSq1);
276
277 const FloatV unitResponse = FAdd(resp0, resp1);
278
279 const FloatV penetration = FSub(separation, restDistance);
280 const FloatV penetrationInvDt = FMul(penetration, invDt);
281
282 FloatV velMultiplier, scaledBias, impulseMultiplier;
283 const FloatV sumVRel(vrel);
284
285 const BoolV isGreater2 = BAnd(BAnd(FIsGrtr(restitution, zero), FIsGrtr(bounceThreshold, vrel)), FIsGrtr(FNeg(vrel), penetrationInvDt));
286
287
288 FloatV targetVelocity = FAdd(cTargetVel, FSel(isGreater2, FMul(FNeg(sumVRel), restitution), zero));
289
290 //Note - we add on the initial target velocity
291 targetVelocity = FSub(targetVelocity, vrel);
292
293 FloatV biasedErr, unbiasedErr;
294
295 if (FAllGrtr(zero, restitution))
296 {
297 const FloatV nrdt = FMul(dt, restitution);
298
299 const FloatV a = FMul(dt, FSub(damping, nrdt));
300 const FloatV b = FMul(dt, FNeg(FMul(restitution, penetration)));
301
302 const FloatV x = FRecip(FScaleAdd(a, unitResponse, FOne()));
303
304 velMultiplier = FMul(x, a);
305 //scaledBias = FSel(isSeparated, FNeg(invStepDt), FDiv(FMul(nrdt, FMul(x, unitResponse)), velMultiplier));
306 scaledBias = FMul(x, b);
307 impulseMultiplier = FSub(FOne(), x);
308
309 unbiasedErr = biasedErr = FScaleAdd(targetVelocity, velMultiplier, FNeg(scaledBias));
310 }
311 else
312 {
313 velMultiplier = FSel(FIsGrtr(unitResponse, zero), FRecip(unitResponse), zero);
314
315 const FloatV penetrationInvDtPt8 = FMax(maxPenBias, FMul(penetration, invDtp8));
316
317 scaledBias = FMul(velMultiplier, penetrationInvDtPt8);
318
319 const BoolV ccdSeparationCondition = FIsGrtrOrEq(ccdMaxSeparation, penetration);
320
321 scaledBias = FSel(BAnd(ccdSeparationCondition, isGreater2), zero, scaledBias);
322
323 impulseMultiplier = FLoad(1.f);
324
325 biasedErr = FScaleAdd(targetVelocity, velMultiplier, FNeg(scaledBias));
326 unbiasedErr = FScaleAdd(targetVelocity, velMultiplier, FSel(isGreater2, zero, FNeg(FMax(scaledBias, zero))));
327
328 }
329
330
331
332
333
334 //const FloatV unbiasedErr = FScaleAdd(targetVelocity, velMultiplier, FNeg(FMax(scaledBias, zero)));
335
336 FStore(biasedErr, &solverContact.biasedErr);
337 FStore(unbiasedErr, &solverContact.unbiasedErr);
338 FStore(impulseMultiplier, &solverContact.impulseMultiplier);
339
340 solverContact.raXn_velMultiplierW = V4SetW(Vec4V_From_Vec3V(raXnSqrtInertia), velMultiplier);
341 solverContact.rbXn_maxImpulseW = V4SetW(Vec4V_From_Vec3V(rbXnSqrtInertia), FLoad(contact.maxImpulse));
342 }
343}
344}
345
346#endif
#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
A single rigid body contact point for the solver.
Definition DySolverContact.h:117
@ eHAS_TARGET_VELOCITY
Indicates this contact stream has target velocities set.
Definition PxContact.h:80
@ eHAS_MAX_IMPULSE
Indicates this contact stream has max impulses set.
Definition PxContact.h:81
Definition PxContactPoint.h:40
@ 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