]> git.tdb.fi Git - libs/game.git/blob - examples/bassteroids/source/rigidbody.cpp
Allow external forces and torques to be added on rigid bodies
[libs/game.git] / examples / bassteroids / source / rigidbody.cpp
1 #include "rigidbody.h"
2 #include <msp/game/entity.h>
3 #include <msp/game/transform.h>
4
5 using namespace Msp;
6
7 RigidBody::RigidBody(Game::Handle<Game::Entity> e, const Setup &s):
8         Component(e),
9         setup(s)
10 { }
11
12 void RigidBody::set_velocity(const LinAl::Vector<float, 2> &v)
13 {
14         velocity = v;
15 }
16
17 void RigidBody::set_angular_velocity(Geometry::Angle<float> as)
18 {
19         angular_velocity = as;
20 }
21
22 void RigidBody::add_force(const LinAl::Vector<float, 2> &f)
23 {
24         force += f;
25 }
26
27 void RigidBody::add_force(const LinAl::Vector<float, 2> &f, const LinAl::Vector<float, 2> &p)
28 {
29         force += f;
30         LinAl::Vector<float, 2> r = p-entity->get_transform()->get_position().slice<2>(0);
31         torque += r.x*f.y-r.y*f.x;
32 }
33
34 void RigidBody::add_torque(float t)
35 {
36         torque += t;
37 }
38
39 void RigidBody::clear_forces()
40 {
41         force = LinAl::Vector<float, 2>();
42         torque = 0.0f;
43 }