2 #include "Derivative.hpp"
4 #include "Simulation.hpp"
7 #include "../geometry/primitive.hpp"
13 Derivative::Derivative() noexcept
16 , dorient(1.0f, 0.0f, 0.0f, 0.0f)
21 Derivative::Derivative(
35 void Object::TestCollision(Object &other, std::vector<Contact> &contacts) {
36 if (!Intersection(bounds, other.bounds)) {
39 // TODO: actual object shape intersection test
40 ReverseCollisionTest(other, contacts);
44 void Simulation::Update(float dt) {
46 for (Object *o : objects) {
47 Integrate(o->state, dt);
52 if (objects.size() < 2) {
53 // we don't handle self-collision, so nothing to do
56 for (std::size_t i = 0; i < objects.size() - 1; ++i) {
57 for (std::size_t j = i + 1; j < objects.size(); ++j) {
58 objects[i]->TestCollision(*objects[j], contacts);
62 // TODO: resolve collisions
65 void Simulation::Integrate(State &s, float dt) {
66 Derivative a(Evaluate(s, 0.0f, Derivative()));
67 Derivative b(Evaluate(s, dt * 0.5f, a));
68 Derivative c(Evaluate(s, dt * 0.5f, b));
69 Derivative d(Evaluate(s, dt, c));
70 constexpr float by_six = 1.0f / 6.0f;
71 Derivative f((a + (2.0f * (b + c)) + d) * by_six);
75 Derivative Simulation::Evaluate(const State &s, float dt, const Derivative &d) {
76 // TODO: calculate linear and torque forces from list of raw forces
77 glm::vec3 force(gravity);
78 glm::vec3 torque(0.0f);
80 s.lin + d.dpos * dt * s.inverse_mass,
82 glm::normalize(s.Spin() + d.dorient * dt * s.inverse_inertia),
88 State &operator +=(State &s, const Derivative &d) noexcept {
91 s.orient *= d.dorient;