Skip to content

Commit 9455d73

Browse files
committed
Add Vector4D constants for directions, sides, and cardinal directions
Not exposed because Godot still doesn't have a way to bind non-integer constants, but at least now these are defined internally, so the source code can be referenced to determine the intended semantics.
1 parent 748d93f commit 9455d73

File tree

9 files changed

+112
-8
lines changed

9 files changed

+112
-8
lines changed

godot_4d_defines.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
#include <godot_cpp/core/class_db.hpp>
1919
#include <godot_cpp/core/version.hpp>
2020
#include <godot_cpp/variant/string.hpp>
21+
#define ABS Math::abs
2122
#define EDSCALE EditorInterface::get_singleton()->get_editor_scale()
2223
#define GDEXTMOD_GUI_INPUT _gui_input
2324
#define GET_NODE_TYPE(m_parent, m_type, m_path) m_parent->get_node<m_type>(NodePath(m_path))
2425
#define TTR(m_text) m_text
26+
#define USE_CONST_NOT_CONSTEXPR_FOR_VECTORS 1
2527
#define VariantUtilityFunctions UtilityFunctions
2628
// Including the namespace helps make GDExtension code more similar to module code.
2729
using namespace godot;
@@ -40,13 +42,18 @@ using namespace godot;
4042
#define GODOT_VERSION_PATCH VERSION_PATCH
4143
#endif
4244

43-
#if GODOT_VERSION_MAJOR == 4 && GODOT_VERSION_MINOR < 6
45+
#if GODOT_VERSION_MAJOR > 4 || (GODOT_VERSION_MAJOR == 4 && GODOT_VERSION_MINOR < 6)
4446
// Prior to Godot 4.6, the internal API of free_rid in RenderingServer and other servers did not match the exposed API.
4547
// See https://github.com/godotengine/godot/pull/107139
4648
#define free_rid free
4749
#endif
4850

49-
#if GODOT_VERSION_MAJOR == 4 && GODOT_VERSION_MINOR > 4
51+
#if GODOT_VERSION_MAJOR > 4 || (GODOT_VERSION_MAJOR == 4 && GODOT_VERSION_MINOR < 5)
52+
// As of Godot 4.5, we can use constexpr for vectors, but 4.4 and earlier don't support constexpr for vectors.
53+
#define USE_CONST_NOT_CONSTEXPR_FOR_VECTORS 1
54+
#endif
55+
56+
#if GODOT_VERSION_MAJOR > 4 || (GODOT_VERSION_MAJOR == 4 && GODOT_VERSION_MINOR > 4)
5057
// In Godot 4.5 and later, Math_TAU was replaced with Math::TAU, and a few other things also moved to the Math namespace.
5158
#define ABS Math::abs
5259
#define Math_E Math::E

math/basis_4d.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "basis_4d.h"
22

33
#include "geometric_algebra/rotor_4d.h"
4-
#include "vector_4d.h"
54

65
// Misc methods.
76

math/basis_4d.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "../godot_4d_defines.h"
44

55
#include "geometric_algebra/bivector_4d.h"
6+
#include "vector_4d.h"
67

