diff --git a/core/math/matrix3.cpp b/core/math/matrix3.cpp index e1708a44b0b0..ceaff04ae85f 100644 --- a/core/math/matrix3.cpp +++ b/core/math/matrix3.cpp @@ -228,12 +228,24 @@ void Basis::scale(const Vector3 &p_scale) { } Basis Basis::scaled(const Vector3 &p_scale) const { - Basis m = *this; m.scale(p_scale); return m; } +void Basis::scale_local(const Vector3 &p_scale) { + // performs a scaling in object-local coordinate system: + // M -> (M.S.Minv).M = M.S. + *this = scaled_local(p_scale); +} + +Basis Basis::scaled_local(const Vector3 &p_scale) const { + Basis b; + b.set_scale(p_scale); + + return (*this) * b; +} + void Basis::set_scale(const Vector3 &p_scale) { set_axis(0, get_axis(0).normalized() * p_scale.x); @@ -312,7 +324,8 @@ void Basis::rotate(const Vector3 &p_axis, real_t p_phi) { } void Basis::rotate_local(const Vector3 &p_axis, real_t p_phi) { - + // performs a rotation in object-local coordinate system: + // M -> (M.R.Minv).M = M.R. *this = rotated_local(p_axis, p_phi); } Basis Basis::rotated_local(const Vector3 &p_axis, real_t p_phi) const { diff --git a/core/math/matrix3.h b/core/math/matrix3.h index 71971bdea8bd..c42643572997 100644 --- a/core/math/matrix3.h +++ b/core/math/matrix3.h @@ -103,6 +103,9 @@ public: void scale(const Vector3 &p_scale); Basis scaled(const Vector3 &p_scale) const; + void scale_local(const Vector3 &p_scale); + Basis scaled_local(const Vector3 &p_scale) const; + void set_scale(const Vector3 &p_scale); Vector3 get_scale() const; Vector3 get_signed_scale() const; diff --git a/doc/classes/Basis.xml b/doc/classes/Basis.xml index a873bd9a27f7..157359648702 100644 --- a/doc/classes/Basis.xml +++ b/doc/classes/Basis.xml @@ -38,7 +38,7 @@ - Create a rotation matrix which rotates around the given axis by the specified angle. The axis must be a normalized vector. + Create a rotation matrix which rotates around the given axis by the specified angle, in radians. The axis must be a normalized vector. diff --git a/doc/classes/Spatial.xml b/doc/classes/Spatial.xml index ea04192a5e66..7940e4065ec7 100644 --- a/doc/classes/Spatial.xml +++ b/doc/classes/Spatial.xml @@ -5,6 +5,8 @@ Most basic 3D game object, with a 3D [Transform] and visibility settings. All other 3D game objects inherit from Spatial. Use Spatial as a parent node to move, scale, rotate and show/hide children in a 3D project. + + Affine operations (rotate, scale, translate) happen in parent's local coordinate system, unless the Spatial object is set as top level. Affine operations in this coordinate system correspond to direct affine operations on the Spatial's transform. The word local below refers to this coordinate system. The coordinate system that is attached to the Spatial object itself is referred to as object-local coordinate system. @@ -32,15 +34,35 @@ Returns the current [World] resource this Spatial node is registered to. + + + + + + + + + Rotates the local transformation around axis, a unit [Vector3], by specified angle in radians. The rotation axis is in object-local coordinate system. + + + + + + + + + Scales the local transformation by given 3D scale factors in object-local coordinate system. + + - + - + - Rotates the current node along normal [Vector3] by angle in radians in Global space. + Rotates the global (world) transformation around axis, a unit [Vector3], by specified angle in radians. The rotation axis is in global coordinate system. @@ -49,7 +71,7 @@ - Moves the node by [Vector3] offset in Global space. + Moves the global (world) transformation by [Vector3] offset. The offset is in global coordinate system. @@ -115,45 +137,45 @@ - Resets this node's transformations (like scale, skew and taper) preserving its rotation and translation. Performs orthonormalization on this node [Transform3D]. + Resets this node's transformations (like scale, skew and taper) preserving its rotation and translation by performing Gram-Schmidt orthonormalization on this node's [Transform3D]. - + - + - Rotates the node in local space on given normal [Vector3] by angle in radians. + Rotates the local transformation around axis, a unit [Vector3], by specified angle in radians. - + - Rotates the node in local space on X axis by angle in radians. + Rotates the local transformation around the X axis by angle in radians - + - Rotates the node in local space on Y axis by angle in radians. + Rotates the local transformation around the Y axis by angle in radians. - + - Rotates the node in local space on Z axis by angle in radians. + Rotates the local transformation around the Z axis by angle in radians. @@ -255,16 +277,18 @@ World space (global) [Transform] of this node. - Local euler rotation in radians of this node. + Rotation part of the local transformation, specified in terms of YXZ-Euler angles in the format (X-angle, Y-angle, Z-angle), in radians. + + Note that in the mathematical sense, rotation is a matrix and not a vector. The three Euler angles, which are the three indepdent 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. - Local euler rotation in degrees of this node. + Rotation part of the local transformation, specified in terms of YXZ-Euler angles in the format (X-angle, Y-angle, Z-angle), in degrees. - Local scale of this node. + Scale part of the local transformation. - Local space [Transform] of this node. + Local space [Transform] of this node, with respect to the parent node. Local translation of this node. diff --git a/scene/3d/spatial.cpp b/scene/3d/spatial.cpp index 8b5c85763a98..a55c4a2cc57e 100644 --- a/scene/3d/spatial.cpp +++ b/scene/3d/spatial.cpp @@ -555,30 +555,36 @@ bool Spatial::is_visible() const { return data.visible; } -void Spatial::rotate(const Vector3 &p_normal, float p_radians) { - +void Spatial::rotate_object_local(const Vector3 &p_axis, float p_angle) { Transform t = get_transform(); - t.basis.rotate_local(p_normal, p_radians); //use local rotation here, as it makes more sense here in tree hierarchy + t.basis.rotate_local(p_axis, p_angle); set_transform(t); } -void Spatial::rotate_x(float p_radians) { +void Spatial::rotate(const Vector3 &p_axis, float p_angle) { Transform t = get_transform(); - t.basis.rotate_local(Vector3(1, 0, 0), p_radians); + t.basis.rotate(p_axis, p_angle); set_transform(t); } -void Spatial::rotate_y(float p_radians) { +void Spatial::rotate_x(float p_angle) { Transform t = get_transform(); - t.basis.rotate_local(Vector3(0, 1, 0), p_radians); + t.basis.rotate(Vector3(1, 0, 0), p_angle); set_transform(t); } -void Spatial::rotate_z(float p_radians) { + +void Spatial::rotate_y(float p_angle) { Transform t = get_transform(); - t.basis.rotate_local(Vector3(0, 0, 1), p_radians); + t.basis.rotate(Vector3(0, 1, 0), p_angle); + set_transform(t); +} +void Spatial::rotate_z(float p_angle) { + + Transform t = get_transform(); + t.basis.rotate(Vector3(0, 0, 1), p_angle); set_transform(t); } @@ -589,19 +595,45 @@ void Spatial::translate(const Vector3 &p_offset) { set_transform(t); } +void Spatial::translate_object_local(const Vector3 &p_offset) { + Transform t = get_transform(); + + Transform s; + s.translate(p_offset); + set_transform(t * s); +} + void Spatial::scale(const Vector3 &p_ratio) { Transform t = get_transform(); t.basis.scale(p_ratio); set_transform(t); } -void Spatial::global_rotate(const Vector3 &p_normal, float p_radians) { - Basis rotation(p_normal, p_radians); +void Spatial::scale_object_local(const Vector3 &p_scale) { + Transform t = get_transform(); + t.basis.scale_local(p_scale); + set_transform(t); +} + +void Spatial::global_rotate(const Vector3 &p_axis, float p_angle) { + + Basis rotation(p_axis, p_angle); Transform t = get_global_transform(); t.basis = rotation * t.basis; set_global_transform(t); } + +void Spatial::global_scale(const Vector3 &p_scale) { + + Basis s; + s.set_scale(p_scale); + + Transform t = get_global_transform(); + t.basis = s * t.basis; + set_global_transform(t); +} + void Spatial::global_translate(const Vector3 &p_offset) { Transform t = get_global_transform(); t.origin += p_offset; @@ -620,7 +652,7 @@ void Spatial::set_identity() { set_transform(Transform()); } -void Spatial::look_at(const Vector3 &p_target, const Vector3 &p_up_normal) { +void Spatial::look_at(const Vector3 &p_target, const Vector3 &p_up) { Transform lookat; lookat.origin = get_global_transform().origin; @@ -629,19 +661,19 @@ void Spatial::look_at(const Vector3 &p_target, const Vector3 &p_up_normal) { ERR_FAIL(); } - if (p_up_normal.cross(p_target - lookat.origin) == Vector3()) { + if (p_up.cross(p_target - lookat.origin) == Vector3()) { ERR_EXPLAIN("Up vector and direction between node origin and target are aligned, look_at() failed"); ERR_FAIL(); } - lookat = lookat.looking_at(p_target, p_up_normal); + lookat = lookat.looking_at(p_target, p_up); set_global_transform(lookat); } -void Spatial::look_at_from_position(const Vector3 &p_pos, const Vector3 &p_target, const Vector3 &p_up_normal) { +void Spatial::look_at_from_position(const Vector3 &p_pos, const Vector3 &p_target, const Vector3 &p_up) { Transform lookat; lookat.origin = p_pos; - lookat = lookat.looking_at(p_target, p_up_normal); + lookat = lookat.looking_at(p_target, p_up); set_global_transform(lookat); } @@ -677,9 +709,9 @@ void Spatial::_bind_methods() { ClassDB::bind_method(D_METHOD("get_transform"), &Spatial::get_transform); ClassDB::bind_method(D_METHOD("set_translation", "translation"), &Spatial::set_translation); ClassDB::bind_method(D_METHOD("get_translation"), &Spatial::get_translation); - ClassDB::bind_method(D_METHOD("set_rotation", "radians"), &Spatial::set_rotation); + ClassDB::bind_method(D_METHOD("set_rotation", "euler"), &Spatial::set_rotation); ClassDB::bind_method(D_METHOD("get_rotation"), &Spatial::get_rotation); - ClassDB::bind_method(D_METHOD("set_rotation_degrees", "degrees"), &Spatial::set_rotation_degrees); + ClassDB::bind_method(D_METHOD("set_rotation_degrees", "euler_degrees"), &Spatial::set_rotation_degrees); 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); @@ -711,22 +743,26 @@ void Spatial::_bind_methods() { ClassDB::bind_method(D_METHOD("set_notify_transform", "enable"), &Spatial::set_notify_transform); ClassDB::bind_method(D_METHOD("is_transform_notification_enabled"), &Spatial::is_transform_notification_enabled); - void rotate(const Vector3 &p_normal, float p_radians); - void rotate_x(float p_radians); - void rotate_y(float p_radians); - void rotate_z(float p_radians); + void rotate(const Vector3 &p_axis, float p_angle); + void rotate_x(float p_angle); + void rotate_y(float p_angle); + void rotate_z(float p_angle); void translate(const Vector3 &p_offset); void scale(const Vector3 &p_ratio); - void global_rotate(const Vector3 &p_normal, float p_radians); + void global_rotate(const Vector3 &p_axis, float p_angle); void global_translate(const Vector3 &p_offset); - ClassDB::bind_method(D_METHOD("rotate", "normal", "radians"), &Spatial::rotate); - ClassDB::bind_method(D_METHOD("global_rotate", "normal", "radians"), &Spatial::global_rotate); - ClassDB::bind_method(D_METHOD("rotate_x", "radians"), &Spatial::rotate_x); - ClassDB::bind_method(D_METHOD("rotate_y", "radians"), &Spatial::rotate_y); - ClassDB::bind_method(D_METHOD("rotate_z", "radians"), &Spatial::rotate_z); - ClassDB::bind_method(D_METHOD("translate", "offset"), &Spatial::translate); + ClassDB::bind_method(D_METHOD("rotate", "axis", "angle"), &Spatial::rotate); + ClassDB::bind_method(D_METHOD("global_rotate", "axis", "angle"), &Spatial::global_rotate); + ClassDB::bind_method(D_METHOD("global_scale", "scale"), &Spatial::global_scale); ClassDB::bind_method(D_METHOD("global_translate", "offset"), &Spatial::global_translate); + ClassDB::bind_method(D_METHOD("rotate_object_local", "axis", "angle"), &Spatial::rotate_object_local); + ClassDB::bind_method(D_METHOD("scale_object_local", "scale"), &Spatial::scale_object_local); + ClassDB::bind_method(D_METHOD("translate_object_local", "offset"), &Spatial::translate_object_local); + ClassDB::bind_method(D_METHOD("rotate_x", "angle"), &Spatial::rotate_x); + ClassDB::bind_method(D_METHOD("rotate_y", "angle"), &Spatial::rotate_y); + ClassDB::bind_method(D_METHOD("rotate_z", "angle"), &Spatial::rotate_z); + ClassDB::bind_method(D_METHOD("translate", "offset"), &Spatial::translate); ClassDB::bind_method(D_METHOD("orthonormalize"), &Spatial::orthonormalize); ClassDB::bind_method(D_METHOD("set_identity"), &Spatial::set_identity); diff --git a/scene/3d/spatial.h b/scene/3d/spatial.h index 5ebe2f5214b2..1c2e73d5ba2a 100644 --- a/scene/3d/spatial.h +++ b/scene/3d/spatial.h @@ -158,17 +158,23 @@ public: Transform get_relative_transform(const Node *p_parent) const; - void rotate(const Vector3 &p_normal, float p_radians); - void rotate_x(float p_radians); - void rotate_y(float p_radians); - void rotate_z(float p_radians); + void rotate(const Vector3 &p_axis, float p_angle); + void rotate_x(float p_angle); + void rotate_y(float p_angle); + void rotate_z(float p_angle); void translate(const Vector3 &p_offset); void scale(const Vector3 &p_ratio); - void global_rotate(const Vector3 &p_normal, float p_radians); + + void rotate_object_local(const Vector3 &p_axis, float p_angle); + void scale_object_local(const Vector3 &p_scale); + void translate_object_local(const Vector3 &p_offset); + + void global_rotate(const Vector3 &p_axis, float p_angle); + void global_scale(const Vector3 &p_scale); void global_translate(const Vector3 &p_offset); - void look_at(const Vector3 &p_target, const Vector3 &p_up_normal); - void look_at_from_position(const Vector3 &p_pos, const Vector3 &p_target, const Vector3 &p_up_normal); + void look_at(const Vector3 &p_target, const Vector3 &p_up); + void look_at_from_position(const Vector3 &p_pos, const Vector3 &p_target, const Vector3 &p_up); Vector3 to_local(Vector3 p_global) const; Vector3 to_global(Vector3 p_local) const;