diff --git a/Source/Engine/Core/Math/Matrix.cpp b/Source/Engine/Core/Math/Matrix.cpp index 7269d4e68..db255e627 100644 --- a/Source/Engine/Core/Math/Matrix.cpp +++ b/Source/Engine/Core/Math/Matrix.cpp @@ -40,9 +40,7 @@ float Matrix::GetDeterminant() const const float temp4 = M31 * M44 - M34 * M41; const float temp5 = M31 * M43 - M33 * M41; const float temp6 = M31 * M42 - M32 * M41; - return M11 * (M22 * temp1 - M23 * temp2 + M24 * temp3) - M12 * (M21 * temp1 - - M23 * temp4 + M24 * temp5) + M13 * (M21 * temp2 - M22 * temp4 + M24 * temp6) - - M14 * (M21 * temp3 - M22 * temp5 + M23 * temp6); + return M11 * (M22 * temp1 - M23 * temp2 + M24 * temp3) - M12 * (M21 * temp1 -M23 * temp4 + M24 * temp5) + M13 * (M21 * temp2 - M22 * temp4 + M24 * temp6) - M14 * (M21 * temp3 - M22 * temp5 + M23 * temp6); } float Matrix::RotDeterminant() const @@ -153,6 +151,179 @@ void Matrix::Decompose(Float3& scale, Matrix& rotation, Float3& translation) con rotation = Matrix(r); } +bool Matrix::operator==(const Matrix& other) const +{ + for (int32 i = 0; i < 16; i++) + { + if (Math::NotNearEqual(other.Raw[i], Raw[i])) + return false; + } + return true; +} + +void Matrix::Add(const Matrix& left, const Matrix& right, Matrix& result) +{ + result.M11 = left.M11 + right.M11; + result.M12 = left.M12 + right.M12; + result.M13 = left.M13 + right.M13; + result.M14 = left.M14 + right.M14; + result.M21 = left.M21 + right.M21; + result.M22 = left.M22 + right.M22; + result.M23 = left.M23 + right.M23; + result.M24 = left.M24 + right.M24; + result.M31 = left.M31 + right.M31; + result.M32 = left.M32 + right.M32; + result.M33 = left.M33 + right.M33; + result.M34 = left.M34 + right.M34; + result.M41 = left.M41 + right.M41; + result.M42 = left.M42 + right.M42; + result.M43 = left.M43 + right.M43; + result.M44 = left.M44 + right.M44; +} + +void Matrix::Subtract(const Matrix& left, const Matrix& right, Matrix& result) +{ + result.M11 = left.M11 - right.M11; + result.M12 = left.M12 - right.M12; + result.M13 = left.M13 - right.M13; + result.M14 = left.M14 - right.M14; + result.M21 = left.M21 - right.M21; + result.M22 = left.M22 - right.M22; + result.M23 = left.M23 - right.M23; + result.M24 = left.M24 - right.M24; + result.M31 = left.M31 - right.M31; + result.M32 = left.M32 - right.M32; + result.M33 = left.M33 - right.M33; + result.M34 = left.M34 - right.M34; + result.M41 = left.M41 - right.M41; + result.M42 = left.M42 - right.M42; + result.M43 = left.M43 - right.M43; + result.M44 = left.M44 - right.M44; +} + +void Matrix::Multiply(const Matrix& left, float right, Matrix& result) +{ + result.M11 = left.M11 * right; + result.M12 = left.M12 * right; + result.M13 = left.M13 * right; + result.M14 = left.M14 * right; + result.M21 = left.M21 * right; + result.M22 = left.M22 * right; + result.M23 = left.M23 * right; + result.M24 = left.M24 * right; + result.M31 = left.M31 * right; + result.M32 = left.M32 * right; + result.M33 = left.M33 * right; + result.M34 = left.M34 * right; + result.M41 = left.M41 * right; + result.M42 = left.M42 * right; + result.M43 = left.M43 * right; + result.M44 = left.M44 * right; +} + +void Matrix::Multiply(const Matrix& left, const Matrix& right, Matrix& result) +{ + result.M11 = left.M11 * right.M11 + left.M12 * right.M21 + left.M13 * right.M31 + left.M14 * right.M41; + result.M12 = left.M11 * right.M12 + left.M12 * right.M22 + left.M13 * right.M32 + left.M14 * right.M42; + result.M13 = left.M11 * right.M13 + left.M12 * right.M23 + left.M13 * right.M33 + left.M14 * right.M43; + result.M14 = left.M11 * right.M14 + left.M12 * right.M24 + left.M13 * right.M34 + left.M14 * right.M44; + result.M21 = left.M21 * right.M11 + left.M22 * right.M21 + left.M23 * right.M31 + left.M24 * right.M41; + result.M22 = left.M21 * right.M12 + left.M22 * right.M22 + left.M23 * right.M32 + left.M24 * right.M42; + result.M23 = left.M21 * right.M13 + left.M22 * right.M23 + left.M23 * right.M33 + left.M24 * right.M43; + result.M24 = left.M21 * right.M14 + left.M22 * right.M24 + left.M23 * right.M34 + left.M24 * right.M44; + result.M31 = left.M31 * right.M11 + left.M32 * right.M21 + left.M33 * right.M31 + left.M34 * right.M41; + result.M32 = left.M31 * right.M12 + left.M32 * right.M22 + left.M33 * right.M32 + left.M34 * right.M42; + result.M33 = left.M31 * right.M13 + left.M32 * right.M23 + left.M33 * right.M33 + left.M34 * right.M43; + result.M34 = left.M31 * right.M14 + left.M32 * right.M24 + left.M33 * right.M34 + left.M34 * right.M44; + result.M41 = left.M41 * right.M11 + left.M42 * right.M21 + left.M43 * right.M31 + left.M44 * right.M41; + result.M42 = left.M41 * right.M12 + left.M42 * right.M22 + left.M43 * right.M32 + left.M44 * right.M42; + result.M43 = left.M41 * right.M13 + left.M42 * right.M23 + left.M43 * right.M33 + left.M44 * right.M43; + result.M44 = left.M41 * right.M14 + left.M42 * right.M24 + left.M43 * right.M34 + left.M44 * right.M44; +} + +void Matrix::Divide(const Matrix& left, float right, Matrix& result) +{ + ASSERT(!Math::IsZero(right)); + const float inv = 1.0f / right; + + result.M11 = left.M11 * inv; + result.M12 = left.M12 * inv; + result.M13 = left.M13 * inv; + result.M14 = left.M14 * inv; + result.M21 = left.M21 * inv; + result.M22 = left.M22 * inv; + result.M23 = left.M23 * inv; + result.M24 = left.M24 * inv; + result.M31 = left.M31 * inv; + result.M32 = left.M32 * inv; + result.M33 = left.M33 * inv; + result.M34 = left.M34 * inv; + result.M41 = left.M41 * inv; + result.M42 = left.M42 * inv; + result.M43 = left.M43 * inv; + result.M44 = left.M44 * inv; +} + +void Matrix::Divide(const Matrix& left, const Matrix& right, Matrix& result) +{ + result.M11 = left.M11 / right.M11; + result.M12 = left.M12 / right.M12; + result.M13 = left.M13 / right.M13; + result.M14 = left.M14 / right.M14; + result.M21 = left.M21 / right.M21; + result.M22 = left.M22 / right.M22; + result.M23 = left.M23 / right.M23; + result.M24 = left.M24 / right.M24; + result.M31 = left.M31 / right.M31; + result.M32 = left.M32 / right.M32; + result.M33 = left.M33 / right.M33; + result.M34 = left.M34 / right.M34; + result.M41 = left.M41 / right.M41; + result.M42 = left.M42 / right.M42; + result.M43 = left.M43 / right.M43; + result.M44 = left.M44 / right.M44; +} + +void Matrix::Negate(const Matrix& value, Matrix& result) +{ + result.M11 = -value.M11; + result.M12 = -value.M12; + result.M13 = -value.M13; + result.M14 = -value.M14; + result.M21 = -value.M21; + result.M22 = -value.M22; + result.M23 = -value.M23; + result.M24 = -value.M24; + result.M31 = -value.M31; + result.M32 = -value.M32; + result.M33 = -value.M33; + result.M34 = -value.M34; + result.M41 = -value.M41; + result.M42 = -value.M42; + result.M43 = -value.M43; + result.M44 = -value.M44; +} + +void Matrix::Lerp(const Matrix& start, const Matrix& end, float amount, Matrix& result) +{ + result.M11 = Math::Lerp(start.M11, end.M11, amount); + result.M12 = Math::Lerp(start.M12, end.M12, amount); + result.M13 = Math::Lerp(start.M13, end.M13, amount); + result.M14 = Math::Lerp(start.M14, end.M14, amount); + result.M21 = Math::Lerp(start.M21, end.M21, amount); + result.M22 = Math::Lerp(start.M22, end.M22, amount); + result.M23 = Math::Lerp(start.M23, end.M23, amount); + result.M24 = Math::Lerp(start.M24, end.M24, amount); + result.M31 = Math::Lerp(start.M31, end.M31, amount); + result.M32 = Math::Lerp(start.M32, end.M32, amount); + result.M33 = Math::Lerp(start.M33, end.M33, amount); + result.M34 = Math::Lerp(start.M34, end.M34, amount); + result.M41 = Math::Lerp(start.M41, end.M41, amount); + result.M42 = Math::Lerp(start.M42, end.M42, amount); + result.M43 = Math::Lerp(start.M43, end.M43, amount); + result.M44 = Math::Lerp(start.M44, end.M44, amount); +} + Matrix Matrix::Transpose(const Matrix& value) { Matrix result; @@ -688,6 +859,15 @@ void Matrix::CreateFromAxisAngle(const Float3& axis, float angle, Matrix& result result.M44 = 1.0f; } +Vector3 Matrix::TransformVector(const Matrix& m, const Vector3& v) +{ + return Vector3( + m.Values[0][0] * v.Raw[0] + m.Values[1][0] * v.Raw[1] + m.Values[2][0] * v.Raw[2], + m.Values[0][1] * v.Raw[0] + m.Values[1][1] * v.Raw[1] + m.Values[2][1] * v.Raw[2], + m.Values[0][2] * v.Raw[0] + m.Values[1][2] * v.Raw[1] + m.Values[2][2] * v.Raw[2] + ); +} + Float4 Matrix::TransformPosition(const Matrix& m, const Float3& v) { return Float4( diff --git a/Source/Engine/Core/Math/Matrix.h b/Source/Engine/Core/Math/Matrix.h index 11fbe1e78..906000592 100644 --- a/Source/Engine/Core/Math/Matrix.h +++ b/Source/Engine/Core/Math/Matrix.h @@ -523,24 +523,10 @@ public: return *this; } - bool operator==(const Matrix& other) const - { - for (int32 i = 0; i < 16; i++) - { - if (Math::NotNearEqual(other.Raw[i], Raw[i])) - return false; - } - return true; - } - + bool operator==(const Matrix& other) const; bool operator!=(const Matrix& other) const { - for (int32 i = 0; i < 16; i++) - { - if (Math::NotNearEqual(other.Raw[i], Raw[i])) - return true; - } - return false; + return !operator==(other); } public: @@ -548,73 +534,19 @@ public: // @param left The first matrix to add. // @param right The second matrix to add. // @param result When the method completes, contains the sum of the two matrices. - static void Add(const Matrix& left, const Matrix& right, Matrix& result) - { - result.M11 = left.M11 + right.M11; - result.M12 = left.M12 + right.M12; - result.M13 = left.M13 + right.M13; - result.M14 = left.M14 + right.M14; - result.M21 = left.M21 + right.M21; - result.M22 = left.M22 + right.M22; - result.M23 = left.M23 + right.M23; - result.M24 = left.M24 + right.M24; - result.M31 = left.M31 + right.M31; - result.M32 = left.M32 + right.M32; - result.M33 = left.M33 + right.M33; - result.M34 = left.M34 + right.M34; - result.M41 = left.M41 + right.M41; - result.M42 = left.M42 + right.M42; - result.M43 = left.M43 + right.M43; - result.M44 = left.M44 + right.M44; - } + static void Add(const Matrix& left, const Matrix& right, Matrix& result); // Calculates the difference between two matrices. // @param left The first matrix to subtract. // @param right The second matrix to subtract. // @param result When the method completes, contains the difference between the two matrices. - static void Subtract(const Matrix& left, const Matrix& right, Matrix& result) - { - result.M11 = left.M11 - right.M11; - result.M12 = left.M12 - right.M12; - result.M13 = left.M13 - right.M13; - result.M14 = left.M14 - right.M14; - result.M21 = left.M21 - right.M21; - result.M22 = left.M22 - right.M22; - result.M23 = left.M23 - right.M23; - result.M24 = left.M24 - right.M24; - result.M31 = left.M31 - right.M31; - result.M32 = left.M32 - right.M32; - result.M33 = left.M33 - right.M33; - result.M34 = left.M34 - right.M34; - result.M41 = left.M41 - right.M41; - result.M42 = left.M42 - right.M42; - result.M43 = left.M43 - right.M43; - result.M44 = left.M44 - right.M44; - } + static void Subtract(const Matrix& left, const Matrix& right, Matrix& result); // Scales a matrix by the given value. // @param left The matrix to scale. // @param right The amount by which to scale. // @param result When the method completes, contains the scaled matrix. - static void Multiply(const Matrix& left, float right, Matrix& result) - { - result.M11 = left.M11 * right; - result.M12 = left.M12 * right; - result.M13 = left.M13 * right; - result.M14 = left.M14 * right; - result.M21 = left.M21 * right; - result.M22 = left.M22 * right; - result.M23 = left.M23 * right; - result.M24 = left.M24 * right; - result.M31 = left.M31 * right; - result.M32 = left.M32 * right; - result.M33 = left.M33 * right; - result.M34 = left.M34 * right; - result.M41 = left.M41 * right; - result.M42 = left.M42 * right; - result.M43 = left.M43 * right; - result.M44 = left.M44 * right; - } + static void Multiply(const Matrix& left, float right, Matrix& result); // Calculates the product of two matrices. // @param left The first matrix to multiply. @@ -631,124 +563,31 @@ public: // @param left The first matrix to multiply. // @param right The second matrix to multiply. // @param result The product of the two matrices. - static void Multiply(const Matrix& left, const Matrix& right, Matrix& result) - { - result.M11 = left.M11 * right.M11 + left.M12 * right.M21 + left.M13 * right.M31 + left.M14 * right.M41; - result.M12 = left.M11 * right.M12 + left.M12 * right.M22 + left.M13 * right.M32 + left.M14 * right.M42; - result.M13 = left.M11 * right.M13 + left.M12 * right.M23 + left.M13 * right.M33 + left.M14 * right.M43; - result.M14 = left.M11 * right.M14 + left.M12 * right.M24 + left.M13 * right.M34 + left.M14 * right.M44; - result.M21 = left.M21 * right.M11 + left.M22 * right.M21 + left.M23 * right.M31 + left.M24 * right.M41; - result.M22 = left.M21 * right.M12 + left.M22 * right.M22 + left.M23 * right.M32 + left.M24 * right.M42; - result.M23 = left.M21 * right.M13 + left.M22 * right.M23 + left.M23 * right.M33 + left.M24 * right.M43; - result.M24 = left.M21 * right.M14 + left.M22 * right.M24 + left.M23 * right.M34 + left.M24 * right.M44; - result.M31 = left.M31 * right.M11 + left.M32 * right.M21 + left.M33 * right.M31 + left.M34 * right.M41; - result.M32 = left.M31 * right.M12 + left.M32 * right.M22 + left.M33 * right.M32 + left.M34 * right.M42; - result.M33 = left.M31 * right.M13 + left.M32 * right.M23 + left.M33 * right.M33 + left.M34 * right.M43; - result.M34 = left.M31 * right.M14 + left.M32 * right.M24 + left.M33 * right.M34 + left.M34 * right.M44; - result.M41 = left.M41 * right.M11 + left.M42 * right.M21 + left.M43 * right.M31 + left.M44 * right.M41; - result.M42 = left.M41 * right.M12 + left.M42 * right.M22 + left.M43 * right.M32 + left.M44 * right.M42; - result.M43 = left.M41 * right.M13 + left.M42 * right.M23 + left.M43 * right.M33 + left.M44 * right.M43; - result.M44 = left.M41 * right.M14 + left.M42 * right.M24 + left.M43 * right.M34 + left.M44 * right.M44; - } + static void Multiply(const Matrix& left, const Matrix& right, Matrix& result); // Scales a matrix by the given value. // @param left The matrix to scale. // @param right The amount by which to scale. // @param result When the method completes, contains the scaled matrix. - static void Divide(const Matrix& left, float right, Matrix& result) - { - ASSERT(!Math::IsZero(right)); - const float inv = 1.0f / right; - - result.M11 = left.M11 * inv; - result.M12 = left.M12 * inv; - result.M13 = left.M13 * inv; - result.M14 = left.M14 * inv; - result.M21 = left.M21 * inv; - result.M22 = left.M22 * inv; - result.M23 = left.M23 * inv; - result.M24 = left.M24 * inv; - result.M31 = left.M31 * inv; - result.M32 = left.M32 * inv; - result.M33 = left.M33 * inv; - result.M34 = left.M34 * inv; - result.M41 = left.M41 * inv; - result.M42 = left.M42 * inv; - result.M43 = left.M43 * inv; - result.M44 = left.M44 * inv; - } + static void Divide(const Matrix& left, float right, Matrix& result); // Calculates the quotient of two matrices. // @param left The first matrix to divide. // @param right The second matrix to divide. // @param result When the method completes, contains the quotient of the two matrices. - static void Divide(const Matrix& left, const Matrix& right, Matrix& result) - { - result.M11 = left.M11 / right.M11; - result.M12 = left.M12 / right.M12; - result.M13 = left.M13 / right.M13; - result.M14 = left.M14 / right.M14; - result.M21 = left.M21 / right.M21; - result.M22 = left.M22 / right.M22; - result.M23 = left.M23 / right.M23; - result.M24 = left.M24 / right.M24; - result.M31 = left.M31 / right.M31; - result.M32 = left.M32 / right.M32; - result.M33 = left.M33 / right.M33; - result.M34 = left.M34 / right.M34; - result.M41 = left.M41 / right.M41; - result.M42 = left.M42 / right.M42; - result.M43 = left.M43 / right.M43; - result.M44 = left.M44 / right.M44; - } + static void Divide(const Matrix& left, const Matrix& right, Matrix& result); // Negates a matrix. // @param value The matrix to be negated. // @param result When the method completes, contains the negated matrix. - static void Negate(const Matrix& value, Matrix& result) - { - result.M11 = -value.M11; - result.M12 = -value.M12; - result.M13 = -value.M13; - result.M14 = -value.M14; - result.M21 = -value.M21; - result.M22 = -value.M22; - result.M23 = -value.M23; - result.M24 = -value.M24; - result.M31 = -value.M31; - result.M32 = -value.M32; - result.M33 = -value.M33; - result.M34 = -value.M34; - result.M41 = -value.M41; - result.M42 = -value.M42; - result.M43 = -value.M43; - result.M44 = -value.M44; - } + static void Negate(const Matrix& value, Matrix& result); // Performs a linear interpolation between two matrices. // @param start Start matrix. // @param end End matrix. // @param amount Value between 0 and 1 indicating the weight of end. // @param result When the method completes, contains the linear interpolation of the two matrices. - static void Lerp(const Matrix& start, const Matrix& end, float amount, Matrix& result) - { - result.M11 = Math::Lerp(start.M11, end.M11, amount); - result.M12 = Math::Lerp(start.M12, end.M12, amount); - result.M13 = Math::Lerp(start.M13, end.M13, amount); - result.M14 = Math::Lerp(start.M14, end.M14, amount); - result.M21 = Math::Lerp(start.M21, end.M21, amount); - result.M22 = Math::Lerp(start.M22, end.M22, amount); - result.M23 = Math::Lerp(start.M23, end.M23, amount); - result.M24 = Math::Lerp(start.M24, end.M24, amount); - result.M31 = Math::Lerp(start.M31, end.M31, amount); - result.M32 = Math::Lerp(start.M32, end.M32, amount); - result.M33 = Math::Lerp(start.M33, end.M33, amount); - result.M34 = Math::Lerp(start.M34, end.M34, amount); - result.M41 = Math::Lerp(start.M41, end.M41, amount); - result.M42 = Math::Lerp(start.M42, end.M42, amount); - result.M43 = Math::Lerp(start.M43, end.M43, amount); - result.M44 = Math::Lerp(start.M44, end.M44, amount); - } + static void Lerp(const Matrix& start, const Matrix& end, float amount, Matrix& result); // Performs a cubic interpolation between two matrices. // @param start Start matrix. @@ -1131,6 +970,7 @@ public: static void CreateFromAxisAngle(const Float3& axis, float angle, Matrix& result); public: + static Vector3 TransformVector(const Matrix& m, const Vector3& v); static Float4 TransformPosition(const Matrix& m, const Float3& v); static Float4 TransformPosition(const Matrix& m, const Float4& v); }; diff --git a/Source/Engine/Core/Math/Matrix3x3.cpp b/Source/Engine/Core/Math/Matrix3x3.cpp index 65a1b6b7e..f7a6298af 100644 --- a/Source/Engine/Core/Math/Matrix3x3.cpp +++ b/Source/Engine/Core/Math/Matrix3x3.cpp @@ -23,6 +23,11 @@ String Matrix3x3::ToString() const return String::Format(TEXT("{}"), *this); } +float Matrix3x3::GetDeterminant() const +{ + return M11 * M22 * M33 + M12 * M23 * M31 + M13 * M21 * M32 - M13 * M22 * M31 - M12 * M21 * M33 - M11 * M23 * M32; +} + void Matrix3x3::NormalizeScale() { const float scaleX = 1.0f / Float3(M11, M21, M31).Length(); diff --git a/Source/Engine/Core/Math/Matrix3x3.h b/Source/Engine/Core/Math/Matrix3x3.h index 0680e7735..177c33d19 100644 --- a/Source/Engine/Core/Math/Matrix3x3.h +++ b/Source/Engine/Core/Math/Matrix3x3.h @@ -312,10 +312,7 @@ public: /// /// Calculates the determinant of the Matrix3x3. /// - float GetDeterminant() const - { - return M11 * M22 * M33 + M12 * M23 * M31 + M13 * M21 * M32 - M13 * M22 * M31 - M12 * M21 * M33 - M11 * M23 * M32; - } + float GetDeterminant() const; public: /// @@ -352,7 +349,7 @@ public: /// /// The Matrix3x3 whose inverse is to be calculated. /// The inverse of the specified Matrix3x3. - static Matrix3x3 Invert(Matrix3x3 value) + static Matrix3x3 Invert(const Matrix3x3& value) { Matrix3x3 result; Invert(value, result); diff --git a/Source/Engine/Core/Math/Transform.cpp b/Source/Engine/Core/Math/Transform.cpp index f08de8279..463182415 100644 --- a/Source/Engine/Core/Math/Transform.cpp +++ b/Source/Engine/Core/Math/Transform.cpp @@ -18,6 +18,16 @@ String Transform::ToString() const return String::Format(TEXT("{}"), *this); } +bool Transform::IsIdentity() const +{ + return Translation.IsZero() && Orientation.IsIdentity() && Scale.IsOne(); +} + +bool Transform::IsNanOrInfinity() const +{ + return Translation.IsNanOrInfinity() || Orientation.IsNanOrInfinity() || Scale.IsNanOrInfinity(); +} + Matrix Transform::GetRotation() const { Matrix result; @@ -188,6 +198,15 @@ void Transform::WorldToLocalVector(const Vector3& vector, Vector3& result) const result *= invScale; } +void Transform::WorldToLocal(const Quaternion& rotation, Quaternion& result) const +{ + Quaternion orientation = Orientation; + orientation.Conjugate(); + Quaternion::Multiply(orientation, rotation, orientation); + orientation.Normalize(); + result = orientation; +} + Float3 Transform::GetRight() const { return Float3::Transform(Float3::Right, Orientation); diff --git a/Source/Engine/Core/Math/Transform.h b/Source/Engine/Core/Math/Transform.h index f61ce8b9c..3811ce0be 100644 --- a/Source/Engine/Core/Math/Transform.h +++ b/Source/Engine/Core/Math/Transform.h @@ -90,18 +90,12 @@ public: /// /// Checks if transform is an identity transformation. /// - bool IsIdentity() const - { - return Translation.IsZero() && Orientation.IsIdentity() && Scale.IsOne(); - } + bool IsIdentity() const; /// /// Returns true if transform has one or more components equal to +/- infinity or NaN. /// - bool IsNanOrInfinity() const - { - return Translation.IsNanOrInfinity() || Orientation.IsNanOrInfinity() || Scale.IsNanOrInfinity(); - } + bool IsNanOrInfinity() const; /// /// Calculates the determinant of this transformation. @@ -278,6 +272,25 @@ public: return result; } + /// + /// Performs transformation of the given rotation in world space to the local space of this transform. + /// + /// The world space rotation. + /// The local space rotation. + void WorldToLocal(const Quaternion& rotation, Quaternion& result) const; + + /// + /// Performs transformation of the given rotation in world space to the local space of this transform. + /// + /// The world space rotation. + /// The local space rotation. + Quaternion WorldToLocal(const Quaternion& rotation) const + { + Quaternion result; + WorldToLocal(rotation, result); + return result; + } + public: FORCE_INLINE Transform operator*(const Transform& other) const { diff --git a/Source/Engine/Level/Actor.cpp b/Source/Engine/Level/Actor.cpp index 64ad37b8e..60646cdbf 100644 --- a/Source/Engine/Level/Actor.cpp +++ b/Source/Engine/Level/Actor.cpp @@ -319,13 +319,9 @@ void Actor::SetParent(Actor* value, bool worldPositionsStays, bool canBreakPrefa if (worldPositionsStays) { if (_parent) - { - _parent->GetTransform().WorldToLocal(prevTransform, _localTransform); - } + _parent->_transform.WorldToLocal(prevTransform, _localTransform); else - { _localTransform = prevTransform; - } } // Fire events @@ -622,7 +618,7 @@ void Actor::SetTransform(const Transform& value) if (!(Vector3::NearEqual(_transform.Translation, value.Translation) && Quaternion::NearEqual(_transform.Orientation, value.Orientation, ACTOR_ORIENTATION_EPSILON) && Float3::NearEqual(_transform.Scale, value.Scale))) { if (_parent) - _parent->GetTransform().WorldToLocal(value, _localTransform); + _parent->_transform.WorldToLocal(value, _localTransform); else _localTransform = value; OnTransformChanged(); @@ -635,7 +631,7 @@ void Actor::SetPosition(const Vector3& value) if (!Vector3::NearEqual(_transform.Translation, value)) { if (_parent) - _localTransform.Translation = _parent->GetTransform().WorldToLocal(value); + _localTransform.Translation = _parent->_transform.WorldToLocal(value); else _localTransform.Translation = value; OnTransformChanged(); @@ -648,16 +644,9 @@ void Actor::SetOrientation(const Quaternion& value) if (!Quaternion::NearEqual(_transform.Orientation, value, ACTOR_ORIENTATION_EPSILON)) { if (_parent) - { - _localTransform.Orientation = _parent->GetOrientation(); - _localTransform.Orientation.Invert(); - Quaternion::Multiply(_localTransform.Orientation, value, _localTransform.Orientation); - _localTransform.Orientation.Normalize(); - } + _parent->_transform.WorldToLocal(value, _localTransform.Orientation); else - { _localTransform.Orientation = value; - } OnTransformChanged(); } }