]> git.localhorst.tv Git - space.git/blob - src/entity/Ship.h
added autopilot that sucks
[space.git] / src / entity / Ship.h
1 #ifndef SPACE_SHIP_H_
2 #define SPACE_SHIP_H_
3
4 #include "../graphics/const.h"
5 #include "../graphics/Vector.h"
6
7 #include <cmath>
8
9
10 namespace space {
11
12 class Ship {
13
14 public:
15         constexpr Ship() { }
16
17 public:
18         float mass = 1;
19         float linForce = 1;
20         float revForce = 1;
21         float rotForce = 1;
22
23         Vector<float> pos;
24         Vector<float> vel;
25         Vector<float> Dir() const {
26                 return Vector<float>::FromPolar(1, orient);
27         }
28         float MaxFwdAcc() const {
29                 return linForce / mass;
30         }
31         float MaxRevAcc() const {
32                 return revForce / mass;
33         }
34         float MaxLinAcc() const {
35                 return (linThrottle < 0 ? MaxRevAcc() : MaxFwdAcc());
36         }
37         Vector<float> Acc() const {
38                 return Dir() * MaxLinAcc() * linThrottle;
39         }
40
41         float orient = 0;
42         float rotVel = 0;
43         float MaxRotAcc() const {
44                 return rotForce / mass;
45         }
46         float RotAcc() const {
47                 return MaxRotAcc() * rotThrottle;
48         }
49
50         float linThrottle = 0;
51         float rotThrottle = 0;
52
53 public:
54         void Update(float delta) {
55                 rotVel += RotAcc() * delta;
56                 orient += rotVel * delta;
57                 while (orient < 0) orient += PI2;
58                 while (orient > PI2) orient -= PI2;
59                 vel += Acc() * delta;
60                 pos += vel * delta;
61         }
62
63 };
64
65 }
66
67 #endif