observer(stage.get_event_bus())
{
observer.observe<Game::Events::EntityCreated>([this](auto &e){ entity_added(e); });
+
+ stage.synthesize_initial_events(observer);
}
void Physics::entity_added(const Game::Events::EntityCreated &e)
{
Game::Handle<RigidBody> body = entity.entity->get_body();
entity.inverse_mass = 1.0f/body->get_mass();
+ entity.moment_of_inertia = body->get_moment_of_inertia();
+ entity.external_force = body->get_force();
+ entity.external_torque = body->get_torque();
entity.velocity = body->get_velocity();
+ entity.angular_velocity = body->get_angular_velocity();
}
}
{
Game::Handle<RigidBody> body = entity.entity->get_body();
body->set_velocity(entity.velocity);
+ body->set_angular_velocity(entity.angular_velocity);
+ body->clear_forces();
}
}
LinAl::Vector<float, 2> new_velocity = entity.velocity+entity.external_force*dt_secs*entity.inverse_mass;
entity.position += (entity.velocity+new_velocity)*(dt_secs/2);
entity.velocity = new_velocity;
+
+ Geometry::Angle<float> new_angular_velocity = entity.angular_velocity+Geometry::Angle<float>::from_radians(entity.external_torque*(dt_secs/entity.moment_of_inertia));
+ entity.rotation = wrap_positive(entity.rotation+(entity.angular_velocity+new_angular_velocity)*(dt_secs/2));
+ entity.angular_velocity = new_angular_velocity;
}
}