From: Mikko Rasa Date: Sun, 20 Nov 2022 19:22:40 +0000 (+0200) Subject: Allow external forces and torques to be added on rigid bodies X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=da195e0fa114b82708d7c2bbd6297590f34f79a4;p=libs%2Fgame.git Allow external forces and torques to be added on rigid bodies --- diff --git a/examples/bassteroids/source/physics.cpp b/examples/bassteroids/source/physics.cpp index a8d49ec..0fb4326 100644 --- a/examples/bassteroids/source/physics.cpp +++ b/examples/bassteroids/source/physics.cpp @@ -76,6 +76,8 @@ 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(); } @@ -93,6 +95,7 @@ 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(); } } diff --git a/examples/bassteroids/source/physics.h b/examples/bassteroids/source/physics.h index 3d9d708..078d2e6 100644 --- a/examples/bassteroids/source/physics.h +++ b/examples/bassteroids/source/physics.h @@ -19,6 +19,7 @@ private: float inverse_mass = 1.0f; float moment_of_inertia = 1.0f; Msp::LinAl::Vector external_force; + float external_torque = 0.0f; Msp::LinAl::Vector position; Msp::Geometry::Angle rotation; diff --git a/examples/bassteroids/source/rigidbody.cpp b/examples/bassteroids/source/rigidbody.cpp index 509e3a9..391707a 100644 --- a/examples/bassteroids/source/rigidbody.cpp +++ b/examples/bassteroids/source/rigidbody.cpp @@ -1,4 +1,6 @@ #include "rigidbody.h" +#include +#include using namespace Msp; @@ -16,3 +18,26 @@ void RigidBody::set_angular_velocity(Geometry::Angle as) { angular_velocity = as; } + +void RigidBody::add_force(const LinAl::Vector &f) +{ + force += f; +} + +void RigidBody::add_force(const LinAl::Vector &f, const LinAl::Vector &p) +{ + force += f; + LinAl::Vector 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(); + torque = 0.0f; +} diff --git a/examples/bassteroids/source/rigidbody.h b/examples/bassteroids/source/rigidbody.h index 6225500..aaffdc0 100644 --- a/examples/bassteroids/source/rigidbody.h +++ b/examples/bassteroids/source/rigidbody.h @@ -20,6 +20,8 @@ private: const Setup &setup; Msp::LinAl::Vector velocity; Msp::Geometry::Angle angular_velocity; + Msp::LinAl::Vector force; + float torque = 0.0f; public: RigidBody(Msp::Game::Handle, 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 &); void set_angular_velocity(Msp::Geometry::Angle); + void add_force(const Msp::LinAl::Vector &); + void add_force(const Msp::LinAl::Vector &, const Msp::LinAl::Vector &); + void add_torque(float); + void clear_forces(); const Msp::LinAl::Vector &get_velocity() const { return velocity; } Msp::Geometry::Angle get_angular_velocity() const { return angular_velocity; } + const Msp::LinAl::Vector &get_force() const { return force; } + float get_torque() const { return torque; } }; #endif