]> git.tdb.fi Git - libs/game.git/blob - examples/bassteroids/source/rigidbody.h
Convert components to buffered where appropriate
[libs/game.git] / examples / bassteroids / source / rigidbody.h
1 #ifndef RIGIDBODY_H_
2 #define RIGIDBODY_H_
3
4 #include <msp/game/component.h>
5 #include <msp/geometry/angle.h>
6 #include <msp/linal/vector.h>
7
8 struct RigidBodySetup
9 {
10         float mass = 1.0f;
11         float moment_of_inertia = 0.5f;
12 };
13
14 struct RigidBodyData
15 {
16         Msp::LinAl::Vector<float, 2> velocity;
17         Msp::Geometry::Angle<float> angular_velocity;
18         Msp::LinAl::Vector<float, 2> force;
19         float torque = 0.0f;
20 };
21
22 class RigidBody: public Msp::Game::BufferedComponent<RigidBodyData>
23 {
24 public:
25         using Setup = RigidBodySetup;
26
27 private:
28         const Setup &setup;
29
30 public:
31         RigidBody(Msp::Game::Handle<Msp::Game::Entity>, const Setup &);
32
33         float get_mass() const { return setup.mass; }
34         float get_moment_of_inertia() const { return setup.moment_of_inertia; }
35         void set_velocity(const Msp::LinAl::Vector<float, 2> &);
36         void set_angular_velocity(Msp::Geometry::Angle<float>);
37         void add_force(const Msp::LinAl::Vector<float, 2> &);
38         void add_force(const Msp::LinAl::Vector<float, 2> &, const Msp::LinAl::Vector<float, 2> &);
39         void add_torque(float);
40         void clear_forces();
41         const Msp::LinAl::Vector<float, 2> &get_velocity() const { return read().velocity; }
42         Msp::Geometry::Angle<float> get_angular_velocity() const { return read().angular_velocity; }
43         const Msp::LinAl::Vector<float, 2> &get_force() const { return read().force; }
44         float get_torque() const { return read().torque; }
45 };
46
47 #endif