2
0

BsPhysics.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Physics/BsPhysics.h"
  4. #include "Physics/BsRigidbody.h"
  5. #include "Math/BsRay.h"
  6. #include "Components/BsCCollider.h"
  7. namespace bs
  8. {
  9. Physics::Physics(const PHYSICS_INIT_DESC& init)
  10. {
  11. memset(mCollisionMap, 1, CollisionMapSize * CollisionMapSize * sizeof(bool));
  12. }
  13. void Physics::toggleCollision(UINT64 groupA, UINT64 groupB, bool enabled)
  14. {
  15. assert(groupA < CollisionMapSize && groupB < CollisionMapSize);
  16. Lock lock(mMutex);
  17. mCollisionMap[groupA][groupB] = enabled;
  18. }
  19. bool Physics::isCollisionEnabled(UINT64 groupA, UINT64 groupB) const
  20. {
  21. assert(groupA < CollisionMapSize && groupB < CollisionMapSize);
  22. enum class MyFlag
  23. {
  24. Flag1 = 1 << 0,
  25. Flag2 = 1 << 1,
  26. Flag3 = 1 << 2
  27. };
  28. Lock lock(mMutex);
  29. return mCollisionMap[groupA][groupB];
  30. }
  31. bool Physics::rayCast(const Ray& ray, PhysicsQueryHit& hit, UINT64 layer, float max) const
  32. {
  33. return rayCast(ray.getOrigin(), ray.getDirection(), hit, layer, max);
  34. }
  35. Vector<PhysicsQueryHit> Physics::rayCastAll(const Ray& ray, UINT64 layer, float max) const
  36. {
  37. return rayCastAll(ray.getOrigin(), ray.getDirection(), layer, max);
  38. }
  39. bool Physics::rayCastAny(const Ray& ray, UINT64 layer, float max) const
  40. {
  41. return rayCastAny(ray.getOrigin(), ray.getDirection(), layer, max);
  42. }
  43. Vector<HCollider> rawToComponent(const Vector<Collider*>& raw)
  44. {
  45. if (raw.empty())
  46. return Vector<HCollider>(0);
  47. Vector<HCollider> output;
  48. for (auto& entry : raw)
  49. {
  50. if (entry == nullptr)
  51. continue;
  52. CCollider* component = (CCollider*)entry->_getOwner(PhysicsOwnerType::Component);
  53. if (component == nullptr)
  54. continue;
  55. output.push_back(component->getHandle());
  56. }
  57. return output;
  58. }
  59. Vector<HCollider> Physics::boxOverlap(const AABox& box, const Quaternion& rotation, UINT64 layer) const
  60. {
  61. return rawToComponent(_boxOverlap(box, rotation, layer));
  62. }
  63. Vector<HCollider> Physics::sphereOverlap(const Sphere& sphere, UINT64 layer) const
  64. {
  65. return rawToComponent(_sphereOverlap(sphere, layer));
  66. }
  67. Vector<HCollider> Physics::capsuleOverlap(const Capsule& capsule, const Quaternion& rotation, UINT64 layer) const
  68. {
  69. return rawToComponent(_capsuleOverlap(capsule, rotation, layer));
  70. }
  71. Vector<HCollider> Physics::convexOverlap(const HPhysicsMesh& mesh, const Vector3& position,
  72. const Quaternion& rotation, UINT64 layer) const
  73. {
  74. return rawToComponent(_convexOverlap(mesh, position, rotation, layer));
  75. }
  76. Physics& gPhysics()
  77. {
  78. return Physics::instance();
  79. }
  80. }