6 m_gravity = XMVectorSet(0.0f, -9.81f, 0.0f, 0.0f);
9physics::physics(
const physics& other)
11 m_gravity = other.m_gravity;
19XMVECTOR physics::GetGravity()
const
25void physics::SetGravity(XMVECTOR gravity)
31void physics::ApplyGravity(
object*
object,
float dragValue)
33 if (
this ==
nullptr ||
object ==
nullptr)
41 XMVECTOR gravityAcceleration = m_gravity /
object->GetMass();
44 object->SetAcceleration(
object->GetAcceleration() + gravityAcceleration);
47 XMVECTOR dragAcceleration = -
object->GetVelocity() * dragValue /
object->GetMass();
50 object->SetAcceleration(
object->GetAcceleration() + dragAcceleration);
53 XMVECTOR velocity =
object->GetVelocity();
56 velocity +=
object->GetAcceleration();
59 object->SetVelocity(velocity);
63void physics::AddForce(
object*
object, XMVECTOR force)
65 if (
object ==
nullptr)
71 float mass =
object->GetMass();
74 XMVECTOR acceleration = force / mass;
77 object->SetAcceleration(
object->GetAcceleration() + acceleration);
80bool physics::IsColliding(
object* object1,
object* object2)
82 ObjectType type1 = object1->GetType();
83 ObjectType type2 = object2->GetType();
85 if (type1 == ObjectType::Unknown || type2 == ObjectType::Unknown)
90 if (type1 == ObjectType::Sphere && type2 == ObjectType::Sphere)
92 return SpheresOverlap(object1, object2);
94 if ((type1 == ObjectType::Cube && type2 == ObjectType::Sphere) ||
95 (type1 == ObjectType::Sphere && type2 == ObjectType::Cube))
97 if (type1 == ObjectType::Cube)
99 return SphereCubeOverlap(object1, object2);
101 else if (type1 == ObjectType::Sphere)
103 return SphereCubeOverlap(object2, object1);
108 return CubesOverlap(object1, object2);
119bool physics::CubesOverlap(
object* cube1,
object* cube2)
121 XMVECTOR position1 = cube1->GetPosition();
122 XMVECTOR position2 = cube2->GetPosition();
124 XMVECTOR scale1 = cube1->GetScale();
125 XMVECTOR scale2 = cube2->GetScale();
127 XMVECTOR min1 = position1 - scale1;
128 XMVECTOR max1 = position1 + scale1;
129 XMVECTOR min2 = position2 - scale2;
130 XMVECTOR max2 = position2 + scale2;
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]);
137bool physics::SpheresOverlap(
object* sphere1,
object* sphere2)
139 XMVECTOR position1 = sphere1->GetPosition();
140 XMVECTOR position2 = sphere2->GetPosition();
142 XMVECTOR scale1 = sphere1->GetScale() / 2;
143 XMVECTOR scale2 = sphere2->GetScale() / 2;
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])
151 float radius1 = XMVectorGetX(XMVector3Length(scale1));
152 float radius2 = XMVectorGetX(XMVector3Length(scale2));
154 return distance < radius1 + radius2;
157bool physics::SphereCubeOverlap(
object* cube,
object* sphere)
159 XMVECTOR position1 = cube->GetPosition();
160 XMVECTOR position2 = sphere->GetPosition();
162 XMVECTOR scale1 = cube->GetScale();
163 XMVECTOR scale2 = XMVectorScale(sphere->GetScale(), 0.5f);
165 XMVECTOR min1 = position1 - scale1;
166 XMVECTOR max1 = position1 + scale1;
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]));
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])
180 float radius = XMVectorGetX(XMVector3Length(scale2));
182 return distance < radius;