1 #include "Derivative.hpp"
3 #include "Simulation.hpp"
10 Derivative::Derivative() noexcept
13 , dorient(1.0f, 0.0f, 0.0f, 0.0f)
18 Derivative::Derivative(
32 void Simulation::Update(float dt) {
34 for (Object *o : objects) {
35 Integrate(o->state, dt);
37 // TODO: detect collisions
38 // TODO: resolve collisions
41 void Simulation::Integrate(State &s, float dt) {
42 Derivative a(Evaluate(s, 0.0f, Derivative()));
43 Derivative b(Evaluate(s, dt * 0.5f, a));
44 Derivative c(Evaluate(s, dt * 0.5f, b));
45 Derivative d(Evaluate(s, dt, c));
46 constexpr float by_six = 1.0f / 6.0f;
47 Derivative f((a + (2.0f * (b + c)) + d) * by_six);
51 Derivative Simulation::Evaluate(const State &s, float dt, const Derivative &d) {
52 // TODO: calculate linear and torque forces from list of raw forces
53 glm::vec3 force(gravity);
54 glm::vec3 torque(0.0f);
56 s.lin + d.dpos * dt * s.inverse_mass,
58 glm::normalize(s.Spin() + d.dorient * dt * s.inverse_inertia),
64 State &operator +=(State &s, const Derivative &d) noexcept {
67 s.orient *= d.dorient;