-
Notifications
You must be signed in to change notification settings - Fork 0
/
RigidBody.cpp
84 lines (68 loc) · 2.14 KB
/
RigidBody.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "RigidBody.h"
RigidBody::RigidBody()
{
}
RigidBody::~RigidBody()
{
}
void RigidBody::updateInvInertia()
{
float w = getMesh().getScale()[0][0] * 2.0f;
float d = getMesh().getScale()[2][2] * 2.0f;
float h = getMesh().getScale()[1][1] * 2.0f;
glm::mat3 inertia = glm::mat3(0.0f);
inertia[0][0] = getMass() / 12.0f * (h * h + d * d);
inertia[1][1] = getMass() / 12.0f * (w * w + d * d);
inertia[2][2] = getMass() / 12.0f * (w * w + h * h);
m_invInertia = inverse(inertia);
}
IntersectData RigidBody::canCollideStatic()
{
if (m_collider.getType() == TYPE::OBB)
{
return m_collider.intersect(getOrientedBoxCollider(), ColliderTester::getPlaneCollider());
}
if (m_collider.getType() == TYPE::SPHERE)
{
return m_collider.intersect(getSphereCollider(), ColliderTester::getPlaneCollider());
}
else
{
std::cout << "Error: Some of these STATIC collisions are not implemented yet.\n";
return IntersectData();
}
}
IntersectData RigidBody::canCollideDynamic(RigidBody* other)
{
if (m_collider.getType() == TYPE::OBB && other->getCollider().getType() == TYPE::OBB)
{
return m_collider.FindCollisionFeatures(getOrientedBoxCollider(), other->getOrientedBoxCollider());
}
else if (m_collider.getType() == TYPE::OBB && other->getCollider().getType() == TYPE::SPHERE)
{
return m_collider.intersect(other->getSphereCollider(), getOrientedBoxCollider());
}
else if (m_collider.getType() == TYPE::SPHERE && other->getCollider().getType() == TYPE::SPHERE)
{
return m_collider.intersect(getSphereCollider(), other->getSphereCollider());
}
else if (m_collider.getType() == TYPE::SPHERE && other->getCollider().getType() == TYPE::OBB)
{
return m_collider.intersect(getSphereCollider(), other->getOrientedBoxCollider());
}
else
{
std::cout << "Error: Some of these DYNAMIC collisions are not implemented yet.\n";
return IntersectData();
}
}
OBB RigidBody::getOrientedBoxCollider()
{
auto halfEdgeLen = getScaleVec();
return OBB(getPos(), glm::mat3(getRotate()), halfEdgeLen);
}
BoundingSphere RigidBody::getSphereCollider()
{
auto R = (getScaleVec().x + getScaleVec().y + getScaleVec().z) / 3.0f;
return BoundingSphere(getPos(), R);
}