Skip to content

Commit 85d75b4

Browse files
committed
✨ Add star collision
1 parent bba727b commit 85d75b4

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

include/collision/collisionDetector.hpp

+18
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,22 @@ class CollisionDetector {
102102

103103
return result;
104104
}
105+
106+
static CollisionResult sphereToSphere(const glm::vec4 &sphere1Center, float sphere1Radius,
107+
const glm::vec4 &sphere2Center, float sphere2Radius) {
108+
CollisionResult result{false, glm::vec4(0.0f), 0};
109+
110+
const glm::vec4 distance = sphere1Center - sphere2Center;
111+
const float distSquared = math::dotProduct(distance, distance);
112+
113+
if (distSquared < (sphere1Radius + sphere2Radius) * (sphere1Radius + sphere2Radius)) {
114+
result.collided = true;
115+
const float dist = sqrt(distSquared);
116+
117+
result.penetrationDepth = sphere1Radius + sphere2Radius - dist;
118+
result.normal = math::normalize(distance);
119+
}
120+
121+
return result;
122+
}
105123
};

include/collision/sphereCollider.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ class SphereCollider : public Collider {
2424
otherOBB->getHalfWidths(), otherOBB->getRotationMatrix());
2525
}
2626

27+
if (const auto otherSphere = dynamic_cast<const SphereCollider *>(&other); otherSphere && transform) {
28+
result = CollisionDetector::sphereToSphere(getPosition(), getRadius(), otherSphere->getPosition(),
29+
otherSphere->getRadius());
30+
}
31+
2732
return result;
2833
}
2934

include/objects/player.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class Player final : public GameObject, public InputObserver {
106106
return;
107107
}
108108

109-
if (other.getObjectType() == ObjectType::DeathBox) {
109+
if (other.getObjectType() == ObjectType::DeathBox || other.getObjectType() == ObjectType::Star) {
110110
deathRoutine();
111111
return;
112112
}

include/objects/star.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Star : public GameObject {
2727

2828
addComponent(std::make_shared<LightEmitter>(color, 1.0f, 1.0e-2f, 5.0e-3f));
2929

30-
addComponent(std::make_shared<SphereCollider>(transform.scale.x / 2.0f));
30+
addComponent(std::make_shared<SphereCollider>(transform.scale.x));
3131
}
3232

3333
[[nodiscard]] ObjectType getObjectType() const override { return ObjectType::Star; }

0 commit comments

Comments
 (0)