4 #include <msp/game/eventobserver.h>
5 #include <msp/game/eventsource.h>
6 #include <msp/game/events.h>
7 #include <msp/game/handle.h>
8 #include <msp/game/system.h>
9 #include <msp/geometry/angle.h>
10 #include <msp/linal/vector.h>
15 class Physics: public Msp::Game::System
18 using EventSource = Msp::Game::EventSource<Events::Collision>;
21 struct SimulatedEntity
23 Msp::Game::Handle<PhysicalEntity> entity;
24 float inverse_mass = 1.0f;
25 float inverse_momi = 1.0f;
26 Msp::LinAl::Vector<float, 2> external_force;
27 float external_torque = 0.0f;
29 Msp::LinAl::Vector<float, 2> position;
30 Msp::Geometry::Angle<float> rotation;
31 Msp::LinAl::Vector<float, 2> velocity;
32 Msp::Geometry::Angle<float> angular_velocity;
34 unsigned collision_count;
35 Msp::LinAl::Vector<float, 2> position_adjust;
40 std::uint16_t body1 = 0;
41 std::uint16_t body2 = 0;
42 Msp::LinAl::Vector<float, 2> point;
44 Msp::LinAl::Vector<float, 2> normal;
47 EventSource event_source;
48 Msp::Game::EventObserver observer;
49 std::vector<SimulatedEntity> entities;
50 unsigned fixture_count = 0;
51 std::vector<Collision> collisions;
54 Physics(Msp::Game::Stage &);
57 void entity_added(const Msp::Game::Events::EntityCreated &);
58 void entity_removed(const Msp::Game::Events::EntityDestroyed &);
61 void tick(Msp::Time::TimeDelta) override;
65 void copy_in(SimulatedEntity &);
68 void copy_out(SimulatedEntity &);
71 void detect_collisions();
72 void solve_collisions();
73 void apply_impulses();
75 Collision &get_collision(unsigned, unsigned);
76 void collide_circle_circle(unsigned, unsigned);
77 void collide_circle_box(unsigned, unsigned);