3 #include <msp/game/transform.h>
4 #include "physicalentity.h"
9 Physics::Physics(Game::Stage &s):
11 observer(stage.get_event_bus())
13 observer.observe<Game::Events::EntityCreated>([this](auto &e){ entity_added(e); });
15 stage.synthesize_initial_events(observer);
18 void Physics::entity_added(const Game::Events::EntityCreated &e)
20 if(Game::Handle<PhysicalEntity> physical = dynamic_handle_cast<PhysicalEntity>(e.entity))
22 for(Game::Handle<Game::Entity> p=e.entity->get_parent(); p; p=p->get_parent())
23 if(p->get_transform())
26 SimulatedEntity sim_body;
27 sim_body.entity = physical;
28 if(physical->is_fixture())
30 entities.insert(entities.begin()+fixture_count, sim_body);
34 entities.push_back(sim_body);
38 void Physics::tick(Time::TimeDelta dt)
40 float dt_secs = dt/Time::sec;
42 for(unsigned i=0; i<fixture_count; ++i)
43 copy_in<true>(entities[i]);
44 for(unsigned i=fixture_count; i<entities.size(); ++i)
45 copy_in<false>(entities[i]);
50 for(unsigned i=0; i<10; ++i)
58 for(unsigned i=0; i<fixture_count; ++i)
59 copy_out<true>(entities[i]);
60 for(unsigned i=fixture_count; i<entities.size(); ++i)
61 copy_out<false>(entities[i]);
64 template<bool is_fixture>
65 void Physics::copy_in(SimulatedEntity &entity)
67 Game::Handle<Game::Transform> transform = entity.entity->get_transform();
68 entity.position = transform->get_position().slice<2>(0);
69 const Geometry::Quaternion<float> &r = transform->get_rotation();
70 entity.rotation = Geometry::atan2<float>(2*(r.a*r.d+r.b*r.c), 1-2*(r.c*r.c+r.d*r.d));
72 if constexpr(is_fixture)
73 entity.inverse_mass = 0.0f;
76 Game::Handle<RigidBody> body = entity.entity->get_body();
77 entity.inverse_mass = 1.0f/body->get_mass();
78 entity.moment_of_inertia = body->get_moment_of_inertia();
79 entity.external_force = body->get_force();
80 entity.external_torque = body->get_torque();
81 entity.velocity = body->get_velocity();
82 entity.angular_velocity = body->get_angular_velocity();
86 template<bool is_fixture>
87 void Physics::copy_out(SimulatedEntity &entity)
89 Game::Handle<Game::Transform> transform = entity.entity->get_transform();
90 transform->set_position(compose(entity.position, 0.0f));
91 transform->set_rotation(Geometry::Quaternion<float>::rotation(entity.rotation, LinAl::Vector<float, 3>(0, 0, 1)));
93 if constexpr(!is_fixture)
95 Game::Handle<RigidBody> body = entity.entity->get_body();
96 body->set_velocity(entity.velocity);
97 body->set_angular_velocity(entity.angular_velocity);
102 void Physics::step(float dt_secs)
104 for(unsigned i=fixture_count; i<entities.size(); ++i)
106 SimulatedEntity &entity = entities[i];
108 LinAl::Vector<float, 2> new_velocity = entity.velocity+entity.external_force*dt_secs*entity.inverse_mass;
109 entity.position += (entity.velocity+new_velocity)*(dt_secs/2);
110 entity.velocity = new_velocity;
112 Geometry::Angle<float> new_angular_velocity = entity.angular_velocity+Geometry::Angle<float>::from_radians(entity.external_torque*(dt_secs/entity.moment_of_inertia));
113 entity.rotation = wrap_positive(entity.rotation+(entity.angular_velocity+new_angular_velocity)*(dt_secs/2));
114 entity.angular_velocity = new_angular_velocity;
118 void Physics::detect_collisions()
120 for(auto &c: collisions)
123 for(unsigned i=fixture_count; i<entities.size(); ++i)
125 Game::Handle<PhysicalEntity> entity1 = entities[i].entity;
126 ColliderType type1 = entity1->get_collider()->get_type();
127 for(unsigned j=0; j<i; ++j)
129 Game::Handle<PhysicalEntity> entity2 = entities[j].entity;
130 ColliderType type2 = entity2->get_collider()->get_type();
131 if(type1==ColliderType::CIRCLE && type2==ColliderType::CIRCLE)
132 collide_circle_circle(i, j);
133 else if(type1==ColliderType::CIRCLE && type2==ColliderType::BOX)
134 collide_circle_box(i, j);
135 else if(type1==ColliderType::BOX && type2==ColliderType::CIRCLE)
136 collide_circle_box(j, i);
141 void Physics::solve_collisions()
143 for(auto &e: entities)
145 e.position_adjust = LinAl::Vector<float, 2>();
146 e.collision_count = 0;
149 for(const auto &c: collisions)
154 SimulatedEntity &entity1 = entities[c.body1];
155 SimulatedEntity &entity2 = entities[c.body2];
156 float inv_mass_sum = 1.0f/(entity1.inverse_mass+entity2.inverse_mass);
157 LinAl::Vector<float, 2> delta = c.normal*c.depth*inv_mass_sum;
158 if(c.body1>=fixture_count)
160 entity1.position_adjust += delta*entity1.inverse_mass;
161 ++entity1.collision_count;
163 if(c.body2>=fixture_count)
165 entity2.position_adjust -= delta*entity1.inverse_mass;
166 ++entity2.collision_count;
170 for(auto &e: entities)
171 if(e.collision_count)
172 e.position += e.position_adjust/static_cast<float>(e.collision_count);
175 void Physics::apply_impulses()
177 for(const auto &c: collisions)
179 SimulatedEntity &entity1 = entities[c.body1];
180 SimulatedEntity &entity2 = entities[c.body2];
181 LinAl::Vector<float, 2> v_rel = entity2.velocity-entity1.velocity;
182 float restitution = 1.0f;
183 float inv_mass_sum = entity1.inverse_mass+entity2.inverse_mass;
184 float impulse = (1+restitution)*inner_product(v_rel, c.normal)/inv_mass_sum;
185 entity1.velocity += c.normal*(impulse*entity1.inverse_mass);
186 entity2.velocity -= c.normal*(impulse*entity2.inverse_mass);
190 Physics::Collision &Physics::get_collision(unsigned i, unsigned j)
192 for(auto &c: collisions)
193 if((c.body1==i && c.body2==j) || (c.body1==j && c.body2==i))
196 Collision &c = collisions.emplace_back();
202 void Physics::collide_circle_circle(unsigned i, unsigned j)
204 const LinAl::Vector<float, 2> &pos1 = entities[i].position;
205 const LinAl::Vector<float, 2> &pos2 = entities[j].position;
206 float r1 = entities[i].entity->get_collider()->get_radius();
207 float r2 = entities[j].entity->get_collider()->get_radius();
209 /* Points in the direction the first body needs to move in to clear the
211 LinAl::Vector<float, 2> delta = pos1-pos2;
212 float d_sq = inner_product(delta, delta);
216 Collision &collision = get_collision(i, j);
217 collision.normal = normalize(delta);
218 collision.depth = r1+r2-sqrt(d_sq);
219 collision.point = pos1-collision.normal*(r1-collision.depth/2);
220 if(collision.body1!=i)
221 collision.normal = -collision.normal;
225 void Physics::collide_circle_box(unsigned i, unsigned j)
227 const LinAl::Vector<float, 2> &pos1 = entities[i].position;
228 const LinAl::Vector<float, 2> &pos2 = entities[j].position;
229 float radius = entities[i].entity->get_collider()->get_radius();
230 LinAl::Vector<float, 2> half_size = entities[j].entity->get_collider()->get_size()/2.0f;
232 LinAl::Vector<float, 2> delta = pos1-pos2;
233 float c = cos(entities[j].rotation);
234 float s = sin(entities[j].rotation);
235 LinAl::Vector<float, 2> local_delta(c*delta.x+s*delta.y, c*delta.y-s*delta.x);
236 LinAl::Vector<float, 2> local_closest(clamp(local_delta.x, -half_size.x, half_size.x), clamp(local_delta.y, -half_size.y, half_size.y));
237 LinAl::Vector<float, 2> local_cdelta = local_delta-local_closest;
238 float d_sq = inner_product(local_cdelta, local_cdelta);
239 if(d_sq<radius*radius)
241 Collision &collision = get_collision(i, j);
242 collision.normal = normalize(LinAl::Vector<float, 2>(c*local_cdelta.x-s*local_cdelta.y, c*local_cdelta.y+s*local_cdelta.x));
243 collision.depth = radius-sqrt(d_sq);
244 collision.point = pos1-collision.normal*(radius-collision.depth/2);
245 if(collision.body1!=i)
246 collision.normal = -collision.normal;