PxContact.h
Go to the documentation of this file.
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 
31 #ifndef PX_CONTACT_H
32 #define PX_CONTACT_H
33 
34 #include "foundation/PxVec3.h"
35 #include "foundation/PxAssert.h"
36 
37 #if !PX_DOXYGEN
38 namespace physx
39 {
40 #endif
41 
42 #if PX_VC
43 #pragma warning(push)
44 #pragma warning(disable: 4324) // Padding was added at the end of a structure because of a __declspec(align) value.
45 #endif
46 
47 #define PXC_CONTACT_NO_FACE_INDEX 0xffffffff
48 
51 {
56 }
57 PX_ALIGN_SUFFIX(16);
58 
65 {
67  {
77  };
78 
79  PX_ALIGN(16, PxMassModificationProps mMassModification); //16
83  PX_ALIGN(16, PxVec3 normal); //28
88 
92  PxU8 nbContacts; //42 //Can be a U8
93 
94  PxU8 materialFlags; //43 //Can be a U16
95  PxU8 internalFlags; //44 //Can be a U16
96  PxU16 materialIndex0; //46 //Can be a U16
97  PxU16 materialIndex1; //48 //Can be a U16
98 
99 
100 }
101 PX_ALIGN_SUFFIX(16);
102 
107 PX_ALIGN_PREFIX(16)
108 struct PxContact
109 {
118 }
119 PX_ALIGN_SUFFIX(16);
120 
121 PX_ALIGN_PREFIX(16)
123 {
127  PX_ALIGN(16, PxVec3 targetVelocity); //28
132 }
133 PX_ALIGN_SUFFIX(16);
134 
139 PX_ALIGN_PREFIX(16)
141 {
145  PX_ALIGN(16, PxVec3 normal); //44
150 
155 
172 }
173 PX_ALIGN_SUFFIX(16);
174 
179 {
181  {
184  eCOMPRESSED_MODIFIABLE_STREAM
185  };
196 
201 
206 
207 
212 
217 
222 
227 
228  /*
229  \brief Size of contact patch header
230  \note This varies whether the patch is modifiable or not.
231  */
246 
248 
250 
254  PX_CUDA_CALLABLE PX_FORCE_INLINE PxContactStreamIterator(const PxU8* contactPatches, const PxU8* contactPoints, const PxU32* contactFaceIndices, PxU32 nbPatches, PxU32 nbContacts)
255  : zero(0.f)
256  {
257  bool modify = false;
258  bool compressedModify = false;
259  bool response = false;
260  bool indices = false;
261 
262  PxU32 pointSize = 0;
263  PxU32 patchHeaderSize = sizeof(PxContactPatch);
264 
265  const PxContactPatch* patches = reinterpret_cast<const PxContactPatch*>(contactPatches);
266 
267  if(patches)
268  {
269  modify = (patches->internalFlags & PxContactPatch::eMODIFIABLE) != 0;
270  compressedModify = (patches->internalFlags & PxContactPatch::eCOMPRESSED_MODIFIED_CONTACT) != 0;
271  indices = (patches->internalFlags & PxContactPatch::eHAS_FACE_INDICES) != 0;
272 
273  patch = patches;
274 
275  contact = reinterpret_cast<const PxContact*>(contactPoints);
276 
277  faceIndice = contactFaceIndices;
278 
279  pointSize = compressedModify ? sizeof(PxExtendedContact) : modify ? sizeof(PxModifiableContact) : sizeof(PxContact);
280 
281  response = (patch->internalFlags & PxContactPatch::eFORCE_NO_RESPONSE) == 0;
282  }
283 
284 
285  mStreamFormat = compressedModify ? eCOMPRESSED_MODIFIABLE_STREAM : modify ? eMODIFIABLE_STREAM : eSIMPLE_STREAM;
286  hasFaceIndices = PxU32(indices);
287  forceNoResponse = PxU32(!response);
288 
289  contactPatchHeaderSize = patchHeaderSize;
290  contactPointSize = pointSize;
291  nextPatchIndex = 0;
292  nextContactIndex = 0;
293  totalContacts = nbContacts;
294  totalPatches = nbPatches;
295 
296  pointStepped = false;
297  }
298 
304  {
305  return nextPatchIndex < totalPatches;
306  }
307 
313  {
314  return totalContacts;
315  }
316 
318  {
319  return totalPatches;
320  }
321 
326  {
327  PX_ASSERT(nextPatchIndex < totalPatches);
328  if(nextPatchIndex)
329  {
330  if(nextContactIndex < patch->nbContacts)
331  {
332  PxU32 nbToStep = patch->nbContacts - this->nextContactIndex;
333  contact = reinterpret_cast<const PxContact*>(reinterpret_cast<const PxU8*>(contact) + contactPointSize * nbToStep);
334  }
335  patch = reinterpret_cast<const PxContactPatch*>(reinterpret_cast<const PxU8*>(patch) + contactPatchHeaderSize);
336  }
337  nextPatchIndex++;
338  nextContactIndex = 0;
339  }
340 
346  {
347  return nextContactIndex < (patch->nbContacts);
348  }
349 
354  {
355  PX_ASSERT(nextContactIndex < patch->nbContacts);
356  if(pointStepped)
357  {
358  contact = reinterpret_cast<const PxContact*>(reinterpret_cast<const PxU8*>(contact) + contactPointSize);
359  faceIndice++;
360  }
361  nextContactIndex++;
362  pointStepped = true;
363  }
364 
365 
371  {
372  return getContactPatch().normal;
373  }
374 
380  {
381  return patch->mMassModification.mInvMassScale0;
382  }
383 
389  {
390  return patch->mMassModification.mInvMassScale1;
391  }
392 
398  {
399  return patch->mMassModification.mInvInertiaScale0;
400  }
401 
407  {
408  return patch->mMassModification.mInvInertiaScale1;
409  }
410 
416  {
417  return mStreamFormat != eSIMPLE_STREAM ? getExtendedContact().maxImpulse : PX_MAX_REAL;
418  }
419 
425  {
426  return mStreamFormat != eSIMPLE_STREAM ? getExtendedContact().targetVelocity : zero;
427  }
428 
434  {
435  return contact->contact;
436  }
443  {
444  return contact->separation;
445  }
452  {
454  }
455 
460  PX_CUDA_CALLABLE PX_FORCE_INLINE PxU32 getFaceIndex1() const
461  {
462  return hasFaceIndices ? *faceIndice : PXC_CONTACT_NO_FACE_INDEX;
463  }
470  {
471  return getContactPatch().staticFriction;
472  }
473 
479  {
480  return getContactPatch().dynamicFriction;
481  }
482 
488  {
489  return getContactPatch().restitution;
490  }
491 
497  {
498  return getContactPatch().materialFlags;
499  }
500 
506  {
507  return PxU16(getContactPatch().materialIndex0);
508  }
509 
515  {
516  return PxU16(getContactPatch().materialIndex1);
517  }
518 
522  bool advanceToIndex(const PxU32 initialIndex)
523  {
524  PX_ASSERT(this->nextPatchIndex == 0 && this->nextContactIndex == 0);
525 
526  PxU32 numToAdvance = initialIndex;
527 
528  if(numToAdvance == 0)
529  {
530  PX_ASSERT(hasNextPatch());
531  nextPatch();
532  return true;
533  }
534 
535  while(numToAdvance)
536  {
537  while(hasNextPatch())
538  {
539  nextPatch();
540  PxU32 patchSize = patch->nbContacts;
541  if(numToAdvance <= patchSize)
542  {
543  contact = reinterpret_cast<const PxContact*>(reinterpret_cast<const PxU8*>(contact) + contactPointSize * numToAdvance);
544  nextContactIndex += numToAdvance;
545  return true;
546  }
547  else
548  {
549  numToAdvance -= patchSize;
550  }
551  }
552  }
553  return false;
554  }
555 
556 private:
557 
562  {
563  return *static_cast<const PxContactPatch*>(patch);
564  }
565 
567  {
568  PX_ASSERT(mStreamFormat == eMODIFIABLE_STREAM || mStreamFormat == eCOMPRESSED_MODIFIABLE_STREAM);
569  return *static_cast<const PxExtendedContact*>(contact);
570  }
571 
572 };
573 
574 
575 #if PX_VC
576 #pragma warning(pop)
577 #endif
578 
579 #if !PX_DOXYGEN
580 } // namespace physx
581 #endif
582 
583 #endif
Definition: GuContactBuffer.h:37
PxU32 forceNoResponse
Indicates whether this stream is notify-only or not.
Definition: PxContact.h:245
PxReal dynamicFriction
Definition: PxContact.h:89
Indicates this contact stream has face indices.
Definition: PxContact.h:438
Contact point data including face (feature) indices.
Definition: PxContact.h:108
PxU32 totalContacts
The total number of contact points in this stream.
Definition: PxContact.h:216
PX_CUDA_CALLABLE PX_FORCE_INLINE PxReal getMaxImpulse() const
Gets the contact&#39;s max impulse.
Definition: PxContact.h:415
PxReal separation
Separation value (negative implies penetration).
Definition: PxContact.h:117
PxU8 internalFlags
Definition: PxContact.h:95
PxU8 nbContacts
Definition: PxContact.h:462
PxReal mInvInertiaScale0
Definition: PxContact.h:53
PxU32 nextPatchIndex
The current patch Index.
Definition: PxContact.h:226
PX_CUDA_CALLABLE PX_FORCE_INLINE const PxVec3 & getTargetVel() const
Gets the contact&#39;s target velocity.
Definition: PxContact.h:424
const PxU32 * faceIndice
The contact triangle face index.
Definition: PxContact.h:205
PxU16 materialIndex1
Shape B&#39;s material index.
Definition: PxContact.h:163
#define PX_FORCE_INLINE
Definition: PxPreprocessor.h:351
float PxReal
Definition: PxSimpleTypes.h:78
#define PXC_CONTACT_NO_FACE_INDEX
Definition: PxContact.h:47
PxReal maxImpulse
Maximum impulse.
Definition: PxContact.h:131
PX_CUDA_CALLABLE PX_FORCE_INLINE PxU16 getMaterialIndex1() const
Gets the contact&#39;s material index for shape 1.
Definition: PxContact.h:514
PX_CUDA_CALLABLE PX_FORCE_INLINE PxReal getInvMassScale0() const
Gets the inverse mass scale for body 0.
Definition: PxContact.h:379
#define PX_MAX_REAL
Definition: PxSimpleTypes.h:93
uint32_t PxU32
Definition: PxSimpleTypes.h:71
PxU8 materialFlags
Definition: PxContact.h:94
PxU32 nextContactIndex
The current contact index.
Definition: PxContact.h:221
Indicates this contact stream has max impulses set.
Definition: PxContact.h:443
PxU32 materialFlags
Material Flags.
Definition: PxContact.h:154
PX_CUDA_CALLABLE PX_FORCE_INLINE PxReal getDynamicFriction() const
Gets the contact&#39;s static dynamic coefficient.
Definition: PxContact.h:478
PX_CUDA_CALLABLE PX_FORCE_INLINE PxReal getInvInertiaScale0() const
Gets the inverse inertia scale for body 0.
Definition: PxContact.h:397
Indicates this contact stream has target velocities set.
Definition: PxContact.h:442
PX_CUDA_CALLABLE PX_FORCE_INLINE bool hasNextPatch() const
Returns whether there are more patches in this stream.
Definition: PxContact.h:303
PxVec3 zero
Utility zero vector to optimize functions returning zero vectors when a certain flag isn&#39;t set...
Definition: PxContact.h:191
PxU8 startContactIndex
Definition: PxContact.h:91
PX_CUDA_CALLABLE PX_FORCE_INLINE PxContactStreamIterator(const PxU8 *contactPatches, const PxU8 *contactPoints, const PxU32 *contactFaceIndices, PxU32 nbPatches, PxU32 nbContacts)
Constructor.
Definition: PxContact.h:254
PxU32 hasFaceIndices
Definition: PxContact.h:249
Indicates this contact stream has face indices.
Definition: PxContact.h:68
PxU8 nbContacts
Definition: PxContact.h:92
PxU32 contactPatchHeaderSize
Definition: PxContact.h:232
Header for contact patch where all points share same material and normal.
Definition: PxContact.h:64
PxReal mInvInertiaScale1
Definition: PxContact.h:55
PX_CUDA_CALLABLE PX_FORCE_INLINE PxU32 getMaterialFlags() const
Gets the contact&#39;s material flags.
Definition: PxContact.h:496
PxU32 totalPatches
The total number of patches in this contact stream.
Definition: PxContact.h:211
PX_CUDA_CALLABLE PX_FORCE_INLINE PxU32 getFaceIndex0() const
Gets the contact&#39;s face index for shape 0.
Definition: PxContact.h:451
PxContactPatchFlags
Definition: PxContact.h:66
PX_CUDA_CALLABLE PX_FORCE_INLINE bool hasNextContact() const
Returns if the current patch has more contacts.
Definition: PxContact.h:345
PX_CUDA_CALLABLE PX_FORCE_INLINE PxU32 getTotalPatchCount() const
Definition: PxContact.h:317
PxReal staticFriction
Definition: PxContact.h:90
Definition: PxContact.h:50
PxReal mInvMassScale1
Definition: PxContact.h:54
PxU16 materialIndex1
Shape B&#39;s material index.
Definition: PxContact.h:467
PxReal mInvMassScale0
Definition: PxContact.h:52
#define PX_ALIGN_PREFIX(alignment)
Definition: PxPreprocessor.h:409
uint8_t PxU8
Definition: PxSimpleTypes.h:75
Definition: PxContact.h:122
PX_CUDA_CALLABLE PX_FORCE_INLINE const PxVec3 & getContactNormal() const
Gets the current contact&#39;s normal.
Definition: PxContact.h:370
This is required if the application modified either the contact normal or the material properties...
Definition: PxContact.h:444
Definition: PxContact.h:182
bool pointStepped
Definition: PxContact.h:247
PX_CUDA_CALLABLE PX_FORCE_INLINE const PxContactPatch & getContactPatch() const
Internal helper.
Definition: PxContact.h:561
PxVec3 contact
Contact point in world space.
Definition: PxContact.h:113
StreamFormat mStreamFormat
The stream format.
Definition: PxContact.h:241
PX_CUDA_CALLABLE PX_FORCE_INLINE void nextContact()
Advances to the next contact in the patch.
Definition: PxContact.h:353
struct PxContactStreamIterator PX_ALIGN_SUFFIX
bool advanceToIndex(const PxU32 initialIndex)
Advances the contact stream iterator to a specific contact index.
Definition: PxContact.h:522
PxU16 materialIndex0
Shape A&#39;s material index.
Definition: PxContact.h:159
PxReal restitution
Restitution coefficient.
Definition: PxContact.h:149
PX_CUDA_CALLABLE PX_FORCE_INLINE PxReal getStaticFriction() const
Gets the contact&#39;s static friction coefficient.
Definition: PxContact.h:469
PxVec3 contact
Contact point in world space.
Definition: PxContact.h:439
Indicates this contact stream is notify-only (no contact response).
Definition: PxContact.h:70
Indicates this contact stream has modified mass ratios.
Definition: PxContact.h:441
PxU16 materialIndex0
Definition: PxContact.h:96
StreamFormat
Definition: PxContact.h:180
Indicates this contact stream is notify-only (no contact response).
Definition: PxContact.h:440
PX_CUDA_CALLABLE PX_FORCE_INLINE const PxVec3 & getContactPoint() const
Gets the contact&#39;s contact point.
Definition: PxContact.h:433
PxReal restitution
Restitution coefficient.
Definition: PxContact.h:87
PxU16 materialIndex0
Shape A&#39;s material index.
Definition: PxContact.h:466
PxReal dynamicFriction
dynamic friction coefficient
Definition: PxContact.h:171
#define PX_ASSERT(exp)
Definition: PxAssert.h:59
PxU16 materialIndex1
Definition: PxContact.h:97
PxU32 contactPointSize
Contact point size.
Definition: PxContact.h:237
PX_ALIGN(16, PxMassModificationProps mMassModification)
A modifiable contact point. This has additional fields per-contact to permit modification by user...
Definition: PxContact.h:140
PX_CUDA_CALLABLE PX_FORCE_INLINE const PxExtendedContact & getExtendedContact() const
Definition: PxContact.h:566
const PxContact * contact
The contacts.
Definition: PxContact.h:200
uint16_t PxU16
Definition: PxSimpleTypes.h:73
Indicates this contact stream is modifiable.
Definition: PxContact.h:69
uint32_t PxU32
Definition: Px.h:48
Indicates this contact stream is modifiable.
Definition: PxContact.h:439
PX_CUDA_CALLABLE PX_FORCE_INLINE PxU32 getTotalContactCount() const
Returns the total contact count.
Definition: PxContact.h:312
PX_CUDA_CALLABLE PX_FORCE_INLINE PxReal getInvInertiaScale1() const
Gets the inverse inertia scale for body 1.
Definition: PxContact.h:406
const PxContactPatch * patch
The patch headers.
Definition: PxContact.h:195
PX_CUDA_CALLABLE PX_FORCE_INLINE PxU16 getMaterialIndex0() const
Gets the contact&#39;s material index for shape 0.
Definition: PxContact.h:505
#define PX_INLINE
Definition: PxPreprocessor.h:336
#define PX_CUDA_CALLABLE
Definition: PxPreprocessor.h:460
A class to iterate over a compressed contact stream. This supports read-only access to the various co...
Definition: PxContact.h:178
3 Element vector class.
Definition: PxVec3.h:49
PX_CUDA_CALLABLE PX_FORCE_INLINE PxReal getInvMassScale1() const
Gets the inverse mass scale for body 1.
Definition: PxContact.h:388
PX_CUDA_CALLABLE PX_FORCE_INLINE PxReal getRestitution() const
Gets the contact&#39;s restitution coefficient.
Definition: PxContact.h:487
Definition: PxContact.h:446
PX_CUDA_CALLABLE PX_INLINE void nextPatch()
Advances iterator to next contact patch.
Definition: PxContact.h:325
PxReal staticFriction
static friction coefficient
Definition: PxContact.h:167