There's no friction yet, so colliding objects don't start rotating.
{
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();
}
}
{
Game::Handle<RigidBody> body = entity.entity->get_body();
body->set_velocity(entity.velocity);
+ body->set_angular_velocity(entity.angular_velocity);
}
}
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;
}
}
{
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;
{
velocity = v;
}
+
+void RigidBody::set_angular_velocity(Geometry::Angle<float> as)
+{
+ angular_velocity = as;
+}
struct RigidBodySetup
{
float mass = 1.0f;
+ float moment_of_inertia = 0.5f;
};
class RigidBody: public Msp::Game::Component
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