]> git.localhorst.tv Git - space.git/blobdiff - src/entity/Ship.h
added autopilot that sucks
[space.git] / src / entity / Ship.h
index 8bcc4bcb6dfecb1f41ea4b658cfe4f6d3f0feeac..a373302904d760597e11228c7f7753586ea2ceba 100644 (file)
@@ -1,12 +1,64 @@
 #ifndef SPACE_SHIP_H_
 #define SPACE_SHIP_H_
 
+#include "../graphics/const.h"
+#include "../graphics/Vector.h"
+
+#include <cmath>
+
 
 namespace space {
 
 class Ship {
 
-private:
+public:
+       constexpr Ship() { }
+
+public:
+       float mass = 1;
+       float linForce = 1;
+       float revForce = 1;
+       float rotForce = 1;
+
+       Vector<float> pos;
+       Vector<float> vel;
+       Vector<float> Dir() const {
+               return Vector<float>::FromPolar(1, orient);
+       }
+       float MaxFwdAcc() const {
+               return linForce / mass;
+       }
+       float MaxRevAcc() const {
+               return revForce / mass;
+       }
+       float MaxLinAcc() const {
+               return (linThrottle < 0 ? MaxRevAcc() : MaxFwdAcc());
+       }
+       Vector<float> Acc() const {
+               return Dir() * MaxLinAcc() * linThrottle;
+       }
+
+       float orient = 0;
+       float rotVel = 0;
+       float MaxRotAcc() const {
+               return rotForce / mass;
+       }
+       float RotAcc() const {
+               return MaxRotAcc() * rotThrottle;
+       }
+
+       float linThrottle = 0;
+       float rotThrottle = 0;
+
+public:
+       void Update(float delta) {
+               rotVel += RotAcc() * delta;
+               orient += rotVel * delta;
+               while (orient < 0) orient += PI2;
+               while (orient > PI2) orient -= PI2;
+               vel += Acc() * delta;
+               pos += vel * delta;
+       }
 
 };