From 5238740fef3dbe789c77b622df171a1f21d093ba Mon Sep 17 00:00:00 2001
From: foxydevloper <12120644+foxydevloper@users.noreply.github.com>
Date: Wed, 21 Jul 2021 00:13:52 -0400
Subject: [PATCH] Add global_translation and global_rotation to Spatial
---
doc/classes/Spatial.xml | 7 +++++++
scene/3d/spatial.cpp | 34 ++++++++++++++++++++++++++++++++--
scene/3d/spatial.h | 6 ++++++
3 files changed, 45 insertions(+), 2 deletions(-)
diff --git a/doc/classes/Spatial.xml b/doc/classes/Spatial.xml
index ee6d23e7879..76553603e7d 100644
--- a/doc/classes/Spatial.xml
+++ b/doc/classes/Spatial.xml
@@ -253,9 +253,16 @@
The [SpatialGizmo] for this node. Used for example in [EditorSpatialGizmo] as custom visualization and editing handles in Editor.
+
+ Rotation part of the global transformation in radians, specified in terms of YXZ-Euler angles in the format (X angle, Y angle, Z angle).
+ [b]Note:[/b] In the mathematical sense, rotation is a matrix and not a vector. The three Euler angles, which are the three independent parameters of the Euler-angle parametrization of the rotation matrix, are stored in a [Vector3] data structure not because the rotation is a vector, but only because [Vector3] exists as a convenient data-structure to store 3 floating-point numbers. Therefore, applying affine operations on the rotation "vector" is not meaningful.
+
World space (global) [Transform] of this node.
+
+ Global position of this node. This is equivalent to [code]global_transform.origin[/code].
+
Rotation part of the local transformation in radians, specified in terms of YXZ-Euler angles in the format (X angle, Y angle, Z angle).
[b]Note:[/b] In the mathematical sense, rotation is a matrix and not a vector. The three Euler angles, which are the three independent parameters of the Euler-angle parametrization of the rotation matrix, are stored in a [Vector3] data structure not because the rotation is a vector, but only because [Vector3] exists as a convenient data-structure to store 3 floating-point numbers. Therefore, applying affine operations on the rotation "vector" is not meaningful.
diff --git a/scene/3d/spatial.cpp b/scene/3d/spatial.cpp
index f4f23814282..47bb5e7b807 100644
--- a/scene/3d/spatial.cpp
+++ b/scene/3d/spatial.cpp
@@ -249,6 +249,28 @@ void Spatial::_notification(int p_what) {
}
}
+Vector3 Spatial::get_global_translation() const {
+ return get_global_transform().get_origin();
+}
+
+void Spatial::set_global_translation(const Vector3 &p_translation) {
+ Transform transform = get_global_transform();
+ transform.set_origin(p_translation);
+ set_global_transform(transform);
+}
+
+Vector3 Spatial::get_global_rotation() const {
+ return get_global_transform().get_basis().get_euler();
+}
+
+void Spatial::set_global_rotation(const Vector3 &p_euler_rad) {
+ Transform transform = get_global_transform();
+ Basis new_basis = transform.get_basis();
+ new_basis.set_euler(p_euler_rad);
+ transform.set_basis(new_basis);
+ set_global_transform(transform);
+}
+
void Spatial::set_transform(const Transform &p_transform) {
data.local_transform = p_transform;
data.dirty |= DIRTY_VECTORS;
@@ -848,8 +870,14 @@ void Spatial::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_rotation_degrees"), &Spatial::get_rotation_degrees);
ClassDB::bind_method(D_METHOD("set_scale", "scale"), &Spatial::set_scale);
ClassDB::bind_method(D_METHOD("get_scale"), &Spatial::get_scale);
+
ClassDB::bind_method(D_METHOD("set_global_transform", "global"), &Spatial::set_global_transform);
ClassDB::bind_method(D_METHOD("get_global_transform"), &Spatial::get_global_transform);
+ ClassDB::bind_method(D_METHOD("set_global_translation", "translation"), &Spatial::set_global_translation);
+ ClassDB::bind_method(D_METHOD("get_global_translation"), &Spatial::get_global_translation);
+ ClassDB::bind_method(D_METHOD("set_global_rotation", "radians"), &Spatial::set_global_rotation);
+ ClassDB::bind_method(D_METHOD("get_global_rotation"), &Spatial::get_global_rotation);
+
ClassDB::bind_method(D_METHOD("get_global_transform_interpolated"), &Spatial::get_global_transform_interpolated);
ClassDB::bind_method(D_METHOD("get_parent_spatial"), &Spatial::get_parent_spatial);
ClassDB::bind_method(D_METHOD("set_ignore_transform_notification", "enabled"), &Spatial::set_ignore_transform_notification);
@@ -906,13 +934,15 @@ void Spatial::_bind_methods() {
BIND_CONSTANT(NOTIFICATION_ENTER_GAMEPLAY);
BIND_CONSTANT(NOTIFICATION_EXIT_GAMEPLAY);
- //ADD_PROPERTY( PropertyInfo(Variant::TRANSFORM,"transform/global",PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR ), "set_global_transform", "get_global_transform") ;
ADD_GROUP("Transform", "");
- ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM, "global_transform", PROPERTY_HINT_NONE, "", 0), "set_global_transform", "get_global_transform");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "translation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_translation", "get_translation");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "rotation_degrees", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_rotation_degrees", "get_rotation_degrees");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "rotation", PROPERTY_HINT_NONE, "", 0), "set_rotation", "get_rotation");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "scale", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_scale", "get_scale");
+ ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM, "global_transform", PROPERTY_HINT_NONE, "", 0), "set_global_transform", "get_global_transform");
+ ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "global_translation", PROPERTY_HINT_NONE, "", 0), "set_global_translation", "get_global_translation");
+ ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "global_rotation", PROPERTY_HINT_NONE, "", 0), "set_global_rotation", "get_global_rotation");
+
ADD_GROUP("Matrix", "");
ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM, "transform", PROPERTY_HINT_NONE, ""), "set_transform", "get_transform");
ADD_GROUP("Visibility", "");
diff --git a/scene/3d/spatial.h b/scene/3d/spatial.h
index 0091fde1eab..000b8cc6ccf 100644
--- a/scene/3d/spatial.h
+++ b/scene/3d/spatial.h
@@ -153,11 +153,17 @@ public:
void set_rotation_degrees(const Vector3 &p_euler_deg);
void set_scale(const Vector3 &p_scale);
+ void set_global_translation(const Vector3 &p_translation);
+ void set_global_rotation(const Vector3 &p_euler_rad);
+
Vector3 get_translation() const;
Vector3 get_rotation() const;
Vector3 get_rotation_degrees() const;
Vector3 get_scale() const;
+ Vector3 get_global_translation() const;
+ Vector3 get_global_rotation() const;
+
void set_transform(const Transform &p_transform);
void set_global_transform(const Transform &p_transform);