From 04637d86974dafa5cca471a29e8b763d3a7e0cff Mon Sep 17 00:00:00 2001 From: lupoDharkael Date: Thu, 18 Oct 2018 18:32:38 +0200 Subject: [PATCH] Vector3: add missing methods --- include/core/Vector3.hpp | 7 +++++++ src/core/Vector3.cpp | 26 ++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/include/core/Vector3.hpp b/include/core/Vector3.hpp index fa7dcf4..c466474 100644 --- a/include/core/Vector3.hpp +++ b/include/core/Vector3.hpp @@ -7,6 +7,7 @@ namespace godot { +class Basis; struct Vector3 { @@ -79,6 +80,8 @@ struct Vector3 { 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; + real_t length() const; real_t length_squared() const; @@ -89,11 +92,15 @@ struct Vector3 { real_t dot(const Vector3& b) const; + real_t angle_to(const Vector3& b) const; + Vector3 floor() const; Vector3 inverse() const; + bool is_normalized() const; + Basis outer(const Vector3& b) const; int max_axis() const; diff --git a/src/core/Vector3.cpp b/src/core/Vector3.cpp index a22bdc0..aac34b5 100644 --- a/src/core/Vector3.cpp +++ b/src/core/Vector3.cpp @@ -2,12 +2,12 @@ #include "String.hpp" +#include "Basis.hpp" + #include #include -#include "Basis.hpp" - namespace godot { @@ -209,6 +209,11 @@ Vector3 Vector3::cubic_interpolate(const Vector3& b, const Vector3& pre_a, const return out; } +Vector3 Vector3::bounce(const Vector3& p_normal) const +{ + return -reflect(p_normal); +} + real_t Vector3::length() const { real_t x2=x*x; @@ -242,6 +247,11 @@ real_t Vector3::dot(const Vector3& b) const return x*b.x + y*b.y + z*b.z; } +real_t Vector3::angle_to(const Vector3& b) const +{ + return std::atan2(cross(b).length(), dot(b)); +} + Vector3 Vector3::floor() const { return Vector3(::floor(x), ::floor(y), ::floor(z)); @@ -252,7 +262,19 @@ Vector3 Vector3::inverse() const return Vector3( 1.0/x, 1.0/y, 1.0/z ); } +bool Vector3::is_normalized() const +{ + return std::abs(length_squared() - 1.0) < 0.00001; +} +Basis Vector3::outer(const Vector3& b) const +{ + Vector3 row0(x * b.x, x * b.y, x * b.z); + Vector3 row1(y * b.x, y * b.y, y * b.z); + Vector3 row2(z * b.x, z * b.y, z * b.z); + + return Basis(row0, row1, row2); +} int Vector3::max_axis() const