pxContactReporter.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "T3D/physics/physX/pxContactReporter.h"
  24. #include "T3D/physics/physX/pxCasts.h"
  25. #include "T3D/physics/physicsUserData.h"
  26. #include "T3D/physics/physX/pxMultiActor.h"
  27. #include "platform/profiler.h"
  28. PxContactReporter::PxContactReporter()
  29. {
  30. }
  31. PxContactReporter::~PxContactReporter()
  32. {
  33. }
  34. void PxContactReporter::onContactNotify( NxContactPair &pair, NxU32 events )
  35. {
  36. PROFILE_SCOPE( PxContactReporter_OnContactNotify );
  37. // For now we only care about start touch events.
  38. if ( !( events & NX_NOTIFY_ON_START_TOUCH ) )
  39. return;
  40. // Skip if either actor is deleted.
  41. if ( pair.isDeletedActor[0] || pair.isDeletedActor[1] )
  42. return;
  43. NxActor *actor0 = pair.actors[0];
  44. NxActor *actor1 = pair.actors[1];
  45. PhysicsUserData *userData0 = PhysicsUserData::cast( actor0->userData );
  46. PhysicsUserData *userData1 = PhysicsUserData::cast( actor1->userData );
  47. // Early out if we don't have user data or signals to notify.
  48. if ( ( !userData0 || userData0->getContactSignal().isEmpty() ) &&
  49. ( !userData1 || userData1->getContactSignal().isEmpty() ) )
  50. return;
  51. // Get an average contact point.
  52. U32 points = 0;
  53. NxVec3 hitPoint( 0.0f );
  54. NxContactStreamIterator iter( pair.stream );
  55. while( iter.goNextPair() )
  56. {
  57. while( iter.goNextPatch() )
  58. {
  59. while( iter.goNextPoint() )
  60. {
  61. hitPoint += iter.getPoint();
  62. ++points;
  63. }
  64. }
  65. }
  66. hitPoint /= (F32)points;
  67. if ( userData0 )
  68. userData0->getContactSignal().trigger( userData0,
  69. userData1,
  70. pxCast<Point3F>( hitPoint ),
  71. pxCast<Point3F>( pair.sumNormalForce ) );
  72. if ( userData1 )
  73. userData1->getContactSignal().trigger( userData1,
  74. userData0,
  75. pxCast<Point3F>( hitPoint ),
  76. pxCast<Point3F>( -pair.sumNormalForce ) );
  77. }
  78. bool PxUserNotify::onJointBreak( NxReal breakingForce, NxJoint &brokenJoint )
  79. {
  80. PROFILE_SCOPE( PxUserNotify_OnJointBreak );
  81. PxUserData *userData = PxUserData::getData( brokenJoint );
  82. if ( userData )
  83. userData->getOnJointBreakSignal().trigger( breakingForce, brokenJoint );
  84. // NOTE: Returning true here will tell the
  85. // PhysX SDK to delete the joint, which will
  86. // cause MANY problems if any of the user app's
  87. // objects still hold references to it.
  88. return false;
  89. }