PhysicsTrigger.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright (C) 2009-2020, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <anki/physics/PhysicsTrigger.h>
  6. #include <anki/physics/PhysicsCollisionShape.h>
  7. #include <anki/physics/PhysicsWorld.h>
  8. #include <anki/util/Rtti.h>
  9. namespace anki
  10. {
  11. PhysicsTrigger::PhysicsTrigger(PhysicsWorld* world, PhysicsCollisionShapePtr shape)
  12. : PhysicsFilteredObject(CLASS_TYPE, world)
  13. {
  14. m_shape = shape;
  15. m_ghostShape.init();
  16. m_ghostShape->setWorldTransform(btTransform::getIdentity());
  17. m_ghostShape->setCollisionShape(shape->getBtShape(true));
  18. m_ghostShape->setUserPointer(static_cast<PhysicsObject*>(this));
  19. setMaterialGroup(PhysicsMaterialBit::TRIGGER);
  20. setMaterialMask(PhysicsMaterialBit::ALL);
  21. auto lock = getWorld().lockBtWorld();
  22. getWorld().getBtWorld()->addCollisionObject(m_ghostShape.get());
  23. }
  24. PhysicsTrigger::~PhysicsTrigger()
  25. {
  26. {
  27. auto lock = getWorld().lockBtWorld();
  28. getWorld().getBtWorld()->removeCollisionObject(m_ghostShape.get());
  29. }
  30. m_ghostShape.destroy();
  31. }
  32. void PhysicsTrigger::processContacts()
  33. {
  34. if(m_contactCallback == nullptr)
  35. {
  36. return;
  37. }
  38. if(m_ghostShape->getOverlappingPairs().size() < 0)
  39. {
  40. return;
  41. }
  42. // Process contacts
  43. for(U32 i = 0; i < U32(m_ghostShape->getOverlappingPairs().size()); ++i)
  44. {
  45. btCollisionObject* obj = m_ghostShape->getOverlappingPairs()[i];
  46. ANKI_ASSERT(obj);
  47. PhysicsObject* aobj = static_cast<PhysicsObject*>(obj->getUserPointer());
  48. ANKI_ASSERT(aobj);
  49. PhysicsFilteredObject* fobj = dcast<PhysicsFilteredObject*>(aobj);
  50. m_contactCallback->processContact(*this, *fobj);
  51. }
  52. }
  53. } // end namespace anki