4 #include <msp/game/eventobserver.h>
5 #include <msp/game/events.h>
6 #include <msp/game/handle.h>
7 #include <msp/game/system.h>
8 #include <msp/geometry/angle.h>
9 #include <msp/linal/vector.h>
13 class Physics: public Msp::Game::System
16 struct SimulatedEntity
18 Msp::Game::Handle<PhysicalEntity> entity;
19 float inverse_mass = 1.0f;
20 float moment_of_inertia = 1.0f;
21 Msp::LinAl::Vector<float, 2> external_force;
23 Msp::LinAl::Vector<float, 2> position;
24 Msp::Geometry::Angle<float> rotation;
25 Msp::LinAl::Vector<float, 2> velocity;
26 Msp::Geometry::Angle<float> angular_velocity;
28 unsigned collision_count;
29 Msp::LinAl::Vector<float, 2> position_adjust;
34 std::uint16_t body1 = 0;
35 std::uint16_t body2 = 0;
36 Msp::LinAl::Vector<float, 2> point;
38 Msp::LinAl::Vector<float, 2> normal;
41 Msp::Game::EventObserver observer;
42 std::vector<SimulatedEntity> entities;
43 unsigned fixture_count = 0;
44 std::vector<Collision> collisions;
47 Physics(Msp::Game::Stage &);
50 void entity_added(const Msp::Game::Events::EntityCreated &);
53 void tick(Msp::Time::TimeDelta) override;
57 void copy_in(SimulatedEntity &);
60 void copy_out(SimulatedEntity &);
63 void detect_collisions();
64 void solve_collisions();
65 void apply_impulses();
67 Collision &get_collision(unsigned, unsigned);
68 void collide_circle_circle(unsigned, unsigned);
69 void collide_circle_box(unsigned, unsigned);