]> git.tdb.fi Git - libs/game.git/commitdiff
Allow external forces and torques to be added on rigid bodies
authorMikko Rasa <tdb@tdb.fi>
Sun, 20 Nov 2022 19:22:40 +0000 (21:22 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 20 Nov 2022 19:22:40 +0000 (21:22 +0200)
examples/bassteroids/source/physics.cpp
examples/bassteroids/source/physics.h
examples/bassteroids/source/rigidbody.cpp
examples/bassteroids/source/rigidbody.h

index a8d49ec4d0331cb98437c07a24264a6c724b88a8..0fb43260f4cff7b82f57e39eb6fed6aa72b3b954 100644 (file)
@@ -76,6 +76,8 @@ 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.external_force = body->get_force();
+               entity.external_torque = body->get_torque();
                entity.velocity = body->get_velocity();
                entity.angular_velocity = body->get_angular_velocity();
        }
@@ -93,6 +95,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);
+               body->clear_forces();
        }
 }
 
index 3d9d7081e7f540e7c7edebcd7f71220fc2ee8d22..078d2e60aa31ac0c07f5b44ec11c7969a772a03e 100644 (file)
@@ -19,6 +19,7 @@ private:
                float inverse_mass = 1.0f;
                float moment_of_inertia = 1.0f;
                Msp::LinAl::Vector<float, 2> external_force;
+               float external_torque = 0.0f;
 
                Msp::LinAl::Vector<float, 2> position;
                Msp::Geometry::Angle<float> rotation;
index 509e3a939cf0872ea4f2daa26e8f8184fe9c24cd..391707a574ee40b2e8b36f8fd098961aab95ab4f 100644 (file)
@@ -1,4 +1,6 @@
 #include "rigidbody.h"
+#include <msp/game/entity.h>
+#include <msp/game/transform.h>
 
 using namespace Msp;
 
@@ -16,3 +18,26 @@ void RigidBody::set_angular_velocity(Geometry::Angle<float> as)
 {
        angular_velocity = as;
 }
+
+void RigidBody::add_force(const LinAl::Vector<float, 2> &f)
+{
+       force += f;
+}
+
+void RigidBody::add_force(const LinAl::Vector<float, 2> &f, const LinAl::Vector<float, 2> &p)
+{
+       force += f;
+       LinAl::Vector<float, 2> r = p-entity->get_transform()->get_position().slice<2>(0);
+       torque += r.x*f.y-r.y*f.x;
+}
+
+void RigidBody::add_torque(float t)
+{
+       torque += t;
+}
+
+void RigidBody::clear_forces()
+{
+       force = LinAl::Vector<float, 2>();
+       torque = 0.0f;
+}
index 6225500237c79f69eb0367994ca4f363cd585c48..aaffdc03c19eec0f18d131d88dd773d6aa8633d6 100644 (file)
@@ -20,6 +20,8 @@ private:
        const Setup &setup;
        Msp::LinAl::Vector<float, 2> velocity;
        Msp::Geometry::Angle<float> angular_velocity;
+       Msp::LinAl::Vector<float, 2> force;
+       float torque = 0.0f;
 
 public:
        RigidBody(Msp::Game::Handle<Msp::Game::Entity>, const Setup &);
@@ -28,8 +30,14 @@ public:
        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>);
+       void add_force(const Msp::LinAl::Vector<float, 2> &);
+       void add_force(const Msp::LinAl::Vector<float, 2> &, const Msp::LinAl::Vector<float, 2> &);
+       void add_torque(float);
+       void clear_forces();
        const Msp::LinAl::Vector<float, 2> &get_velocity() const { return velocity; }
        Msp::Geometry::Angle<float> get_angular_velocity() const { return angular_velocity; }
+       const Msp::LinAl::Vector<float, 2> &get_force() const { return force; }
+       float get_torque() const { return torque; }
 };
 
 #endif