Khaotic Engine Reborn
Loading...
Searching...
No Matches
physics.cpp
1#include "physics.h"
2
3
4physics::physics()
5{
6 m_gravity = XMVectorSet(0.0f, -9.81f, 0.0f, 0.0f); // initialize the gravity vector
7}
8
9physics::physics(const physics& other)
10{
11 m_gravity = other.m_gravity; // Copy the gravity value
12}
13
14physics::~physics()
15{
16}
17
18// Get the gravity value
19XMVECTOR physics::GetGravity() const
20{
21 return m_gravity;
22}
23
24// Define the gravity value
25void physics::SetGravity(XMVECTOR gravity)
26{
27 m_gravity = gravity;
28}
29
30// Apply gravity to an object
31void physics::ApplyGravity(object* object, float dragValue)
32{
33 if (this == nullptr || object == nullptr) // Verify if 'this' and 'object' are not null
34 {
35 return;
36 }
37
38 if (!object->IsGrounded()) // Verify if the object is grounded
39 {
40 // Calculate the acceleration caused by gravity
41 XMVECTOR gravityAcceleration = m_gravity / object->GetMass();
42
43 // Add the gravity acceleration to the object's current acceleration
44 object->SetAcceleration(object->GetAcceleration() + gravityAcceleration);
45
46 // Calculate the acceleration caused by drag
47 XMVECTOR dragAcceleration = -object->GetVelocity() * dragValue / object->GetMass();
48
49 // Add the drag acceleration to the object's current acceleration
50 object->SetAcceleration(object->GetAcceleration() + dragAcceleration);
51
52 // Get the object velocity
53 XMVECTOR velocity = object->GetVelocity();
54
55 // Update the velocity with the object's acceleration
56 velocity += object->GetAcceleration();
57
58 // Set the new velocity
59 object->SetVelocity(velocity);
60 }
61}
62
63void physics::AddForce(object* object, XMVECTOR force)
64{
65 if (object == nullptr) // Verify if the object is not null
66 {
67 return;
68 }
69
70 // Get the mass of the object
71 float mass = object->GetMass();
72
73 // Calculate the acceleration caused by the force
74 XMVECTOR acceleration = force / mass;
75
76 // Add the acceleration to the object's current acceleration
77 object->SetAcceleration(object->GetAcceleration() + acceleration);
78}
79
80bool physics::IsColliding(object* object1, object* object2)
81{
82 ObjectType type1 = object1->GetType();
83 ObjectType type2 = object2->GetType();
84
85 if (type1 == ObjectType::Unknown || type2 == ObjectType::Unknown)
86 {
87 return false;
88 }
89
90 if (type1 == ObjectType::Sphere && type2 == ObjectType::Sphere)
91 {
92 return SpheresOverlap(object1, object2);
93 }
94 if ((type1 == ObjectType::Cube && type2 == ObjectType::Sphere) ||
95 (type1 == ObjectType::Sphere && type2 == ObjectType::Cube))
96 {
97 if (type1 == ObjectType::Cube)
98 {
99 return SphereCubeOverlap(object1, object2);
100 }
101 else if (type1 == ObjectType::Sphere)
102 {
103 return SphereCubeOverlap(object2, object1);
104 }
105 }
106 else
107 {
108 return CubesOverlap(object1, object2);
109 }
110
111 return false;
112}
113
114
116// AABB method for collision detection //
118
119bool physics::CubesOverlap(object* cube1, object* cube2)
120{
121 XMVECTOR position1 = cube1->GetPosition();
122 XMVECTOR position2 = cube2->GetPosition();
123
124 XMVECTOR scale1 = cube1->GetScale();
125 XMVECTOR scale2 = cube2->GetScale();
126
127 XMVECTOR min1 = position1 - scale1;
128 XMVECTOR max1 = position1 + scale1;
129 XMVECTOR min2 = position2 - scale2;
130 XMVECTOR max2 = position2 + scale2;
131
132 return (min1.m128_f32[0] <= max2.m128_f32[0] && max1.m128_f32[0] >= min2.m128_f32[0] &&
133 min1.m128_f32[1] <= max2.m128_f32[1] && max1.m128_f32[1] >= min2.m128_f32[1] &&
134 min1.m128_f32[2] <= max2.m128_f32[2] && max1.m128_f32[2] >= min2.m128_f32[2]);
135}
136
137bool physics::SpheresOverlap(object* sphere1, object* sphere2)
138{
139 XMVECTOR position1 = sphere1->GetPosition();
140 XMVECTOR position2 = sphere2->GetPosition();
141
142 XMVECTOR scale1 = sphere1->GetScale() / 2;
143 XMVECTOR scale2 = sphere2->GetScale() / 2;
144
145 float distance = sqrt(
146 (position1.m128_f32[0] - position2.m128_f32[0]) * (position1.m128_f32[0] - position2.m128_f32[0]) +
147 (position1.m128_f32[1] - position2.m128_f32[1]) * (position1.m128_f32[1] - position2.m128_f32[1]) +
148 (position1.m128_f32[2] - position2.m128_f32[2]) * (position1.m128_f32[2] - position2.m128_f32[2])
149 );
150
151 float radius1 = XMVectorGetX(XMVector3Length(scale1));
152 float radius2 = XMVectorGetX(XMVector3Length(scale2));
153
154 return distance < radius1 + radius2;
155}
156
157bool physics::SphereCubeOverlap(object* cube, object* sphere)
158{
159 XMVECTOR position1 = cube->GetPosition();
160 XMVECTOR position2 = sphere->GetPosition();
161
162 XMVECTOR scale1 = cube->GetScale();
163 XMVECTOR scale2 = XMVectorScale(sphere->GetScale(), 0.5f);
164
165 XMVECTOR min1 = position1 - scale1;
166 XMVECTOR max1 = position1 + scale1;
167
168 // Get box closest point to sphere center by clamping
169 float x = max(min1.m128_f32[0], min(position2.m128_f32[0], max1.m128_f32[0]));
170 float y = max(min1.m128_f32[1], min(position2.m128_f32[1], max1.m128_f32[1]));
171 float z = max(min1.m128_f32[2], min(position2.m128_f32[2], max1.m128_f32[2]));
172
173 // This is the same as SpheresOverlap
174 float distance = sqrt(
175 (x - position2.m128_f32[0]) * (x - position2.m128_f32[0]) +
176 (y - position2.m128_f32[1]) * (y - position2.m128_f32[1]) +
177 (z - position2.m128_f32[2]) * (z - position2.m128_f32[2])
178 );
179
180 float radius = XMVectorGetX(XMVector3Length(scale2));
181
182 return distance < radius;
183}