]> git.localhorst.tv Git - gong.git/blobdiff - src/physics/sim.cpp
half-{complete,assed} sphere collision detection
[gong.git] / src / physics / sim.cpp
index 568d28cdefb6fc1b18c037bc6efbe1135e4f3bd8..68cc3e04ebd3719b8097ef394c64a4156a65c619 100644 (file)
@@ -1,8 +1,11 @@
+#include "Contact.hpp"
 #include "Derivative.hpp"
 #include "Object.hpp"
 #include "Simulation.hpp"
 #include "State.hpp"
 
+#include "../geometry/primitive.hpp"
+
 
 namespace gong {
 namespace physics {
@@ -29,12 +32,33 @@ Derivative::Derivative(
 }
 
 
+void Object::TestCollision(Object &other, std::vector<Contact> &contacts) {
+       if (!Intersection(bounds, other.bounds)) {
+               return;
+       }
+       // TODO: actual object shape intersection test
+       ReverseCollisionTest(other, contacts);
+}
+
+
 void Simulation::Update(float dt) {
        // integrate state
        for (Object *o : objects) {
                Integrate(o->state, dt);
        }
-       // TODO: detect collisions
+
+       // detect collisions
+       contacts.clear();
+       if (objects.size() < 2) {
+               // we don't handle self-collision, so nothing to do
+               return;
+       }
+       for (std::size_t i = 0; i < objects.size() - 1; ++i) {
+               for (std::size_t j = i + 1; j < objects.size(); ++j) {
+                       objects[i]->TestCollision(*objects[j], contacts);
+               }
+       }
+
        // TODO: resolve collisions
 }