From c543a3cbe15b729e1020cee088aa81eaa3c47724 Mon Sep 17 00:00:00 2001 From: Marc Gilleron Date: Sun, 18 Sep 2022 22:35:43 +0100 Subject: [PATCH] Add Transform3D `translated_local`, `rotated_local`, `scaled_local` --- include/godot_cpp/variant/transform3d.hpp | 3 +++ src/variant/transform3d.cpp | 30 +++++++++++++++++------ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/include/godot_cpp/variant/transform3d.hpp b/include/godot_cpp/variant/transform3d.hpp index 975adf29..26ebca96 100644 --- a/include/godot_cpp/variant/transform3d.hpp +++ b/include/godot_cpp/variant/transform3d.hpp @@ -55,6 +55,7 @@ public: Transform3D affine_inverse() const; Transform3D rotated(const Vector3 &p_axis, real_t p_phi) const; + Transform3D rotated_local(const Vector3 &p_axis, real_t p_angle) const; void rotate(const Vector3 &p_axis, real_t p_phi); void rotate_basis(const Vector3 &p_axis, real_t p_phi); @@ -64,10 +65,12 @@ public: void scale(const Vector3 &p_scale); Transform3D scaled(const Vector3 &p_scale) const; + Transform3D scaled_local(const Vector3 &p_scale) const; void scale_basis(const Vector3 &p_scale); void translate(real_t p_tx, real_t p_ty, real_t p_tz); void translate(const Vector3 &p_translation); Transform3D translated(const Vector3 &p_translation) const; + Transform3D translated_local(const Vector3 &p_translation) const; const Basis &get_basis() const { return basis; } void set_basis(const Basis &p_basis) { basis = p_basis; } diff --git a/src/variant/transform3d.cpp b/src/variant/transform3d.cpp index 6b176833..b7a2e3c6 100644 --- a/src/variant/transform3d.cpp +++ b/src/variant/transform3d.cpp @@ -63,7 +63,15 @@ void Transform3D::rotate(const Vector3 &p_axis, real_t p_phi) { } Transform3D Transform3D::rotated(const Vector3 &p_axis, real_t p_phi) const { - return Transform3D(Basis(p_axis, p_phi), Vector3()) * (*this); + // Equivalent to left multiplication + Basis p_basis(p_axis, p_phi); + return Transform3D(p_basis * basis, p_basis.xform(origin)); +} + +Transform3D Transform3D::rotated_local(const Vector3 &p_axis, real_t p_angle) const { + // Equivalent to right multiplication + Basis p_basis(p_axis, p_angle); + return Transform3D(basis * p_basis, origin); } void Transform3D::rotate_basis(const Vector3 &p_axis, real_t p_phi) { @@ -133,9 +141,13 @@ void Transform3D::scale(const Vector3 &p_scale) { } Transform3D Transform3D::scaled(const Vector3 &p_scale) const { - Transform3D t = *this; - t.scale(p_scale); - return t; + // Equivalent to left multiplication + return Transform3D(basis.scaled(p_scale), origin * p_scale); +} + +Transform3D Transform3D::scaled_local(const Vector3 &p_scale) const { + // Equivalent to right multiplication + return Transform3D(basis.scaled_local(p_scale), origin); } void Transform3D::scale_basis(const Vector3 &p_scale) { @@ -153,9 +165,13 @@ void Transform3D::translate(const Vector3 &p_translation) { } Transform3D Transform3D::translated(const Vector3 &p_translation) const { - Transform3D t = *this; - t.translate(p_translation); - return t; + // Equivalent to left multiplication + return Transform3D(basis, origin + p_translation); +} + +Transform3D Transform3D::translated_local(const Vector3 &p_translation) const { + // Equivalent to right multiplication + return Transform3D(basis, origin + basis.xform(p_translation)); } void Transform3D::orthonormalize() {