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 Msp::LinAl::Vector<float, 2> external_force;
22 Msp::LinAl::Vector<float, 2> position;
23 Msp::Geometry::Angle<float> rotation;
24 Msp::LinAl::Vector<float, 2> velocity;
26 unsigned collision_count;
27 Msp::LinAl::Vector<float, 2> position_adjust;
32 std::uint16_t body1 = 0;
33 std::uint16_t body2 = 0;
34 Msp::LinAl::Vector<float, 2> point;
36 Msp::LinAl::Vector<float, 2> normal;
39 Msp::Game::EventObserver observer;
40 std::vector<SimulatedEntity> entities;
41 unsigned fixture_count = 0;
42 std::vector<Collision> collisions;
45 Physics(Msp::Game::Stage &);
48 void entity_added(const Msp::Game::Events::EntityCreated &);
51 void tick(Msp::Time::TimeDelta) override;
55 void copy_in(SimulatedEntity &);
58 void copy_out(SimulatedEntity &);
61 void detect_collisions();
62 void solve_collisions();
63 void apply_impulses();
65 Collision &get_collision(unsigned, unsigned);
66 void collide_circle_circle(unsigned, unsigned);
67 void collide_circle_box(unsigned, unsigned);