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 inverse_momi = 1.0f;
21 Msp::LinAl::Vector<float, 2> external_force;
22 float external_torque = 0.0f;
24 Msp::LinAl::Vector<float, 2> position;
25 Msp::Geometry::Angle<float> rotation;
26 Msp::LinAl::Vector<float, 2> velocity;
27 Msp::Geometry::Angle<float> angular_velocity;
29 unsigned collision_count;
30 Msp::LinAl::Vector<float, 2> position_adjust;
35 std::uint16_t body1 = 0;
36 std::uint16_t body2 = 0;
37 Msp::LinAl::Vector<float, 2> point;
39 Msp::LinAl::Vector<float, 2> normal;
42 Msp::Game::EventObserver observer;
43 std::vector<SimulatedEntity> entities;
44 unsigned fixture_count = 0;
45 std::vector<Collision> collisions;
48 Physics(Msp::Game::Stage &);
51 void entity_added(const Msp::Game::Events::EntityCreated &);
54 void tick(Msp::Time::TimeDelta) override;
58 void copy_in(SimulatedEntity &);
61 void copy_out(SimulatedEntity &);
64 void detect_collisions();
65 void solve_collisions();
66 void apply_impulses();
68 Collision &get_collision(unsigned, unsigned);
69 void collide_circle_circle(unsigned, unsigned);
70 void collide_circle_box(unsigned, unsigned);