]> git.localhorst.tv Git - space.git/blobdiff - src/graphics/Vector.h
force based movement
[space.git] / src / graphics / Vector.h
index 34d829713e5dea735d339b0ed56cafdb3a409d11..992ba1eef6a8bbdc323b8f612eef14eb2471f7d7 100644 (file)
@@ -2,6 +2,7 @@
 #define SPACE_VECTOR_H_
 
 #include <algorithm>
+#include <cmath>
 #include <ostream>
 #include <SDL.h>
 
@@ -18,6 +19,12 @@ public:
        template<class Other>
        constexpr Vector(Vector<Other> other) : x(other.x), y(other.y) { }
 
+       static Vector<Scalar> FromPolar(Scalar rad, Scalar az) {
+               return Vector<Scalar>(rad * std::cos(az), rad * std::sin(az));
+       }
+
+       static constexpr Vector<Scalar> unit45 = Vector<Scalar>(-0.7071, 0.7071);
+
 public:
        Vector<Scalar> &operator +=(Vector<Scalar> other) {
                x += other.x;
@@ -43,6 +50,9 @@ public:
 
 };
 
+template<class T>
+constexpr Vector<T> Vector<T>::unit45;
+
 /// specialization with same layout as SDL_Point
 template<>
 class Vector<int>
@@ -132,6 +142,29 @@ constexpr bool operator !=(Vector<Scalar> lhs, Vector<Scalar> rhs) {
 }
 
 
+template<class Scalar>
+constexpr Scalar Dot(Vector<Scalar> lhs, Vector<Scalar> rhs) {
+       return (lhs.x * rhs.x) + (lhs.y * rhs.y);
+}
+template<class Scalar>
+constexpr Scalar Length(Vector<Scalar> v) {
+       return std::sqrt(Dot(v, v));
+}
+
+template<class Scalar>
+constexpr Vector<Scalar> Rotate90(Vector<Scalar> v) {
+       return Vector<Scalar>(-v.y, v.x);
+}
+template<class Scalar>
+constexpr Vector<Scalar> Rotate180(Vector<Scalar> v) {
+       return -v;
+}
+template<class Scalar>
+constexpr Vector<Scalar> Rotate270(Vector<Scalar> v) {
+       return Vector<Scalar>(v.y, -v.x);
+}
+
+
 template<class Scalar>
 inline std::ostream &operator <<(std::ostream &out, Vector<Scalar> v) {
        return out << '<' << v.x << ',' << v.y << '>';