summaryrefslogtreecommitdiffstats
path: root/include/core/Vector3.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'include/core/Vector3.hpp')
-rw-r--r--include/core/Vector3.hpp263
1 files changed, 202 insertions, 61 deletions
diff --git a/include/core/Vector3.hpp b/include/core/Vector3.hpp
index df1d60c..2d78f21 100644
--- a/include/core/Vector3.hpp
+++ b/include/core/Vector3.hpp
@@ -1,10 +1,14 @@
#ifndef VECTOR3_H
#define VECTOR3_H
+#include <gdnative/vector3.h>
+
#include "Defs.hpp"
#include "String.hpp"
+#include <cmath>
+
namespace godot {
class Basis;
@@ -24,80 +28,192 @@ struct Vector3 {
real_t z;
};
- real_t coord[3];
+ real_t coord[3]; // Not for direct access, use [] operator instead
};
- Vector3(real_t x, real_t y, real_t z);
-
- Vector3();
-
- const real_t &operator[](int p_axis) const;
-
- real_t &operator[](int p_axis);
-
- Vector3 &operator+=(const Vector3 &p_v);
-
- Vector3 operator+(const Vector3 &p_v) const;
-
- Vector3 &operator-=(const Vector3 &p_v);
-
- Vector3 operator-(const Vector3 &p_v) const;
-
- Vector3 &operator*=(const Vector3 &p_v);
-
- Vector3 operator*(const Vector3 &p_v) const;
-
- Vector3 &operator/=(const Vector3 &p_v);
-
- Vector3 operator/(const Vector3 &p_v) const;
-
- Vector3 &operator*=(real_t p_scalar);
-
- Vector3 operator*(real_t p_scalar) const;
-
- Vector3 &operator/=(real_t p_scalar);
-
- Vector3 operator/(real_t p_scalar) const;
-
- Vector3 operator-() const;
-
- bool operator==(const Vector3 &p_v) const;
-
- bool operator!=(const Vector3 &p_v) const;
+ inline Vector3(real_t x, real_t y, real_t z) {
+ this->x = x;
+ this->y = y;
+ this->z = z;
+ }
+
+ inline Vector3() {
+ this->x = 0;
+ this->y = 0;
+ this->z = 0;
+ }
+
+ inline const real_t &operator[](int p_axis) const {
+ return coord[p_axis];
+ }
+
+ inline real_t &operator[](int p_axis) {
+ return coord[p_axis];
+ }
+
+ inline Vector3 &operator+=(const Vector3 &p_v) {
+ x += p_v.x;
+ y += p_v.y;
+ z += p_v.z;
+ return *this;
+ }
+
+ inline Vector3 operator+(const Vector3 &p_v) const {
+ Vector3 v = *this;
+ v += p_v;
+ return v;
+ }
+
+ inline Vector3 &operator-=(const Vector3 &p_v) {
+ x -= p_v.x;
+ y -= p_v.y;
+ z -= p_v.z;
+ return *this;
+ }
+
+ inline Vector3 operator-(const Vector3 &p_v) const {
+ Vector3 v = *this;
+ v -= p_v;
+ return v;
+ }
+
+ inline Vector3 &operator*=(const Vector3 &p_v) {
+ x *= p_v.x;
+ y *= p_v.y;
+ z *= p_v.z;
+ return *this;
+ }
+
+ inline Vector3 operator*(const Vector3 &p_v) const {
+ Vector3 v = *this;
+ v *= p_v;
+ return v;
+ }
+
+ inline Vector3 &operator/=(const Vector3 &p_v) {
+ x /= p_v.x;
+ y /= p_v.y;
+ z /= p_v.z;
+ return *this;
+ }
+
+ inline Vector3 operator/(const Vector3 &p_v) const {
+ Vector3 v = *this;
+ v /= p_v;
+ return v;
+ }
+
+ inline Vector3 &operator*=(real_t p_scalar) {
+ *this *= Vector3(p_scalar, p_scalar, p_scalar);
+ return *this;
+ }
+
+ inline Vector3 operator*(real_t p_scalar) const {
+ Vector3 v = *this;
+ v *= p_scalar;
+ return v;
+ }
+
+ inline Vector3 &operator/=(real_t p_scalar) {
+ *this /= Vector3(p_scalar, p_scalar, p_scalar);
+ return *this;
+ }
+
+ inline Vector3 operator/(real_t p_scalar) const {
+ Vector3 v = *this;
+ v /= p_scalar;
+ return v;
+ }
+
+ inline Vector3 operator-() const {
+ return Vector3(-x, -y, -z);
+ }
+
+ inline bool operator==(const Vector3 &p_v) const {
+ return (x == p_v.x && y == p_v.y && z == p_v.z);
+ }
+
+ inline bool operator!=(const Vector3 &p_v) const {
+ return (x != p_v.x || y != p_v.y || z != p_v.z);
+ }
bool operator<(const Vector3 &p_v) const;
bool operator<=(const Vector3 &p_v) const;
- Vector3 abs() const;
+ inline Vector3 abs() const {
+ return Vector3(::fabs(x), ::fabs(y), ::fabs(z));
+ }
- Vector3 ceil() const;
+ inline Vector3 ceil() const {
+ return Vector3(::ceil(x), ::ceil(y), ::ceil(z));
+ }
- Vector3 cross(const Vector3 &b) const;
+ inline Vector3 cross(const Vector3 &b) const {
+ Vector3 ret(
+ (y * b.z) - (z * b.y),
+ (z * b.x) - (x * b.z),
+ (x * b.y) - (y * b.x));
- Vector3 linear_interpolate(const Vector3 &p_b, real_t p_t) const;
+ return ret;
+ }
+
+ inline Vector3 linear_interpolate(const Vector3 &p_b, real_t p_t) const {
+ return Vector3(
+ x + (p_t * (p_b.x - x)),
+ y + (p_t * (p_b.y - y)),
+ z + (p_t * (p_b.z - z)));
+ }
Vector3 cubic_interpolate(const Vector3 &b, const Vector3 &pre_a, const Vector3 &post_b, const real_t t) const;
- Vector3 bounce(const Vector3 &p_normal) const;
+ Vector3 bounce(const Vector3 &p_normal) const {
+ return -reflect(p_normal);
+ }
+
+ inline real_t length() const {
+ real_t x2 = x * x;
+ real_t y2 = y * y;
+ real_t z2 = z * z;
+
+ return ::sqrt(x2 + y2 + z2);
+ }
- real_t length() const;
+ inline real_t length_squared() const {
+ real_t x2 = x * x;
+ real_t y2 = y * y;
+ real_t z2 = z * z;
- real_t length_squared() const;
+ return x2 + y2 + z2;
+ }
- real_t distance_squared_to(const Vector3 &b) const;
+ inline real_t distance_squared_to(const Vector3 &b) const {
+ return (b - *this).length_squared();
+ }
- real_t distance_to(const Vector3 &b) const;
+ inline real_t distance_to(const Vector3 &b) const {
+ return (b - *this).length();
+ }
- real_t dot(const Vector3 &b) const;
+ inline real_t dot(const Vector3 &b) const {
+ return x * b.x + y * b.y + z * b.z;
+ }
- real_t angle_to(const Vector3 &b) const;
+ inline real_t angle_to(const Vector3 &b) const {
+ return std::atan2(cross(b).length(), dot(b));
+ }
- Vector3 floor() const;
+ inline Vector3 floor() const {
+ return Vector3(::floor(x), ::floor(y), ::floor(z));
+ }
- Vector3 inverse() const;
+ inline Vector3 inverse() const {
+ return Vector3(1.f / x, 1.f / y, 1.f / z);
+ }
- bool is_normalized() const;
+ inline bool is_normalized() const {
+ return std::abs(length_squared() - 1.f) < 0.00001f;
+ }
Basis outer(const Vector3 &b) const;
@@ -105,21 +221,46 @@ struct Vector3 {
int min_axis() const;
- void normalize();
-
- Vector3 normalized() const;
-
- Vector3 reflect(const Vector3 &by) const;
-
- Vector3 rotated(const Vector3 &axis, const real_t phi) const;
+ inline void normalize() {
+ real_t l = length();
+ if (l == 0) {
+ x = y = z = 0;
+ } else {
+ x /= l;
+ y /= l;
+ z /= l;
+ }
+ }
+
+ inline Vector3 normalized() const {
+ Vector3 v = *this;
+ v.normalize();
+ return v;
+ }
+
+ inline Vector3 reflect(const Vector3 &by) const {
+ return by - *this * this->dot(by) * 2.f;
+ }
+
+ inline Vector3 rotated(const Vector3 &axis, const real_t phi) const {
+ Vector3 v = *this;
+ v.rotate(axis, phi);
+ return v;
+ }
void rotate(const Vector3 &p_axis, real_t p_phi);
- Vector3 slide(const Vector3 &by) const;
+ inline Vector3 slide(const Vector3 &by) const {
+ return by - *this * this->dot(by);
+ }
void snap(real_t p_val);
- Vector3 snapped(const float by);
+ inline Vector3 snapped(const float by) {
+ Vector3 v = *this;
+ v.snap(by);
+ return v;
+ }
operator String() const;
};