1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#include "Physics/BsPhysics.h"
4#include "Physics/BsRigidbody.h"
5#include "Math/BsRay.h"
6#include "Components/BsCCollider.h"
7
8namespace bs
9{
10 Physics::Physics(const PHYSICS_INIT_DESC& init)
11 {
12 memset(mCollisionMap, 1, CollisionMapSize * CollisionMapSize * sizeof(bool));
13 }
14
15 void Physics::toggleCollision(UINT64 groupA, UINT64 groupB, bool enabled)
16 {
17 assert(groupA < CollisionMapSize && groupB < CollisionMapSize);
18
19 Lock lock(mMutex);
20 mCollisionMap[groupA][groupB] = enabled;
21 }
22
23 bool Physics::isCollisionEnabled(UINT64 groupA, UINT64 groupB) const
24 {
25 assert(groupA < CollisionMapSize && groupB < CollisionMapSize);
26
27 enum class MyFlag
28 {
29 Flag1 = 1 << 0,
30 Flag2 = 1 << 1,
31 Flag3 = 1 << 2
32 };
33
34 Lock lock(mMutex);
35 return mCollisionMap[groupA][groupB];
36 }
37
38 bool PhysicsScene::rayCast(const Ray& ray, PhysicsQueryHit& hit, UINT64 layer, float max) const
39 {
40 return rayCast(ray.getOrigin(), ray.getDirection(), hit, layer, max);
41 }
42
43 Vector<PhysicsQueryHit> PhysicsScene::rayCastAll(const Ray& ray, UINT64 layer, float max) const
44 {
45 return rayCastAll(ray.getOrigin(), ray.getDirection(), layer, max);
46 }
47
48 bool PhysicsScene::rayCastAny(const Ray& ray, UINT64 layer, float max) const
49 {
50 return rayCastAny(ray.getOrigin(), ray.getDirection(), layer, max);
51 }
52
53 Vector<HCollider> rawToComponent(const Vector<Collider*>& raw)
54 {
55 if (raw.empty())
56 return Vector<HCollider>(0);
57
58 Vector<HCollider> output;
59 for (auto& entry : raw)
60 {
61 if (entry == nullptr)
62 continue;
63
64 CCollider* component = (CCollider*)entry->_getOwner(PhysicsOwnerType::Component);
65 if (component == nullptr)
66 continue;
67
68 output.push_back(static_object_cast<CCollider>(component->getHandle()));
69 }
70
71 return output;
72 }
73
74 Vector<HCollider> PhysicsScene::boxOverlap(const AABox& box, const Quaternion& rotation, UINT64 layer) const
75 {
76 return rawToComponent(_boxOverlap(box, rotation, layer));
77 }
78
79 Vector<HCollider> PhysicsScene::sphereOverlap(const Sphere& sphere, UINT64 layer) const
80 {
81 return rawToComponent(_sphereOverlap(sphere, layer));
82 }
83
84 Vector<HCollider> PhysicsScene::capsuleOverlap(const Capsule& capsule, const Quaternion& rotation, UINT64 layer) const
85 {
86 return rawToComponent(_capsuleOverlap(capsule, rotation, layer));
87 }
88
89 Vector<HCollider> PhysicsScene::convexOverlap(const HPhysicsMesh& mesh, const Vector3& position,
90 const Quaternion& rotation, UINT64 layer) const
91 {
92 return rawToComponent(_convexOverlap(mesh, position, rotation, layer));
93 }
94
95 Physics& gPhysics()
96 {
97 return Physics::instance();
98 }
99}