PxContact.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2015, NVIDIA CORPORATION. All rights reserved.
3  *
4  * NVIDIA CORPORATION and its licensors retain all intellectual property
5  * and proprietary rights in and to this software, related documentation
6  * and any modifications thereto. Any use, reproduction, disclosure or
7  * distribution of this software and related documentation without an express
8  * license agreement from NVIDIA CORPORATION is strictly prohibited.
9  */
10 // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
11 // Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
12 
13 
14 #ifndef PX_CONTACT_H
15 #define PX_CONTACT_H
16 
17 #include "foundation/PxVec3.h"
18 
19 #ifndef PX_DOXYGEN
20 namespace physx
21 {
22 #endif
23 
24 #ifdef PX_VC
25 #pragma warning(push)
26 #pragma warning(disable: 4324) // Padding was added at the end of a structure because of a __declspec(align) value.
27 #endif
28 
29 #define PXC_CONTACT_NO_FACE_INDEX 0xffffffff
30 
31 
36 {
38  {
39  eHAS_FACE_INDICES = 1,
40  eMODIFIABLE = 2,
41  eFORCE_NO_RESPONSE = 4,
42  eHAS_MODIFIED_MASS_RATIOS = 8,
43  eHAS_TARGET_VELOCITY = 16,
44  eHAS_MAX_IMPULSE = 32
45  };
54  PxU16 flags; //4
55 };
56 
61 {
78 };
79 
84 {
92  PxU16 flags; //4
93 };
94 
99 {
124 };
125 
130 {
139 };
140 
145 {
154 };
155 
161 {
197  PxU32 flags; //72
198 };
199 
204 {
218  const PxU8* currPtr;
222  const PxU8* endPtr;
226  const PxU8* patchStart;
230  const PxU8* patchEnd;
269 
276  : zero(0.f), streamSize(size), nbContactsInPatch(0), currentContact(0)
277  {
278  const PxContactHeader* h = reinterpret_cast<const PxContactHeader*>(stream);
279  header = h;
280 
281  bool modify = false;
282  bool faceIndices = false;
283  bool response = false;
284 
285  PxU32 pointSize = 0;
286  PxU32 patchHeaderSize = 0;
287  const PxU8* start = NULL;
288 
289  if(size > 0)
290  {
291  modify = (h->flags & PxContactHeader::eMODIFIABLE) != 0;
292  faceIndices = (h->flags & PxContactHeader::eHAS_FACE_INDICES) != 0;
293 
294  start = stream + (modify ? sizeof(PxModifyContactHeader) : sizeof(PxContactHeader));
295 
296 
297  PX_ASSERT(((PxU32)(start - stream)) < size);
298  //if(((PxU32)(start - stream)) < size)
299  {
300  patchHeaderSize = modify ? sizeof(PxContactPatchBase) : sizeof(PxContactPatch);
301  pointSize = modify ? sizeof(PxModifiableContact) : faceIndices ? sizeof(PxFeatureContact) : sizeof(PxSimpleContact);
302 
303  response = (header->flags & PxContactHeader::eFORCE_NO_RESPONSE) == 0;
304  }
305  }
306 
307  contactsWereModifiable = (PxU32)modify;
308  hasFaceIndices = (PxU32)faceIndices;
309  forceNoResponse = (PxU32)!response;
310 
311  contactPatchHeaderSize = patchHeaderSize;
312  contactPointSize = pointSize;
313 
314  patchStart = start;
315  patchEnd = start;
316  currPtr = start;
317  }
318 
323  PX_FORCE_INLINE bool hasNextPatch() const
324  {
325  return ((PxU32)(patchEnd - reinterpret_cast<const PxU8*>(header))) < streamSize;
326  }
327 
332  PX_FORCE_INLINE PxU32 getTotalContactCount() const
333  {
334  return header->totalContactCount;
335  }
336 
340  PX_INLINE void nextPatch()
341  {
342  const PxU8* start = patchEnd;
343  patchStart = start;
344 
345  if(((PxU32)(start - (reinterpret_cast<const PxU8*>(header)))) < streamSize)
346  {
347  const PxU32 numContactsInPatch = *(reinterpret_cast<const PxU16*>(patchStart));
348  nbContactsInPatch = numContactsInPatch;
349 
350  patchEnd = start + contactPatchHeaderSize + numContactsInPatch * contactPointSize;
351  currPtr = start + contactPatchHeaderSize;
352  currentContact = 0;
353  }
354  else
355  {
356  patchEnd = start;
357  }
358  }
359 
364  PX_FORCE_INLINE bool hasNextContact() const
365  {
366  return currentContact < nbContactsInPatch;
367  }
368 
372  PX_FORCE_INLINE void nextContact()
373  {
374  PX_ASSERT(currentContact < nbContactsInPatch);
375  currentContact++;
376  contactStart = reinterpret_cast<const PxSimpleContact*>(currPtr);
377  currPtr += contactPointSize;
378  }
379 
384  PX_FORCE_INLINE const PxVec3& getContactNormal() const
385  {
386  return contactsWereModifiable ? getModifiableContact().normal : getContactPatch().normal;
387  }
388 
393  PX_FORCE_INLINE PxReal getInvMassScale0() const
394  {
395  return contactsWereModifiable ? getModifiableContactHeader().invMassScale0 : 1.f;
396  }
397 
402  PX_FORCE_INLINE PxReal getInvMassScale1() const
403  {
404  return contactsWereModifiable ? getModifiableContactHeader().invMassScale1 : 1.f;
405  }
406 
411  PX_FORCE_INLINE PxReal getInvInertiaScale0() const
412  {
413  return contactsWereModifiable ? getModifiableContactHeader().invInertiaScale0 : 1.f;
414  }
415 
420  PX_FORCE_INLINE PxReal getInvInertiaScale1() const
421  {
422  return contactsWereModifiable ? getModifiableContactHeader().invInertiaScale1 : 1.f;
423  }
424 
429  PX_FORCE_INLINE PxReal getMaxImpulse() const
430  {
431  return contactsWereModifiable ? getModifiableContact().maxImpulse : PX_MAX_REAL;
432  }
433 
438  PX_FORCE_INLINE const PxVec3& getTargetVel() const
439  {
440  return contactsWereModifiable ? getModifiableContact().targetVel : zero;
441  }
442 
447  PX_FORCE_INLINE const PxVec3& getContactPoint() const
448  {
449  return contactStart->contact;
450  }
451 
456  PX_FORCE_INLINE PxReal getSeparation() const
457  {
458  return contactStart->separation;
459  }
460 
465  PX_FORCE_INLINE PxU32 getFaceIndex0() const
466  {
467  return hasFaceIndices ? (static_cast<const PxFeatureContact*>(contactStart))->internalFaceIndex0 : PXC_CONTACT_NO_FACE_INDEX;
468  }
469 
474  PX_FORCE_INLINE PxU32 getFaceIndex1() const
475  {
476  return hasFaceIndices ? (static_cast<const PxFeatureContact*>(contactStart))->internalFaceIndex1 : PXC_CONTACT_NO_FACE_INDEX;
477  }
478 
483  PX_FORCE_INLINE PxReal getStaticFriction() const
484  {
485  return contactsWereModifiable ? getModifiableContact().staticFriction : getContactPatch().staticFriction;
486  }
487 
492  PX_FORCE_INLINE PxReal getDynamicFriction() const
493  {
494  return contactsWereModifiable ? getModifiableContact().dynamicFriction : getContactPatch().dynamicFriction;
495  }
496 
501  PX_FORCE_INLINE PxReal getRestitution() const
502  {
503  return contactsWereModifiable ? getModifiableContact().restitution : getContactPatch().restitution;
504  }
505 
510  PX_FORCE_INLINE PxU32 getMaterialFlags() const
511  {
512  return contactsWereModifiable ? getModifiableContact().flags : getContactPatch().flags;
513  }
514 
519  PX_FORCE_INLINE PxU16 getMaterialIndex0() const
520  {
521  return contactsWereModifiable ? getModifiableContact().materialIndex0 : getContactPatch().materialIndex0;
522  }
523 
528  PX_FORCE_INLINE PxU16 getMaterialIndex1() const
529  {
530  return contactsWereModifiable ? getModifiableContact().materialIndex1 : getContactPatch().materialIndex1;
531  }
532 
536  bool advanceToIndex(const PxU32 initialIndex)
537  {
538  PX_ASSERT(currPtr == (reinterpret_cast<const PxU8*>(header + 1)));
539 
540  PxU32 numToAdvance = initialIndex;
541 
542  if(numToAdvance == 0)
543  {
544  PX_ASSERT(hasNextPatch());
545  nextPatch();
546  return true;
547  }
548 
549  while(numToAdvance)
550  {
551  while(hasNextPatch())
552  {
553  nextPatch();
554  PxU32 patchSize = nbContactsInPatch;
555  if(numToAdvance <= patchSize)
556  {
557  while(hasNextContact())
558  {
559  --numToAdvance;
560  if(numToAdvance == 0)
561  return true;
562  nextContact();
563  }
564  }
565  else
566  {
567  numToAdvance -= patchSize;
568  }
569  }
570  }
571  return false;
572  }
573 
574 private:
575 
579  PX_FORCE_INLINE const PxContactPatch& getContactPatch() const
580  {
581  PX_ASSERT(!contactsWereModifiable);
582  return *reinterpret_cast<const PxContactPatch*>(patchStart);
583  }
584 
588  PX_FORCE_INLINE const PxModifiableContact& getModifiableContact() const
589  {
590  PX_ASSERT(contactsWereModifiable);
591  return *static_cast<const PxModifiableContact*>(contactStart);
592  }
593 
597  PX_FORCE_INLINE const PxModifyContactHeader& getModifiableContactHeader() const
598  {
599  PX_ASSERT(contactsWereModifiable);
600  return *static_cast<const PxModifyContactHeader*>(header);
601  }
602 
603 };
604 
605 
606 #ifdef PX_VC
607 #pragma warning(pop)
608 #endif
609 
610 #ifndef PX_DOXYGEN
611 } // namespace physx
612 #endif
613 
614 #endif


Copyright © 2008-2015 NVIDIA Corporation, 2701 San Tomas Expressway, Santa Clara, CA 95050 U.S.A. All rights reserved. www.nvidia.com