From f242f9c7385f86c5bb5ebd8c56b5a6d6ebb59985 Mon Sep 17 00:00:00 2001 From: Fabian Keller Date: Sat, 30 Jul 2022 12:17:33 +0200 Subject: Fix consistency of translated/scaled/rotated in Transform2D and Transform3D --- .../glue/GodotSharp/GodotSharp/Core/Transform2D.cs | 57 ++++++++++++++++++--- .../glue/GodotSharp/GodotSharp/Core/Transform3D.cs | 58 +++++++++++++++++++--- 2 files changed, 100 insertions(+), 15 deletions(-) (limited to 'modules/mono/glue') diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Transform2D.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Transform2D.cs index b8413f1e16..68d097eb4e 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Transform2D.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Transform2D.cs @@ -297,7 +297,9 @@ namespace Godot } /// - /// Rotates the transform by (in radians), using matrix multiplication. + /// Rotates the transform by (in radians). + /// The operation is done in the parent/global frame, equivalent to + /// multiplying the matrix from the left. /// /// The angle to rotate, in radians. /// The rotated transformation matrix. @@ -307,7 +309,21 @@ namespace Godot } /// - /// Scales the transform by the given scaling factor, using matrix multiplication. + /// Rotates the transform by (in radians). + /// The operation is done in the local frame, equivalent to + /// multiplying the matrix from the right. + /// + /// The angle to rotate, in radians. + /// The rotated transformation matrix. + public Transform2D RotatedLocal(real_t angle) + { + return new Transform2D(angle, new Vector2()) * this; + } + + /// + /// Scales the transform by the given scaling factor. + /// The operation is done in the parent/global frame, equivalent to + /// multiplying the matrix from the left. /// /// The scale to introduce. /// The scaled transformation matrix. @@ -320,6 +336,21 @@ namespace Godot return copy; } + /// + /// Scales the transform by the given scaling factor. + /// The operation is done in the local frame, equivalent to + /// multiplying the matrix from the right. + /// + /// The scale to introduce. + /// The scaled transformation matrix. + public Transform2D ScaledLocal(Vector2 scale) + { + Transform2D copy = this; + copy.x *= scale; + copy.y *= scale; + return copy; + } + private real_t Tdotx(Vector2 with) { return (this[0, 0] * with[0]) + (this[1, 0] * with[1]); @@ -331,11 +362,23 @@ namespace Godot } /// - /// Translates the transform by the given , - /// relative to the transform's basis vectors. - /// - /// Unlike and , - /// this does not use matrix multiplication. + /// Translates the transform by the given . + /// The operation is done in the parent/global frame, equivalent to + /// multiplying the matrix from the left. + /// + /// The offset to translate by. + /// The translated matrix. + public Transform2D Translated(Vector2 offset) + { + Transform2D copy = this; + copy.origin += offset; + return copy; + } + + /// + /// Translates the transform by the given . + /// The operation is done in the local frame, equivalent to + /// multiplying the matrix from the right. /// /// The offset to translate by. /// The translated matrix. diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Transform3D.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Transform3D.cs index 97b4a87713..c00b9d8e9b 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Transform3D.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Transform3D.cs @@ -186,8 +186,10 @@ namespace Godot } /// - /// Rotates the transform around the given by (in radians), - /// using matrix multiplication. The axis must be a normalized vector. + /// Rotates the transform around the given by (in radians). + /// The axis must be a normalized vector. + /// The operation is done in the parent/global frame, equivalent to + /// multiplying the matrix from the left. /// /// The axis to rotate around. Must be normalized. /// The angle to rotate, in radians. @@ -198,7 +200,24 @@ namespace Godot } /// - /// Scales the transform by the given 3D scaling factor, using matrix multiplication. + /// Rotates the transform around the given by (in radians). + /// The axis must be a normalized vector. + /// The operation is done in the local frame, equivalent to + /// multiplying the matrix from the right. + /// + /// The axis to rotate around. Must be normalized. + /// The angle to rotate, in radians. + /// The rotated transformation matrix. + public Transform3D RotatedLocal(Vector3 axis, real_t angle) + { + Basis tmpBasis = new Basis(axis, angle); + return new Transform3D(basis * tmpBasis, origin); + } + + /// + /// Scales the transform by the given 3D factor. + /// The operation is done in the parent/global frame, equivalent to + /// multiplying the matrix from the left. /// /// The scale to introduce. /// The scaled transformation matrix. @@ -207,6 +226,19 @@ namespace Godot return new Transform3D(basis.Scaled(scale), origin * scale); } + /// + /// Scales the transform by the given 3D factor. + /// The operation is done in the local frame, equivalent to + /// multiplying the matrix from the right. + /// + /// The scale to introduce. + /// The scaled transformation matrix. + public Transform3D ScaledLocal(Vector3 scale) + { + Basis tmpBasis = new Basis(new Vector3(scale.x, 0, 0), new Vector3(0, scale.y, 0), new Vector3(0, 0, scale.z)); + return new Transform3D(basis * tmpBasis, origin); + } + private void SetLookAt(Vector3 eye, Vector3 target, Vector3 up) { // Make rotation matrix @@ -231,11 +263,21 @@ namespace Godot } /// - /// Translates the transform by the given , - /// relative to the transform's basis vectors. - /// - /// Unlike and , - /// this does not use matrix multiplication. + /// Translates the transform by the given . + /// The operation is done in the parent/global frame, equivalent to + /// multiplying the matrix from the left. + /// + /// The offset to translate by. + /// The translated matrix. + public Transform3D Translated(Vector3 offset) + { + return new Transform3D(basis, origin + offset); + } + + /// + /// Translates the transform by the given . + /// The operation is done in the local frame, equivalent to + /// multiplying the matrix from the right. /// /// The offset to translate by. /// The translated matrix. -- cgit v1.2.3