X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=examples%2Fbassteroids%2Fsource%2Fphysics.cpp;h=0fb43260f4cff7b82f57e39eb6fed6aa72b3b954;hb=da195e0fa114b82708d7c2bbd6297590f34f79a4;hp=5d4c242233da509062fecc093b14aca7d0494003;hpb=cf97839fff9ecb0783cecc9510994fb4a13f2dbe;p=libs%2Fgame.git diff --git a/examples/bassteroids/source/physics.cpp b/examples/bassteroids/source/physics.cpp index 5d4c242..0fb4326 100644 --- a/examples/bassteroids/source/physics.cpp +++ b/examples/bassteroids/source/physics.cpp @@ -11,6 +11,8 @@ Physics::Physics(Game::Stage &s): observer(stage.get_event_bus()) { observer.observe([this](auto &e){ entity_added(e); }); + + stage.synthesize_initial_events(observer); } void Physics::entity_added(const Game::Events::EntityCreated &e) @@ -73,7 +75,11 @@ void Physics::copy_in(SimulatedEntity &entity) { Game::Handle 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(); } } @@ -88,6 +94,8 @@ void Physics::copy_out(SimulatedEntity &entity) { Game::Handle body = entity.entity->get_body(); body->set_velocity(entity.velocity); + body->set_angular_velocity(entity.angular_velocity); + body->clear_forces(); } } @@ -100,6 +108,10 @@ void Physics::step(float dt_secs) LinAl::Vector 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 new_angular_velocity = entity.angular_velocity+Geometry::Angle::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; } }