]> git.tdb.fi Git - libs/game.git/blobdiff - examples/bassteroids/source/physics.cpp
Add rotation to physics simulation
[libs/game.git] / examples / bassteroids / source / physics.cpp
index db8e494f4d4f179aa8e02eddad9b909f53e9a951..a8d49ec4d0331cb98437c07a24264a6c724b88a8 100644 (file)
@@ -75,7 +75,9 @@ void Physics::copy_in(SimulatedEntity &entity)
        {
                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.velocity = body->get_velocity();
+               entity.angular_velocity = body->get_angular_velocity();
        }
 }
 
@@ -90,6 +92,7 @@ void Physics::copy_out(SimulatedEntity &entity)
        {
                Game::Handle<RigidBody> body = entity.entity->get_body();
                body->set_velocity(entity.velocity);
+               body->set_angular_velocity(entity.angular_velocity);
        }
 }
 
@@ -102,6 +105,10 @@ void Physics::step(float dt_secs)
                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;
        }
 }