78
#if GDEXTENSION
89
#include <godot_cpp/variant/projection.hpp>
@@ -164,7 +165,7 @@ struct _NO_DISCARD_ Basis4D {
164165
static Basis4D from_wy(const real_t p_wy);
165166
static Basis4D from_zw(const real_t p_zw);
166167
static Basis4D from_swap_rotation(const int p_rot_from, const int p_rot_to);
167-
static Basis4D looking_at(const Vector4 &p_target, const Vector4 &p_perp = Vector4(0, 0, 0, 1), const Vector4 &p_up = Vector4(0, 1, 0, 0), bool p_use_model_front = false);
168+
static Basis4D looking_at(const Vector4 &p_target, const Vector4 &p_perp = Vector4D::DIR_ANA, const Vector4 &p_up = Vector4D::DIR_UP, bool p_use_model_front = false);
168169

169170
_FORCE_INLINE_ Basis4D() {}
170171
_FORCE_INLINE_ Basis4D(const Vector4 &p_x, const Vector4 &p_y, const Vector4 &p_z, const Vector4 &p_w) {

math/vector_4d.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,38 @@
88
#include "core/variant/variant_utility.h"
99
#endif
1010

11+
#if USE_CONST_NOT_CONSTEXPR_FOR_VECTORS
12+
const Vector4 Vector4D::ZERO = Vector4(0, 0, 0, 0);
13+
const Vector4 Vector4D::ONE = Vector4(1, 1, 1, 1);
14+
15+
const Vector4 Vector4D::DIR_RIGHT = Vector4(1, 0, 0, 0);
16+
const Vector4 Vector4D::DIR_LEFT = Vector4(-1, 0, 0, 0);
17+
const Vector4 Vector4D::DIR_UP = Vector4(0, 1, 0, 0);
18+
const Vector4 Vector4D::DIR_DOWN = Vector4(0, -1, 0, 0);
19+
const Vector4 Vector4D::DIR_BACK = Vector4(0, 0, 1, 0);
20+
const Vector4 Vector4D::DIR_FORWARD = Vector4(0, 0, -1, 0);
21+
const Vector4 Vector4D::DIR_ANA = Vector4(0, 0, 0, 1);
22+
const Vector4 Vector4D::DIR_KATA = Vector4(0, 0, 0, -1);
23+
24+
const Vector4 Vector4D::MODEL_LEFT_SIDE = Vector4(1, 0, 0, 0);
25+
const Vector4 Vector4D::MODEL_RIGHT_SIDE = Vector4(-1, 0, 0, 0);
26+
const Vector4 Vector4D::MODEL_TOP_SIDE = Vector4(0, 1, 0, 0);
27+
const Vector4 Vector4D::MODEL_BOTTOM_SIDE = Vector4(0, -1, 0, 0);
28+
const Vector4 Vector4D::MODEL_FRONT_SIDE = Vector4(0, 0, 1, 0);
29+
const Vector4 Vector4D::MODEL_REAR_SIDE = Vector4(0, 0, -1, 0);
30+
const Vector4 Vector4D::MODEL_ANA_SIDE = Vector4(0, 0, 0, 1);
31+
const Vector4 Vector4D::MODEL_KATA_SIDE = Vector4(0, 0, 0, -1);
32+
33+
const Vector4 Vector4D::CARDINAL_EAST = Vector4(1, 0, 0, 0);
34+
const Vector4 Vector4D::CARDINAL_WEST = Vector4(-1, 0, 0, 0);
35+
const Vector4 Vector4D::CARDINAL_ZENITH = Vector4(0, 1, 0, 0);
36+
const Vector4 Vector4D::CARDINAL_NADIR = Vector4(0, -1, 0, 0);
37+
const Vector4 Vector4D::CARDINAL_SOUTH = Vector4(0, 0, 1, 0);
38+
const Vector4 Vector4D::CARDINAL_NORTH = Vector4(0, 0, -1, 0);
39+
const Vector4 Vector4D::CARDINAL_ANTH = Vector4(0, 0, 0, 1);
40+
const Vector4 Vector4D::CARDINAL_KENTH = Vector4(0, 0, 0, -1);
41+
#endif // USE_CONST_NOT_CONSTEXPR
42+
1143
// NOTE: Editor code grabs the colors from the Godot editor theme instead.
1244
// This function is for non-editor code or when the theme is not available.
1345
Color Vector4D::axis_color(int64_t p_axis) {

math/vector_4d.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,70 @@ class Vector4D : public Object {
1111
static void _bind_methods();
1212

1313
public:
14+
// These are a superset of the directions found in Godot's Vector3 type.
15+
// These align with the G4MF specification: https://github.com/godot-dimensions/g4mf/blob/main/specification/parts/coordinate_system.md
16+
#if USE_CONST_NOT_CONSTEXPR_FOR_VECTORS
17+
static const Vector4 ZERO;
18+
static const Vector4 ONE;
19+
20+
static const Vector4 DIR_RIGHT;
21+
static const Vector4 DIR_LEFT;
22+
static const Vector4 DIR_UP;
23+
static const Vector4 DIR_DOWN;
24+
static const Vector4 DIR_BACK;
25+
static const Vector4 DIR_FORWARD;
26+
static const Vector4 DIR_ANA;
27+
static const Vector4 DIR_KATA;
28+
29+
static const Vector4 MODEL_LEFT_SIDE;
30+
static const Vector4 MODEL_RIGHT_SIDE;
31+
static const Vector4 MODEL_TOP_SIDE;
32+
static const Vector4 MODEL_BOTTOM_SIDE;
33+
static const Vector4 MODEL_FRONT_SIDE;
34+
static const Vector4 MODEL_REAR_SIDE;
35+
static const Vector4 MODEL_ANA_SIDE;
36+
static const Vector4 MODEL_KATA_SIDE;
37+
38+
static const Vector4 CARDINAL_EAST;
39+
static const Vector4 CARDINAL_WEST;
40+
static const Vector4 CARDINAL_ZENITH;
41+
static const Vector4 CARDINAL_NADIR;
42+
static const Vector4 CARDINAL_SOUTH;
43+
static const Vector4 CARDINAL_NORTH;
44+
static const Vector4 CARDINAL_ANTH;
45+
static const Vector4 CARDINAL_KENTH;
46+
#else
47+
static constexpr Vector4 ZERO = Vector4(0, 0, 0, 0);
48+
static constexpr Vector4 ONE = Vector4(1, 1, 1, 1);
49+
50+
static constexpr Vector4 DIR_RIGHT = Vector4(1, 0, 0, 0);
51+
static constexpr Vector4 DIR_LEFT = Vector4(-1, 0, 0, 0);
52+
static constexpr Vector4 DIR_UP = Vector4(0, 1, 0, 0);
53+
static constexpr Vector4 DIR_DOWN = Vector4(0, -1, 0, 0);
54+
static constexpr Vector4 DIR_BACK = Vector4(0, 0, 1, 0);
55+
static constexpr Vector4 DIR_FORWARD = Vector4(0, 0, -1, 0);
56+
static constexpr Vector4 DIR_ANA = Vector4(0, 0, 0, 1);
57+
static constexpr Vector4 DIR_KATA = Vector4(0, 0, 0, -1);
58+
59+
static constexpr Vector4 MODEL_LEFT_SIDE = Vector4(1, 0, 0, 0);
60+
static constexpr Vector4 MODEL_RIGHT_SIDE = Vector4(-1, 0, 0, 0);
61+
static constexpr Vector4 MODEL_TOP_SIDE = Vector4(0, 1, 0, 0);
62+
static constexpr Vector4 MODEL_BOTTOM_SIDE = Vector4(0, -1, 0, 0);
63+
static constexpr Vector4 MODEL_FRONT_SIDE = Vector4(0, 0, 1, 0);
64+
static constexpr Vector4 MODEL_REAR_SIDE = Vector4(0, 0, -1, 0);
65+
static constexpr Vector4 MODEL_ANA_SIDE = Vector4(0, 0, 0, 1);
66+
static constexpr Vector4 MODEL_KATA_SIDE = Vector4(0, 0, 0, -1);
67+
68+
static constexpr Vector4 CARDINAL_EAST = Vector4(1, 0, 0, 0);
69+
static constexpr Vector4 CARDINAL_WEST = Vector4(-1, 0, 0, 0);
70+
static constexpr Vector4 CARDINAL_ZENITH = Vector4(0, 1, 0, 0);
71+
static constexpr Vector4 CARDINAL_NADIR = Vector4(0, -1, 0, 0);
72+
static constexpr Vector4 CARDINAL_SOUTH = Vector4(0, 0, 1, 0);
73+
static constexpr Vector4 CARDINAL_NORTH = Vector4(0, 0, -1, 0);
74+
static constexpr Vector4 CARDINAL_ANTH = Vector4(0, 0, 0, 1);
75+
static constexpr Vector4 CARDINAL_KENTH = Vector4(0, 0, 0, -1);
76+
#endif
77+
1478
// Cosmetic functions.
1579
static Color axis_color(int64_t p_axis);
1680
static String axis_letter(int64_t p_axis);

model/g4mf/structures/g4mf_node_4d.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ Dictionary G4MFNode4D::to_dictionary(const bool p_prefer_basis) const {
210210
dict["rotor"] = rotor_4d_to_json_array(rotor);
211211
}
212212
Vector4 scale = _transform.basis.get_scale();
213-
if (!scale.is_equal_approx(Vector4(1, 1, 1, 1))) {
213+
if (!scale.is_equal_approx(Vector4D::ONE)) {
214214
if (Vector4D::is_uniform(scale)) {
215215
Array uniform_scale_array;
216216
uniform_scale_array.push_back(scale.x);

nodes/node_4d.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ void Node4D::_bind_methods() {
791791
// Transform altering methods.
792792
ClassDB::bind_method(D_METHOD("apply_scale", "ratio"), &Node4D::apply_scale);
793793
ClassDB::bind_method(D_METHOD("translate_local", "offset"), &Node4D::translate_local);
794-
ClassDB::bind_method(D_METHOD("look_at", "global_target", "up", "use_model_front"), &Node4D::look_at, DEFVAL(Vector4(0, 1, 0, 0)), DEFVAL(false));
794+
ClassDB::bind_method(D_METHOD("look_at", "global_target", "up", "use_model_front"), &Node4D::look_at, DEFVAL(Vector4D::DIR_UP), DEFVAL(false));
795795
ClassDB::bind_method(D_METHOD("rotate_euler", "euler"), &Node4D::rotate_euler_bind);
796796
ClassDB::bind_method(D_METHOD("rotate_euler_local", "euler_local"), &Node4D::rotate_euler_local_bind);
797797
// Geometric algebra rotation altering methods.

nodes/node_4d.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "../math/geometric_algebra/rotor_4d_bind.h"
55
#include "../math/rect4.h"
66
#include "../math/transform_4d_bind.h"
7+
#include "../math/vector_4d.h"
78

89
#if GDEXTENSION
910
#include <godot_cpp/classes/node.hpp>
@@ -56,7 +57,7 @@ class Node4D : public Node {
5657
// Transform altering methods.
5758
void apply_scale(const Vector4 &p_amount);
5859
void translate_local(const Vector4 &p_amount);
59-
void look_at(const Vector4 &p_global_target, const Vector4 &p_up = Vector4(0, 1, 0, 0), const bool p_use_model_front = false);
60+
void look_at(const Vector4 &p_global_target, const Vector4 &p_up = Vector4D::DIR_UP, const bool p_use_model_front = false);
6061

6162
void rotate_euler(const Euler4D &p_euler);
6263
void rotate_euler_bind(const AABB &p_euler);

physics/bodies/character_body_4d.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class CharacterBody4D : public PhysicsBody4D {
66
GDCLASS(CharacterBody4D, PhysicsBody4D);
77

88
Vector4 _linear_velocity = Vector4();
9-
Vector4 _up_direction = Vector4(0, 1, 0, 0);
9+
Vector4 _up_direction = Vector4D::DIR_UP;
1010
real_t _floor_max_angle = Math_TAU / 4.0;
1111
bool _is_on_ceiling = false;
1212
bool _is_on_floor = false;

0 commit comments

Comments
 (0)