Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion core/math/basis.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
#include "core/math/vector3.h"

struct [[nodiscard]] Basis {
static const Basis FLIP_X;
static const Basis FLIP_Y;
static const Basis FLIP_Z;

Vector3 rows[3] = {
Vector3(1, 0, 0),
Vector3(0, 1, 0),
Expand Down Expand Up @@ -224,7 +228,7 @@ struct [[nodiscard]] Basis {

operator Quaternion() const { return get_quaternion(); }

static Basis looking_at(const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0), bool p_use_model_front = false);
static Basis looking_at(const Vector3 &p_target, const Vector3 &p_up = Vector3::UP, bool p_use_model_front = false);

Basis(const Quaternion &p_quaternion) { set_quaternion(p_quaternion); }
Basis(const Quaternion &p_quaternion, const Vector3 &p_scale) { set_quaternion_scale(p_quaternion, p_scale); }
Expand All @@ -247,6 +251,10 @@ struct [[nodiscard]] Basis {
void _set_diagonal(const Vector3 &p_diag);
};

inline constexpr Basis Basis::FLIP_X = { { -1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
inline constexpr Basis Basis::FLIP_Y = { { 1, 0, 0 }, { 0, -1, 0 }, { 0, 0, 1 } };
inline constexpr Basis Basis::FLIP_Z = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, -1 } };

constexpr bool Basis::operator==(const Basis &p_matrix) const {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
Expand Down
8 changes: 8 additions & 0 deletions core/math/plane.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
class Variant;

struct [[nodiscard]] Plane {
static const Plane PLANE_YZ;
static const Plane PLANE_XZ;
static const Plane PLANE_XY;

Vector3 normal;
real_t d = 0;

Expand Down Expand Up @@ -90,6 +94,10 @@ struct [[nodiscard]] Plane {
_FORCE_INLINE_ Plane(const Vector3 &p_point1, const Vector3 &p_point2, const Vector3 &p_point3, ClockDirection p_dir = CLOCKWISE);
};

inline constexpr Plane Plane::PLANE_YZ = { 1, 0, 0, 0 };
inline constexpr Plane Plane::PLANE_XZ = { 0, 1, 0, 0 };
inline constexpr Plane Plane::PLANE_XY = { 0, 0, 1, 0 };

bool Plane::is_point_over(const Vector3 &p_point) const {
return (normal.dot(p_point) > d);
}
Expand Down
6 changes: 6 additions & 0 deletions core/math/transform_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ struct [[nodiscard]] Transform2D {
// WARNING: Be aware that unlike 3D code, 2D code uses a left-handed coordinate system:
// Y-axis points down, and angle is measure from +X to +Y in a clockwise-fashion.

static const Transform2D FLIP_X;
static const Transform2D FLIP_Y;

Vector2 columns[3] = {
{ 1, 0 },
{ 0, 1 },
Expand Down Expand Up @@ -149,6 +152,9 @@ struct [[nodiscard]] Transform2D {
Transform2D() = default;
};

inline constexpr Transform2D Transform2D::FLIP_X = { { -1, 0 }, { 0, 1 }, { 0, 0 } };
inline constexpr Transform2D Transform2D::FLIP_Y = { { 1, 0 }, { 0, -1 }, { 0, 0 } };

constexpr bool Transform2D::operator==(const Transform2D &p_transform) const {
for (int i = 0; i < 3; i++) {
if (columns[i] != p_transform.columns[i]) {
Expand Down
12 changes: 10 additions & 2 deletions core/math/transform_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
#include "core/templates/vector.h"

struct [[nodiscard]] Transform3D {
static const Transform3D FLIP_X;
static const Transform3D FLIP_Y;
static const Transform3D FLIP_Z;

Basis basis;
Vector3 origin;

Expand All @@ -51,8 +55,8 @@ struct [[nodiscard]] Transform3D {
void rotate(const Vector3 &p_axis, real_t p_angle);
void rotate_basis(const Vector3 &p_axis, real_t p_angle);

void set_look_at(const Vector3 &p_eye, const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0), bool p_use_model_front = false);
Transform3D looking_at(const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0), bool p_use_model_front = false) const;
void set_look_at(const Vector3 &p_eye, const Vector3 &p_target, const Vector3 &p_up = Vector3::UP, bool p_use_model_front = false);
Transform3D looking_at(const Vector3 &p_target, const Vector3 &p_up = Vector3::UP, bool p_use_model_front = false) const;

void scale(const Vector3 &p_scale);
Transform3D scaled(const Vector3 &p_scale) const;
Expand Down Expand Up @@ -136,6 +140,10 @@ struct [[nodiscard]] Transform3D {
origin(p_ox, p_oy, p_oz) {}
};

inline constexpr Transform3D Transform3D::FLIP_X = { Basis::FLIP_X };
inline constexpr Transform3D Transform3D::FLIP_Y = { Basis::FLIP_Y };
inline constexpr Transform3D Transform3D::FLIP_Z = { Basis::FLIP_Z };

constexpr bool Transform3D::operator==(const Transform3D &p_transform) const {
return (basis == p_transform.basis && origin == p_transform.origin);
}
Expand Down
12 changes: 11 additions & 1 deletion core/math/vector2.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ class String;
struct Vector2i;

struct [[nodiscard]] Vector2 {
static const int AXIS_COUNT = 2;
static const Vector2 LEFT;
static const Vector2 RIGHT;
static const Vector2 UP;
static const Vector2 DOWN;

static constexpr int AXIS_COUNT = 2;

enum Axis {
AXIS_X,
Expand Down Expand Up @@ -193,6 +198,11 @@ struct [[nodiscard]] Vector2 {
// NOLINTEND(cppcoreguidelines-pro-type-member-init)
};

inline constexpr Vector2 Vector2::LEFT = { -1, 0 };
inline constexpr Vector2 Vector2::RIGHT = { 1, 0 };
inline constexpr Vector2 Vector2::UP = { 0, -1 };
inline constexpr Vector2 Vector2::DOWN = { 0, 1 };

_FORCE_INLINE_ Vector2 Vector2::plane_project(real_t p_d, const Vector2 &p_vec) const {
return p_vec - *this * (dot(p_vec) - p_d);
}
Expand Down
12 changes: 11 additions & 1 deletion core/math/vector2i.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ class String;
struct Vector2;

struct [[nodiscard]] Vector2i {
static const int AXIS_COUNT = 2;
static const Vector2i LEFT;
static const Vector2i RIGHT;
static const Vector2i UP;
static const Vector2i DOWN;

static constexpr int AXIS_COUNT = 2;

enum Axis {
AXIS_X,
Expand Down Expand Up @@ -150,6 +155,11 @@ struct [[nodiscard]] Vector2i {
// NOLINTEND(cppcoreguidelines-pro-type-member-init)
};

inline constexpr Vector2i Vector2i::LEFT = { -1, 0 };
inline constexpr Vector2i Vector2i::RIGHT = { 1, 0 };
inline constexpr Vector2i Vector2i::UP = { 0, -1 };
inline constexpr Vector2i Vector2i::DOWN = { 0, 1 };

constexpr Vector2i Vector2i::operator+(const Vector2i &p_v) const {
return Vector2i(x + p_v.x, y + p_v.y);
}
Expand Down
30 changes: 28 additions & 2 deletions core/math/vector3.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,20 @@ struct Vector2;
struct Vector3i;

struct [[nodiscard]] Vector3 {
static const int AXIS_COUNT = 3;
static const Vector3 LEFT;
static const Vector3 RIGHT;
static const Vector3 UP;
static const Vector3 DOWN;
static const Vector3 FORWARD;
static const Vector3 BACK;
static const Vector3 MODEL_LEFT;
static const Vector3 MODEL_RIGHT;
static const Vector3 MODEL_TOP;
static const Vector3 MODEL_BOTTOM;
static const Vector3 MODEL_FRONT;
static const Vector3 MODEL_REAR;

static constexpr int AXIS_COUNT = 3;

enum Axis {
AXIS_X,
Expand Down Expand Up @@ -195,6 +208,19 @@ struct [[nodiscard]] Vector3 {
x(p_x), y(p_y), z(p_z) {}
};

inline constexpr Vector3 Vector3::LEFT = { -1, 0, 0 };
inline constexpr Vector3 Vector3::RIGHT = { 1, 0, 0 };
inline constexpr Vector3 Vector3::UP = { 0, 1, 0 };
inline constexpr Vector3 Vector3::DOWN = { 0, -1, 0 };
inline constexpr Vector3 Vector3::FORWARD = { 0, 0, -1 };
inline constexpr Vector3 Vector3::BACK = { 0, 0, 1 };
inline constexpr Vector3 Vector3::MODEL_LEFT = { 1, 0, 0 };
inline constexpr Vector3 Vector3::MODEL_RIGHT = { -1, 0, 0 };
inline constexpr Vector3 Vector3::MODEL_TOP = { 0, 1, 0 };
inline constexpr Vector3 Vector3::MODEL_BOTTOM = { 0, -1, 0 };
inline constexpr Vector3 Vector3::MODEL_FRONT = { 0, 0, 1 };
inline constexpr Vector3 Vector3::MODEL_REAR = { 0, 0, -1 };

Vector3 Vector3::cross(const Vector3 &p_with) const {
Vector3 ret(
(y * p_with.z) - (z * p_with.y),
Expand Down Expand Up @@ -334,7 +360,7 @@ Vector3 Vector3::get_any_perpendicular() const {
// since it could be a different vector depending on the prior branching code Math::abs(x) <= Math::abs(y) && Math::abs(x) <= Math::abs(z).
// However, it would be reasonable to use any of the axes of the basis, as it is simpler to calculate.
ERR_FAIL_COND_V_MSG(is_zero_approx(), Vector3(0, 0, 0), "The Vector3 must not be zero.");
return cross((Math::abs(x) <= Math::abs(y) && Math::abs(x) <= Math::abs(z)) ? Vector3(1, 0, 0) : Vector3(0, 1, 0)).normalized();
return cross((Math::abs(x) <= Math::abs(y) && Math::abs(x) <= Math::abs(z)) ? Vector3::RIGHT : Vector3::UP).normalized();
}

/* Operators */
Expand Down
16 changes: 15 additions & 1 deletion core/math/vector3i.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ class String;
struct Vector3;

struct [[nodiscard]] Vector3i {
static const int AXIS_COUNT = 3;
static const Vector3i LEFT;
static const Vector3i RIGHT;
static const Vector3i UP;
static const Vector3i DOWN;
static const Vector3i FORWARD;
static const Vector3i BACK;

static constexpr int AXIS_COUNT = 3;

enum Axis {
AXIS_X,
Expand Down Expand Up @@ -139,6 +146,13 @@ struct [[nodiscard]] Vector3i {
x(p_x), y(p_y), z(p_z) {}
};

inline constexpr Vector3i Vector3i::LEFT = { -1, 0, 0 };
inline constexpr Vector3i Vector3i::RIGHT = { 1, 0, 0 };
inline constexpr Vector3i Vector3i::UP = { 0, 1, 0 };
inline constexpr Vector3i Vector3i::DOWN = { 0, -1, 0 };
inline constexpr Vector3i Vector3i::FORWARD = { 0, 0, -1 };
inline constexpr Vector3i Vector3i::BACK = { 0, 0, 1 };

int64_t Vector3i::length_squared() const {
return x * (int64_t)x + y * (int64_t)y + z * (int64_t)z;
}
Expand Down
2 changes: 1 addition & 1 deletion core/math/vector4.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class String;
struct Vector4i;

struct [[nodiscard]] Vector4 {
static const int AXIS_COUNT = 4;
static constexpr int AXIS_COUNT = 4;

enum Axis {
AXIS_X,
Expand Down
2 changes: 1 addition & 1 deletion core/math/vector4i.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class String;
struct Vector4;

struct [[nodiscard]] Vector4i {
static const int AXIS_COUNT = 4;
static constexpr int AXIS_COUNT = 4;

enum Axis {
AXIS_X,
Expand Down
Loading
Loading