]> git.tdb.fi Git - libs/game.git/commitdiff
Add rotation to physics simulation
authorMikko Rasa <tdb@tdb.fi>
Sun, 20 Nov 2022 19:10:32 +0000 (21:10 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 20 Nov 2022 19:10:32 +0000 (21:10 +0200)
There's no friction yet, so colliding objects don't start rotating.

examples/bassteroids/source/physics.cpp
examples/bassteroids/source/physics.h
examples/bassteroids/source/rigidbody.cpp
examples/bassteroids/source/rigidbody.h

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;
        }
 }
 
index fdeb8b2c82167577c217d835584872ea037a8cdc..3d9d7081e7f540e7c7edebcd7f71220fc2ee8d22 100644 (file)
@@ -17,11 +17,13 @@ private:
        {
                Msp::Game::Handle<PhysicalEntity> entity;
                float inverse_mass = 1.0f;
+               float moment_of_inertia = 1.0f;
                Msp::LinAl::Vector<float, 2> external_force;
 
                Msp::LinAl::Vector<float, 2> position;
                Msp::Geometry::Angle<float> rotation;
                Msp::LinAl::Vector<float, 2> velocity;
+               Msp::Geometry::Angle<float> angular_velocity;
 
                unsigned collision_count;
                Msp::LinAl::Vector<float, 2> position_adjust;
index c1d096e6a69817a2ba00299349810304b7f4a9f4..509e3a939cf0872ea4f2daa26e8f8184fe9c24cd 100644 (file)
@@ -11,3 +11,8 @@ void RigidBody::set_velocity(const LinAl::Vector<float, 2> &v)
 {
        velocity = v;
 }
+
+void RigidBody::set_angular_velocity(Geometry::Angle<float> as)
+{
+       angular_velocity = as;
+}
index 8a7a5b3568a6b75920366480687161c65360f95f..6225500237c79f69eb0367994ca4f363cd585c48 100644 (file)
@@ -8,6 +8,7 @@
 struct RigidBodySetup
 {
        float mass = 1.0f;
+       float moment_of_inertia = 0.5f;
 };
 
 class RigidBody: public Msp::Game::Component
@@ -18,13 +19,17 @@ public:
 private:
        const Setup &setup;
        Msp::LinAl::Vector<float, 2> velocity;
+       Msp::Geometry::Angle<float> angular_velocity;
 
 public:
        RigidBody(Msp::Game::Handle<Msp::Game::Entity>, const Setup &);
 
        float get_mass() const { return setup.mass; }
+       float get_moment_of_inertia() const { return setup.moment_of_inertia; }
        void set_velocity(const Msp::LinAl::Vector<float, 2> &);
+       void set_angular_velocity(Msp::Geometry::Angle<float>);
        const Msp::LinAl::Vector<float, 2> &get_velocity() const { return velocity; }
+       Msp::Geometry::Angle<float> get_angular_velocity() const { return angular_velocity; }
 };
 
 #endif