Ported: Add global_translation and global_rotation to Spatial - foxydevloper

5238740fef
This commit is contained in:
Relintai 2022-07-29 21:02:00 +02:00
parent e2987f8914
commit 5ebeb6a36c
3 changed files with 54 additions and 5 deletions

View File

@ -253,9 +253,16 @@
<member name="gizmo" type="SpatialGizmo" setter="set_gizmo" getter="get_gizmo">
The [SpatialGizmo] for this node. Used for example in [EditorSpatialGizmo] as custom visualization and editing handles in Editor.
</member>
<member name="global_rotation" type="Vector3" setter="set_global_rotation" getter="get_global_rotation">
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.
</member>
<member name="global_transform" type="Transform" setter="set_global_transform" getter="get_global_transform">
World space (global) [Transform] of this node.
</member>
<member name="global_translation" type="Vector3" setter="set_global_translation" getter="get_global_translation">
Global position of this node. This is equivalent to [code]global_transform.origin[/code].
</member>
<member name="rotation" type="Vector3" setter="set_rotation" getter="get_rotation">
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.

View File

@ -250,6 +250,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;
@ -849,8 +871,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);
@ -907,13 +935,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", "");

View File

@ -121,11 +121,15 @@ class Spatial : public Node {
void _propagate_visibility_changed();
protected:
_FORCE_INLINE_ void set_ignore_transform_notification(bool p_ignore) { data.ignore_notification = p_ignore; }
_FORCE_INLINE_ void set_ignore_transform_notification(bool p_ignore) {
data.ignore_notification = p_ignore;
}
_FORCE_INLINE_ void _update_local_transform() const;
void _set_vi_visible(bool p_visible);
bool _is_vi_visible() const { return data.vi_visible; }
bool _is_vi_visible() const {
return data.vi_visible;
}
Transform _get_global_transform_interpolated(real_t p_interpolation_fraction);
void _disable_client_physics_interpolation();
@ -154,11 +158,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);
@ -184,7 +194,9 @@ public:
void set_gizmo(const Ref<SpatialGizmo> &p_gizmo);
Ref<SpatialGizmo> get_gizmo() const;
_FORCE_INLINE_ bool is_inside_world() const { return data.inside_world; }
_FORCE_INLINE_ bool is_inside_world() const {
return data.inside_world;
}
Transform get_relative_transform(const Node *p_parent) const;