From c34faade8679b7d34735ad47332e445bafc4abb8 Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Mon, 24 Oct 2022 02:45:47 +0800 Subject: [PATCH 1/4] Converted doubles to floats in SHADE Managed. Added Quaternions. --- SHADE_Managed/Quaternion.hxx | 17 -- SHADE_Managed/src/Math/Math.cxx | 14 +- SHADE_Managed/src/Math/Math.hxx | 24 +-- SHADE_Managed/src/Math/Quaternion.cxx | 170 ++++++++++++++++++ SHADE_Managed/src/Math/Quaternion.hxx | 237 ++++++++++++++++++++++++++ SHADE_Managed/src/Math/Vector2.cxx | 52 +++--- SHADE_Managed/src/Math/Vector2.hxx | 76 ++++----- SHADE_Managed/src/Math/Vector3.cxx | 61 +++---- SHADE_Managed/src/Math/Vector3.hxx | 98 +++++------ SHADE_Managed/src/Utility/Convert.cxx | 19 ++- SHADE_Managed/src/Utility/Convert.hxx | 14 ++ 11 files changed, 596 insertions(+), 186 deletions(-) delete mode 100644 SHADE_Managed/Quaternion.hxx create mode 100644 SHADE_Managed/src/Math/Quaternion.cxx create mode 100644 SHADE_Managed/src/Math/Quaternion.hxx diff --git a/SHADE_Managed/Quaternion.hxx b/SHADE_Managed/Quaternion.hxx deleted file mode 100644 index 0b07a34e..00000000 --- a/SHADE_Managed/Quaternion.hxx +++ /dev/null @@ -1,17 +0,0 @@ -/************************************************************************************//*! -\file Quaternion.hxx -\author Diren D Bharwani, diren.dbharwani, 390002520 -\par email: diren.dbharwani\@digipen.edu -\date Oct 23, 2022 -\brief Contains the definitions of Quaternion struct. - - Note: This file is written in C++17/CLI. - -Copyright (C) 2021 DigiPen Institute of Technology. -Reproduction or disclosure of this file or its contents without the prior written consent -of DigiPen Institute of Technology is prohibited. -*//*************************************************************************************/ - -#pragma once - -// TODO(Diren) diff --git a/SHADE_Managed/src/Math/Math.cxx b/SHADE_Managed/src/Math/Math.cxx index 5ec850a1..fa72e2b6 100644 --- a/SHADE_Managed/src/Math/Math.cxx +++ b/SHADE_Managed/src/Math/Math.cxx @@ -21,7 +21,7 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ /* Utility Functions */ /*---------------------------------------------------------------------------------*/ - double Math::Wrap(double value, double min, double max) + float Math::Wrap(float value, float min, float max) { while (value < min) { @@ -33,24 +33,24 @@ namespace SHADE } return value; } - double Math::DegreesToRadians(double degrees) + float Math::DegreesToRadians(float degrees) { return degrees * Deg2Rad; } - double Math::RadiansToDegrees(double radians) + float Math::RadiansToDegrees(float radians) { return radians * Rad2Deg; } - double Math::Lerp(double a, double b, double t) + float Math::Lerp(float a, float b, float t) { - return LerpUnclamped(a, b, System::Math::Clamp(t, 0.0, 1.0)); + return LerpUnclamped(a, b, System::Math::Clamp(t, 0.0f, 1.0f)); } - double Math::LerpUnclamped(double a, double b, double t) + float Math::LerpUnclamped(float a, float b, float t) { return a + t * (b - a); } - double Math::InverseLerp(double a, double b, double value) + float Math::InverseLerp(float a, float b, float value) { return (value - a) / (b - a); } diff --git a/SHADE_Managed/src/Math/Math.hxx b/SHADE_Managed/src/Math/Math.hxx index 3ddc5149..1578d97c 100644 --- a/SHADE_Managed/src/Math/Math.hxx +++ b/SHADE_Managed/src/Math/Math.hxx @@ -27,11 +27,11 @@ namespace SHADE /// /// Degrees-to-radians conversion constant /// - static constexpr double Deg2Rad = System::Math::PI / 180.0; + static constexpr float Deg2Rad = System::Math::PI / 180.0f; /// /// Radians-to-degrees conversion constant /// - static constexpr double Rad2Deg = 180.0 / System::Math::PI; + static constexpr float Rad2Deg = 180.0f / System::Math::PI; /// /// Small value used for single precision floating point comparisons. /// @@ -47,28 +47,28 @@ namespace SHADE /// Minimum value to wrap at. /// Maximum value to wrap at. /// Wrapped value. - static double Wrap(double value, double min, double max); + static float Wrap(float value, float min, float max); /// /// Converts an angle from degree representation to radian representation. /// /// Degree-based angle to convert. /// The specified angle in radians. - static double DegreesToRadians(double degrees); + static float DegreesToRadians(float degrees); /// /// Converts an angle from radian representation to degree representation. /// /// Radian-based angle to convert. /// The specified angle in degrees. - static double RadiansToDegrees(double radians); + static float RadiansToDegrees(float radians); /// /// Linearly interpolates between a and b by t. /// The parameter t is clamped to the range [0, 1]. /// /// The start value. /// The end value. - /// The interpolation value between the two double. - /// The interpolated double result between the two double values. - static double Lerp(double a, double b, double t); + /// The interpolation value between the two float. + /// The interpolated float result between the two float values. + static float Lerp(float a, float b, float t); /// /// Linearly interpolates between a and b by t. /// The parameter t is not clamped and a value based on a and b is supported. @@ -77,9 +77,9 @@ namespace SHADE /// /// The start value. /// The end value. - /// The interpolation value between the two double. - /// The interpolated double result between the two double values. - static double LerpUnclamped(double a, double b, double t); + /// The interpolation value between the two float. + /// The interpolated float result between the two float values. + static float LerpUnclamped(float a, float b, float t); /// /// Calculates the linear parameter t that produces the interpolant value within the range [a, b]. /// @@ -87,6 +87,6 @@ namespace SHADE /// End value. /// Value between start and end. /// Percentage of value between start and end. - static double InverseLerp(double a, double b, double value); + static float InverseLerp(float a, float b, float value); }; } diff --git a/SHADE_Managed/src/Math/Quaternion.cxx b/SHADE_Managed/src/Math/Quaternion.cxx new file mode 100644 index 00000000..863241ac --- /dev/null +++ b/SHADE_Managed/src/Math/Quaternion.cxx @@ -0,0 +1,170 @@ +/************************************************************************************//*! +\file Quaternion.cxx +\author Diren D Bharwani, diren.dbharwani, 390002520 +\par email: diren.dbharwani\@digipen.edu +\date Oct 23, 2022 +\brief Contains the definitions of functions in the Quaternion struct. + + Note: This file is written in C++17/CLI. + +Copyright (C) 2021 DigiPen Institute of Technology. +Reproduction or disclosure of this file or its contents without the prior written consent +of DigiPen Institute of Technology is prohibited. +*//*************************************************************************************/ + +// Precompiled Headers +#include "SHpch.h" +// Primary Header +#include "Quaternion.hxx" +// External Dependencies +#include "Math/SHQuaternion.h" +#include "Math/Vector/SHVec4.h" +// Project Headers +#include "Utility/Convert.hxx" +#include "Math.hxx" + +namespace SHADE +{ + /*---------------------------------------------------------------------------------*/ + /* Constructors */ + /*---------------------------------------------------------------------------------*/ + + Quaternion::Quaternion(float _x, float _y, float _z, float _w) + : x { _x } + , y { _y } + , z { _z } + , w { _w } + {} + + /*---------------------------------------------------------------------------------*/ + /* Usage Functions */ + /*---------------------------------------------------------------------------------*/ + + void Quaternion::SetFromToRotation(Vector3 fromDirection, Vector3 toDirection) + { + const SHQuaternion R = SHQuaternion::FromToRotation(Convert::ToNative(fromDirection), Convert::ToNative(toDirection)); + *this = Convert::ToCLI(R); + } + + void Quaternion::SetLookRotation(Vector3 view, Vector3 up) + { + const SHQuaternion R = SHQuaternion::LookRotation(Convert::ToNative(view), Convert::ToNative(up)); + *this = Convert::ToCLI(R); + } + + void Quaternion::ToAngleAxis(float^% angle, Vector3^% axis) + { + const SHVec4 NATIVE_AXIS_ANGLE = Convert::ToNative(*this).GetAxisAngle(); + axis = Convert::ToCLI(NATIVE_AXIS_ANGLE.ToVec3()); + angle = NATIVE_AXIS_ANGLE.w; + } + + System::String^ Quaternion::ToString() + { + return ValueType::ToString(); + } + + /*---------------------------------------------------------------------------------*/ + /* IEquatable */ + /*---------------------------------------------------------------------------------*/ + + bool Quaternion::Equals(Quaternion other) + { + const float DOT = Dot(*this, other); + return fabs(1.0f - DOT) <= Math::Epsilon; + } + + /*---------------------------------------------------------------------------------*/ + /* Object Overrides */ + /*---------------------------------------------------------------------------------*/ + + bool Quaternion::Equals(Object^ o) + { + return ValueType::Equals(o); + } + + int Quaternion::GetHashCode() + { + return ValueType::GetHashCode(); + } + + /*---------------------------------------------------------------------------------*/ + /* Static Functions */ + /*---------------------------------------------------------------------------------*/ + + float Quaternion::Angle(Quaternion a, Quaternion b) + { + return SHQuaternion::Angle(Convert::ToNative(a), Convert::ToNative(b)); + } + + Quaternion Quaternion::AngleAxis(float angle, Vector3 axis) + { + return Convert::ToCLI(SHQuaternion::FromAxisAngle(Convert::ToNative(axis), angle)); + } + + float Quaternion::Dot(Quaternion a, Quaternion b) + { + return (a.x * b.x) + (a.y * b.y) + (a.z * b.z) + (a.w * b.w); + } + + Quaternion Quaternion::Euler(float _x, float _y, float _z) + { + return Convert::ToCLI(SHQuaternion::FromPitchYawRoll(_x, _y, _z)); + } + + Quaternion Quaternion::FromToRotation(Vector3 fromDirection, Vector3 toDirection) + { + return Convert::ToCLI(SHQuaternion::FromToRotation(Convert::ToNative(fromDirection), Convert::ToNative(toDirection))); + } + + Quaternion Quaternion::Inverse(Quaternion rotation) + { + return Convert::ToCLI(SHQuaternion::Inverse(Convert::ToNative(rotation))); + } + + Quaternion Quaternion::Lerp(Quaternion a, Quaternion b, float t) + { + return Convert::ToCLI(SHQuaternion::ClampedLerp(Convert::ToNative(a), Convert::ToNative(b), t)); + } + + Quaternion Quaternion::LerpUnclamped(Quaternion a, Quaternion b, float t) + { + return Convert::ToCLI(SHQuaternion::Lerp(Convert::ToNative(a), Convert::ToNative(b), t)); + } + + Quaternion Quaternion::LookRotation(Vector3 forward, Vector3 upwards) + { + return Convert::ToCLI(SHQuaternion::LookRotation(Convert::ToNative(forward), Convert::ToNative(upwards))); + } + + Quaternion Quaternion::Normalize(Quaternion q) + { + return Convert::ToCLI(SHQuaternion::Normalise(Convert::ToNative(q))); + } + + Quaternion Quaternion::RotateTowards(Quaternion from, Quaternion to, float maxDegreesDelta) + { + return Convert::ToCLI(SHQuaternion::RotateTowards(Convert::ToNative(from), Convert::ToNative(to), Math::DegreesToRadians(maxDegreesDelta))); + } + + Quaternion Quaternion::Slerp(Quaternion a, Quaternion b, float t) + { + return Convert::ToCLI(SHQuaternion::ClampedSlerp(Convert::ToNative(a), Convert::ToNative(b), t)); + } + + Quaternion Quaternion::SlerpUnclamped(Quaternion a, Quaternion b, float t) + { + return Convert::ToCLI(SHQuaternion::Slerp(Convert::ToNative(a), Convert::ToNative(b), t)); + } + + + Quaternion Quaternion::operator*(Quaternion lhs, Quaternion rhs) + { + return Convert::ToCLI(Convert::ToNative(lhs) * Convert::ToNative(rhs)); + } + + bool Quaternion::operator==(Quaternion lhs, Quaternion rhs) + { + return lhs.Equals(rhs); + } +} \ No newline at end of file diff --git a/SHADE_Managed/src/Math/Quaternion.hxx b/SHADE_Managed/src/Math/Quaternion.hxx new file mode 100644 index 00000000..783038c9 --- /dev/null +++ b/SHADE_Managed/src/Math/Quaternion.hxx @@ -0,0 +1,237 @@ +/************************************************************************************//*! +\file Quaternion.hxx +\author Diren D Bharwani, diren.dbharwani, 390002520 +\par email: diren.dbharwani\@digipen.edu +\date Oct 23, 2022 +\brief Contains the definitions of Quaternion struct. + + Note: This file is written in C++17/CLI. + +Copyright (C) 2021 DigiPen Institute of Technology. +Reproduction or disclosure of this file or its contents without the prior written consent +of DigiPen Institute of Technology is prohibited. +*//*************************************************************************************/ + +#pragma once + +// Standard Libraries +#include +// Project Includes +#include "Vector3.hxx" + +namespace SHADE +{ + /// + /// CLR version of SHADE's Quaternion class that represents an orientation. + /// Designed to closely match Unity's Quaternion struct. + /// + [System::Runtime::InteropServices::StructLayout(System::Runtime::InteropServices::LayoutKind::Sequential)] + public value struct Quaternion : public System::IEquatable + { + public: + /*-----------------------------------------------------------------------------*/ + /* Constants */ + /*-----------------------------------------------------------------------------*/ +#pragma region Constants + + /// + /// Shorthand for writing Quaternion(0, 0, 0, 1). + /// + static initonly Quaternion Identity = Quaternion(0.0f, 0.0f, 0.0f, 1.0f); + +#pragma endregion + + /*-----------------------------------------------------------------------------*/ + /* Public Members */ + /*-----------------------------------------------------------------------------*/ + + /// + /// X-component of the Quaternion. + /// Don't modify this directly unless you know quaternions inside out. + /// + float x; + /// + /// Y-component of the Quaternion. + /// Don't modify this directly unless you know quaternions inside out. + /// + float y; + /// + /// Z-component of the Quaternion. + /// Don't modify this directly unless you know quaternions inside out. + /// + float z; + /// + /// W-component of the Quaternion. Do not directly modify quaternions. + /// + float w; + + /*-----------------------------------------------------------------------------*/ + /* Constructors */ + /*-----------------------------------------------------------------------------*/ + + /// + /// Constructor to construct a Quaternion with the specified components. + /// + /// X-coordinate to set. + /// Y-coordinate to set. + /// Z-coordinate to set. + /// W-coordinate to set. + Quaternion(float _x, float _y, float _z, float _w); + + /*-----------------------------------------------------------------------------*/ + /* Usage Functions */ + /*-----------------------------------------------------------------------------*/ + + /// + /// Creates a rotation which rotates from fromDirection to toDirection.
+ /// Use this to create a rotation which starts at the first Vector (fromDirection) and rotates to the second Vector (toDirection). + /// These Vectors must be set up in a script. + ///
+ void SetFromToRotation(Vector3 fromDirection, Vector3 toDirection); + + /// + /// Creates a rotation with the specified forward and upwards directions.
+ /// The result is applied to this quaternion. + /// If used to orient a Transform, the Z axis will be aligned with forward and the Y axis with upwards, assuming these vectors are orthogonal. + /// Logs an error if the forward direction is zero. + ///
+ /// The direction to look in. + /// The vector that defines in which direction up is. + void SetLookRotation(Vector3 view, Vector3 up); + + /// + /// Converts a rotation to angle-axis representation (angles in degrees). + /// + void ToAngleAxis(float^% angle, Vector3^% axis); + + System::String^ ToString() override; + + /*-----------------------------------------------------------------------------*/ + /* IEquatable */ + /*-----------------------------------------------------------------------------*/ + + /// + /// Compares equality with an object of the same type. + /// + /// The object to compare with. + /// True if both objects are the same. + virtual bool Equals(Quaternion other); + + /*-----------------------------------------------------------------------------*/ + /* Object */ + /*-----------------------------------------------------------------------------*/ + + /// + /// Compares equality with another unboxed object. + /// + /// The unboxed object to compare with. + /// True if both objects are the same. + bool Equals(Object^ o) override; + + /// + /// Gets a unique hash for this object. + /// + /// Unique hash for this object. + int GetHashCode() override; + + /*-----------------------------------------------------------------------------*/ + /* Static Functions */ + /*-----------------------------------------------------------------------------*/ + + /// + /// Returns the angle in degrees between two rotations a and b.
+ ///
+ /// The angle in degrees between the two vectors. + static float Angle(Quaternion a, Quaternion b); + + /// + /// Creates a rotation which rotates angle degrees around axis. + /// + static Quaternion AngleAxis(float angle, Vector3 axis); + + /// + /// The dot product between two rotations. + /// + static float Dot(Quaternion a, Quaternion b); + + /// + /// Returns a rotation that rotates y degrees around the y axis, x degrees around the x axis, and z degrees around the z axis; applied in that order. + /// + static Quaternion Euler(float _x, float _y, float _z); + + /// + /// Creates a rotation which rotates from fromDirection to toDirection. + /// + static Quaternion FromToRotation(Vector3 fromDirection, Vector3 toDirection); + + /// + /// Returns the Inverse of rotation. + /// + static Quaternion Inverse(Quaternion rotation); + + /// + /// Interpolates between a and b by t and normalizes the result afterwards. The parameter t is clamped to the range [0, 1]. + /// + /// Start value, returned when t = 0. + /// End value, returned when t = 1. + /// Interpolation ratio. + /// A quaternion interpolated between quaternions a and b. + static Quaternion Lerp(Quaternion a, Quaternion b, float t); + + /// + /// Interpolates between a and b by t and normalizes the result afterwards. The parameter t is not clamped. + /// + static Quaternion LerpUnclamped(Quaternion a, Quaternion b, float t); + + /// + /// Creates a rotation with the specified forward and upwards directions.
+ /// Z axis will be aligned with forward, X axis aligned with cross product between forward and upwards, and Y axis aligned with cross product between Z and X. + ///
+ static Quaternion LookRotation(Vector3 forward, Vector3 upwards); + + /// + /// Converts this quaternion to one with the same orientation but with a magnitude of 1. + /// + static Quaternion Normalize(Quaternion q); + + /// + /// Rotates a rotation from towards to.
+ /// The from quaternion is rotated towards to by an angular step of maxDegreesDelta (but note that the rotation will not overshoot). + /// Negative values of maxDegreesDelta will move away from to until the rotation is exactly the opposite direction. + ///
+ static Quaternion RotateTowards(Quaternion from, Quaternion to, float maxDegreesDelta); + + /// + /// Spherically interpolates between quaternions a and b by ratio t. The parameter t is clamped to the range [0, 1]. + /// + /// Start value, returned when t = 0. + /// End value, returned when t = 1. + /// Interpolation ratio. + /// A quaternion spherically interpolated between quaternions a and b. + static Quaternion Slerp(Quaternion a, Quaternion b, float t); + + /// + /// Spherically interpolates between a and b by t. The parameter t is not clamped. + /// + static Quaternion SlerpUnclamped(Quaternion a, Quaternion b, float t); + + /*-----------------------------------------------------------------------------*/ + /* Overloaded Operators */ + /*-----------------------------------------------------------------------------*/ + + /// + /// Combines rotations lhs and rhs. + /// + /// Left-hand side quaternion. + /// Right-hand side quaternion. + static Quaternion operator*(Quaternion lhs, Quaternion rhs); + + /// + /// Are two quaternions equal to each other? + /// + /// Left-hand side quaternion. + /// Right-hand side quaternion. + static bool operator==(Quaternion lhs, Quaternion rhs); + }; + +} // namespace SHADE diff --git a/SHADE_Managed/src/Math/Vector2.cxx b/SHADE_Managed/src/Math/Vector2.cxx index d40e2323..b110d4f8 100644 --- a/SHADE_Managed/src/Math/Vector2.cxx +++ b/SHADE_Managed/src/Math/Vector2.cxx @@ -26,10 +26,10 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ /* Constructors */ /*---------------------------------------------------------------------------------*/ - Vector2::Vector2(double _x) - : Vector2 { _x, 0.0 } + Vector2::Vector2(float _x) + : Vector2 { _x, 0.0f } {} - Vector2::Vector2(double _x, double _y) + Vector2::Vector2(float _x, float _y) : x { _x } , y { _y } {} @@ -47,22 +47,22 @@ namespace SHADE return *this / GetMagnitude(); } - double Vector2::GetMagnitude() + float Vector2::GetMagnitude() { return sqrt(x * x + y * y); } - double Vector2::GetSqrMagnitude() + float Vector2::GetSqrMagnitude() { return x * x + y * y; } - double Vector2::AngleFromRightRadians() + float Vector2::AngleFromRightRadians() { return atan2(y, x); } - double Vector2::AngleFromRightDegrees() + float Vector2::AngleFromRightDegrees() { return Math::RadiansToDegrees(AngleFromRightRadians()); } @@ -72,7 +72,7 @@ namespace SHADE return IsNearPoint(point, Math::Epsilon); } - bool Vector2::IsNearPoint(Vector2 point, double tolerance) + bool Vector2::IsNearPoint(Vector2 point, float tolerance) { return (*this - point).GetSqrMagnitude() < (tolerance * tolerance); } @@ -113,13 +113,13 @@ namespace SHADE { return IsNear(lhs, rhs, Math::Epsilon); } - bool Vector2::IsNear(Vector2 lhs, Vector2 rhs, double tolerance) + bool Vector2::IsNear(Vector2 lhs, Vector2 rhs, float tolerance) { return (std::abs(lhs.x) - std::abs(rhs.x)) < tolerance && (std::abs(lhs.y) - std::abs(rhs.y)) < tolerance; } - double Vector2::Dot(Vector2 lhs, Vector2 rhs) + float Vector2::Dot(Vector2 lhs, Vector2 rhs) { return lhs.x * rhs.x + lhs.y * rhs.y; } @@ -153,12 +153,12 @@ namespace SHADE } Vector2 Vector2::Reflect(Vector2 vec, Vector2 normal) { - return vec - (Project(vec, normal.GetNormalised()) * 2.0); + return vec - (Project(vec, normal.GetNormalised()) * 2.0f); } - Vector2 Vector2::RotateRadians(Vector2 vec, double radians) + Vector2 Vector2::RotateRadians(Vector2 vec, float radians) { - const double SINE = sin(radians); - const double COSINE = cos(radians); + const float SINE = sin(radians); + const float COSINE = cos(radians); return Vector2 ( @@ -166,35 +166,35 @@ namespace SHADE vec.x * SINE + vec.y * COSINE ); } - Vector2 Vector2::RotateDegrees(Vector2 vec, double degrees) + Vector2 Vector2::RotateDegrees(Vector2 vec, float degrees) { return RotateRadians(vec, Math::DegreesToRadians(degrees)); } Vector2 Vector2::Min(Vector2 lhs, Vector2 rhs) { - double lx = lhs.x, rx = rhs.x; - double ly = lhs.y, ry = rhs.y; + float lx = lhs.x, rx = rhs.x; + float ly = lhs.y, ry = rhs.y; return Vector2(std::min(lx, rx), std::min(ly, ry)); } Vector2 Vector2::Max(Vector2 lhs, Vector2 rhs) { - double lx = lhs.x, rx = rhs.x; - double ly = lhs.y, ry = rhs.y; + float lx = lhs.x, rx = rhs.x; + float ly = lhs.y, ry = rhs.y; return Vector2(std::max(lx, rx), std::max(ly, ry)); } - Vector2 Vector2::Lerp(Vector2 a, Vector2 b, double t) + Vector2 Vector2::Lerp(Vector2 a, Vector2 b, float t) { - return LerpUnclamped(a, b, std::clamp(t, 0.0, 1.0)); + return LerpUnclamped(a, b, std::clamp(t, 0.0f, 1.0f)); } - Vector2 Vector2::LerpUnclamped(Vector2 a, Vector2 b, double t) + Vector2 Vector2::LerpUnclamped(Vector2 a, Vector2 b, float t) { return a + ((b - a) * t); } - Vector2 Vector2::MoveTowards(Vector2 current, Vector2 target, double maxDistanceDelta) + Vector2 Vector2::MoveTowards(Vector2 current, Vector2 target, float maxDistanceDelta) { // Ignore if it is exactly on the same point if (current == target) @@ -206,7 +206,7 @@ namespace SHADE // Check if check if is behind or ahead of target Vector2 DIFF = target - newPos; - if (Dot(DELTA, DIFF) < 0.0) + if (Dot(DELTA, DIFF) < 0.0f) { newPos = target; } @@ -236,7 +236,7 @@ namespace SHADE lhs.y * rhs.y ); } - Vector2 Vector2::operator*(Vector2 lhs, double rhs) + Vector2 Vector2::operator*(Vector2 lhs, float rhs) { return Vector2 ( @@ -244,7 +244,7 @@ namespace SHADE lhs.y * rhs ); } - Vector2 Vector2::operator/(Vector2 lhs, double rhs) + Vector2 Vector2::operator/(Vector2 lhs, float rhs) { return Vector2 ( diff --git a/SHADE_Managed/src/Math/Vector2.hxx b/SHADE_Managed/src/Math/Vector2.hxx index 9253a703..94b1989f 100644 --- a/SHADE_Managed/src/Math/Vector2.hxx +++ b/SHADE_Managed/src/Math/Vector2.hxx @@ -19,8 +19,8 @@ of DigiPen Institute of Technology is prohibited. namespace SHADE { /// - /// CLR version of the the SHADE Engine's Vector2 class that represents a - /// 2-Dimensional Vector. Designed to closely match Unity's Vector2 struct. + /// CLR version of SHADE Engine's Vector2 class that represents a 2-Dimensional Vector. + /// Designed to closely match Unity's Vector2 struct. /// [System::Runtime::InteropServices::StructLayout(System::Runtime::InteropServices::LayoutKind::Sequential)] public value struct Vector2 : public System::IEquatable @@ -33,37 +33,37 @@ namespace SHADE /// /// Shorthand for writing Vector2(0, -1). /// - static initonly Vector2 Down = Vector2(0.0, -1.0); + static initonly Vector2 Down = Vector2(0.0f, -1.0f); /// /// Shorthand for writing Vector2(-1, 0). /// - static initonly Vector2 Left = Vector2(-1.0, 0.0); + static initonly Vector2 Left = Vector2(-1.0f, 0.0f); /// - /// Shorthand for writing Vector2(double.NegativeInfinity, - /// double.NegativeInfinity). + /// Shorthand for writing Vector2(float.NegativeInfinity, + /// float.NegativeInfinity). /// - static initonly Vector2 NegativeInfinity = Vector2(std::numeric_limits::lowest(), std::numeric_limits::lowest()); + static initonly Vector2 NegativeInfinity = Vector2(std::numeric_limits::lowest(), std::numeric_limits::lowest()); /// /// Shorthand for writing Vector2(1, 1). /// - static initonly Vector2 One = Vector2(1.0, 1.0); + static initonly Vector2 One = Vector2(1.0f, 1.0f); /// - /// Shorthand for writing Vector2(double.PositiveInfinity, - /// double.PositiveInfinity). + /// Shorthand for writing Vector2(float.PositiveInfinity, + /// float.PositiveInfinity). /// - static initonly Vector2 PositiveInfinity = Vector2(std::numeric_limits::max(), std::numeric_limits::max()); + static initonly Vector2 PositiveInfinity = Vector2(std::numeric_limits::max(), std::numeric_limits::max()); /// /// Shorthand for writing Vector2(1, 0). /// - static initonly Vector2 Right = Vector2(1.0, 0.0); + static initonly Vector2 Right = Vector2(1.0f, 0.0f); /// /// Shorthand for writing Vector2(0, 1). /// - static initonly Vector2 Up = Vector2(0.0, 1.0); + static initonly Vector2 Up = Vector2(0.0f, 1.0f); /// /// Shorthand for writing Vector2(0, 0). /// - static initonly Vector2 Zero = Vector2(0.0, 0.0); + static initonly Vector2 Zero = Vector2(0.0f, 0.0f); #pragma endregion /*-----------------------------------------------------------------------------*/ @@ -72,27 +72,27 @@ namespace SHADE /// /// X-component of the Vector2. /// - double x; + float x; /// /// Y-component of the Vector2. /// - double y; + float y; /*-----------------------------------------------------------------------------*/ /* Constructors */ /*-----------------------------------------------------------------------------*/ /// /// Constructor to construct a Vector2 with the specified components with the - /// Y-component set to 0.0. + /// Y-component set to 0.0f. /// /// X-coordinate to set. - Vector2(double _x); + Vector2(float _x); /// /// Constructor to construct a Vector2 with the specified components.. /// /// X-coordinate to set. /// Y-coordinate to set. - Vector2(double _x, double _y); + Vector2(float _x, float _y); /*-----------------------------------------------------------------------------*/ /* Usage Functions */ @@ -117,24 +117,24 @@ namespace SHADE /// need the precise magnitude, consider using GetSqrMagnitude() instead. /// /// Returns the length of this Vector2. - double GetMagnitude(); + float GetMagnitude(); /// /// Calculates and returns the squared magnitude of this Vector2. /// /// Returns the squared length of this Vector2. - double GetSqrMagnitude(); + float GetSqrMagnitude(); /// /// Calculates and returns the angle of this vector from the right vector. This /// function returns values between -Math.PI and Math.PI. /// /// Returns the angle of this vector from the right vector in radians. - double AngleFromRightRadians(); + float AngleFromRightRadians(); /// /// Calculates and returns the angle of this vector from the right vector. This - /// function returns values between -180.0 and 180.0. + /// function returns values between -180.0f and 180.0f. /// /// Returns the angle of this vector from the right vector in degrees. - double AngleFromRightDegrees(); + float AngleFromRightDegrees(); /// /// Checks if a specified point is near this Vector2 that represents a point with /// a tolerance value of PLS_EPSILON. @@ -156,7 +156,7 @@ namespace SHADE /// True if this Vector2 representing a point and the specified point are within /// the range of the specified tolerance. False otherwise. /// - bool IsNearPoint(Vector2 point, double tolerance); + bool IsNearPoint(Vector2 point, float tolerance); /*-----------------------------------------------------------------------------*/ /* IEquatable */ @@ -206,7 +206,7 @@ namespace SHADE /// /// True if the two Vector2s are within the tolerance value specified /// - static bool IsNear(Vector2 lhs, Vector2 rhs, double tolerance); + static bool IsNear(Vector2 lhs, Vector2 rhs, float tolerance); /// /// Computes and returns the dot product of 2 specified Vector2s. /// @@ -215,7 +215,7 @@ namespace SHADE /// /// Scalar value representing the dot product of the two Vector2s. /// - static double Dot(Vector2 lhs, Vector2 rhs); + static float Dot(Vector2 lhs, Vector2 rhs); /// /// Computes the inward perpendicular Vector2 to the specified Vector2. /// Equivalent to calling Perpendicular(lhs, true). This means, the @@ -260,7 +260,7 @@ namespace SHADE /// Angle to rotate the vector by in an anti-clockwise direction in radians. /// /// The Vector2 that represents the rotated vector. - static Vector2 RotateRadians(Vector2 vec, double radians); + static Vector2 RotateRadians(Vector2 vec, float radians); /// /// Rotates a Vector2 on the Z-axis by a specified angle in an anti-clockwise /// direction. @@ -270,7 +270,7 @@ namespace SHADE /// Angle to rotate the vector by in an anti-clockwise direction in degrees. /// /// The Vector2 that represents the rotated vector. - static Vector2 RotateDegrees(Vector2 vec, double degrees); + static Vector2 RotateDegrees(Vector2 vec, float degrees); /// /// Computes and returns a Vector2 that is made from the smallest components of /// the two specified Vector2s. @@ -298,25 +298,25 @@ namespace SHADE /// This is most commonly used to find a point some fraction of the way along a /// line between two endpoints. /// - /// The start Vector2, returned when t = 0.0. - /// The end Vector2, returned when t = 1.0. + /// The start Vector2, returned when t = 0.0f. + /// The end Vector2, returned when t = 1.0f. /// /// Value used to interpolate between a and b which is clamped to /// the range[0, 1]. /// /// The interpolated Vector2. - static Vector2 Lerp(Vector2 a, Vector2 b, double t); + static Vector2 Lerp(Vector2 a, Vector2 b, float t); /// /// Linearly interpolates between two specified points. /// This is most commonly used to find a point some fraction of the way along a /// line between two endpoints. /// Unlike Lerp(), t is not clamped to a range at all. /// - /// The start Vector2, returned when t = 0.0. - /// The end Vector2, returned when t = 1.0. + /// The start Vector2, returned when t = 0.0f. + /// The end Vector2, returned when t = 1.0f. /// Value used to interpolate between a and b. /// The interpolated Vector2. - static Vector2 LerpUnclamped(Vector2 a, Vector2 b, double t); + static Vector2 LerpUnclamped(Vector2 a, Vector2 b, float t); /// /// Moves a point current towards target. /// Similar to Lerp(), however, the function will ensure that the distance never @@ -327,7 +327,7 @@ namespace SHADE /// The target position to move to. /// Maximum distance moved per call. /// Vector representing the moved point. - static Vector2 MoveTowards(Vector2 current, Vector2 target, double maxDistanceDelta); + static Vector2 MoveTowards(Vector2 current, Vector2 target, float maxDistanceDelta); /*-----------------------------------------------------------------------------*/ /* Overloaded Operators */ @@ -361,7 +361,7 @@ namespace SHADE /// Vector2 to multiply with. /// Scalar to multiply with. /// The result of the scalar multiplication. - static Vector2 operator*(Vector2 lhs, double rhs); + static Vector2 operator*(Vector2 lhs, float rhs); /// /// Calculates the division of a Vector2 with a scalar value and returns /// the result. @@ -369,7 +369,7 @@ namespace SHADE /// Scalar to divide with. /// Vector2 to divide with. /// The result of the scalar division. - static Vector2 operator/(Vector2 lhs, double rhs); + static Vector2 operator/(Vector2 lhs, float rhs); /// /// Checks if two Vector2s are approximately equal. This is equivalent to /// calling Vector2.IsNear() with default tolerance values. diff --git a/SHADE_Managed/src/Math/Vector3.cxx b/SHADE_Managed/src/Math/Vector3.cxx index 26ff5a72..adbb4d3a 100644 --- a/SHADE_Managed/src/Math/Vector3.cxx +++ b/SHADE_Managed/src/Math/Vector3.cxx @@ -11,6 +11,7 @@ Copyright (C) 2021 DigiPen Institute of Technology. Reproduction or disclosure of this file or its contents without the prior written consent of DigiPen Institute of Technology is prohibited. *//*************************************************************************************/ + // Precompiled Headers #include "SHpch.h" // Primary Header @@ -26,13 +27,13 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ /* Constructors */ /*---------------------------------------------------------------------------------*/ - Vector3::Vector3(double _x) - : Vector3 {_x, 0.0, 0.0} + Vector3::Vector3(float _x) + : Vector3 {_x, 0.0f, 0.0f} {} - Vector3::Vector3(double _x, double _y) - : Vector3 {_x, _y, 0.0} + Vector3::Vector3(float _x, float _y) + : Vector3 {_x, _y, 0.0f} {} - Vector3::Vector3(double _x, double _y, double _z) + Vector3::Vector3(float _x, float _y, float _z) : x { _x } , y { _y } , z { _z } @@ -54,22 +55,22 @@ namespace SHADE return *this / GetSqrMagnitude(); } - double Vector3::GetMagnitude() + float Vector3::GetMagnitude() { return sqrt(x * x + y * y + z * z); } - double Vector3::GetSqrMagnitude() + float Vector3::GetSqrMagnitude() { return x * x + y * y + z * z; } - double Vector3::Angle2DFromRightRadians() + float Vector3::Angle2DFromRightRadians() { return atan2(y, x); } - double Vector3::Angle2DFromRightDegrees() + float Vector3::Angle2DFromRightDegrees() { return Math::RadiansToDegrees(Angle2DFromRightRadians()); } @@ -79,7 +80,7 @@ namespace SHADE return IsNearPoint(point, Math::Epsilon); } - bool Vector3::IsNearPoint(Vector3 point, double tolerance) + bool Vector3::IsNearPoint(Vector3 point, float tolerance) { return (*this - point).GetSqrMagnitude() < (tolerance * tolerance); } @@ -121,7 +122,7 @@ namespace SHADE { return IsNear(lhs, rhs, Math::Epsilon); } - bool Vector3::IsNear(Vector3 lhs, Vector3 rhs, double tolerance) + bool Vector3::IsNear(Vector3 lhs, Vector3 rhs, float tolerance) { return (std::abs(lhs.x) - std::abs(rhs.x)) < tolerance && @@ -129,7 +130,7 @@ namespace SHADE && (std::abs(lhs.z) - std::abs(rhs.z)) < tolerance; } - double Vector3::Dot(Vector3 lhs, Vector3 rhs) + float Vector3::Dot(Vector3 lhs, Vector3 rhs) { return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z; } @@ -145,12 +146,12 @@ namespace SHADE } Vector3 Vector3::Reflect(Vector3 vec, Vector3 normal) { - return vec - (Project(vec, normal.GetNormalised()) * 2.0); + return vec - (Project(vec, normal.GetNormalised()) * 2.0f); } - Vector3 Vector3::RotateRadians(Vector3 vec, double radians) + Vector3 Vector3::RotateRadians(Vector3 vec, float radians) { - const double SINE = sin(radians); - const double COSINE = cos(radians); + const float SINE = sin(radians); + const float COSINE = cos(radians); return Vector3 ( @@ -159,15 +160,15 @@ namespace SHADE vec.z ); } - Vector3 Vector3::RotateDegrees(Vector3 vec, double degrees) + Vector3 Vector3::RotateDegrees(Vector3 vec, float degrees) { return RotateRadians(vec, Math::DegreesToRadians(degrees)); } Vector3 Vector3::Min(Vector3 lhs, Vector3 rhs) { - double lx = lhs.x, rx = rhs.x; - double ly = lhs.y, ry = rhs.y; - double lz = lhs.z, rz = rhs.z; + float lx = lhs.x, rx = rhs.x; + float ly = lhs.y, ry = rhs.y; + float lz = lhs.z, rz = rhs.z; return Vector3(std::min(lx, rx), std::min(ly, ry), @@ -175,23 +176,23 @@ namespace SHADE } Vector3 Vector3::Max(Vector3 lhs, Vector3 rhs) { - double lx = lhs.x, rx = rhs.x; - double ly = lhs.y, ry = rhs.y; - double lz = lhs.z, rz = rhs.z; + float lx = lhs.x, rx = rhs.x; + float ly = lhs.y, ry = rhs.y; + float lz = lhs.z, rz = rhs.z; return Vector3(std::max(lx, rx), std::max(ly, ry), std::max(lz, rz)); } - Vector3 Vector3::Lerp(Vector3 a, Vector3 b, double t) + Vector3 Vector3::Lerp(Vector3 a, Vector3 b, float t) { - return LerpUnclamped(a, b, std::clamp(t, 0.0, 1.0)); + return LerpUnclamped(a, b, std::clamp(t, 0.0f, 1.0f)); } - Vector3 Vector3::LerpUnclamped(Vector3 a, Vector3 b, double t) + Vector3 Vector3::LerpUnclamped(Vector3 a, Vector3 b, float t) { return a + ((b - a) * t); } - Vector3 Vector3::MoveTowards(Vector3 current, Vector3 target, double maxDistanceDelta) + Vector3 Vector3::MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta) { // Ignore if it is exactly on the same point if (current == target) @@ -203,7 +204,7 @@ namespace SHADE // Check if check if is behind or ahead of target Vector3 DIFF = target - newPos; - if (Dot(DELTA, DIFF) < 0.0) + if (Dot(DELTA, DIFF) < 0.0f) { newPos = target; } @@ -236,7 +237,7 @@ namespace SHADE lhs.z * rhs.z ); } - Vector3 Vector3::operator*(Vector3 lhs, double rhs) + Vector3 Vector3::operator*(Vector3 lhs, float rhs) { return Vector3 ( @@ -245,7 +246,7 @@ namespace SHADE lhs.z * rhs ); } - Vector3 Vector3::operator/(Vector3 lhs, double rhs) + Vector3 Vector3::operator/(Vector3 lhs, float rhs) { return Vector3 ( diff --git a/SHADE_Managed/src/Math/Vector3.hxx b/SHADE_Managed/src/Math/Vector3.hxx index 8b66439c..70cff88f 100644 --- a/SHADE_Managed/src/Math/Vector3.hxx +++ b/SHADE_Managed/src/Math/Vector3.hxx @@ -22,8 +22,8 @@ of DigiPen Institute of Technology is prohibited. namespace SHADE { /// - /// CLR version of the the PlushieEngine's Vector3 class that represents a - /// 3-Dimensional Vector. Designed to closely match Unity's Vector3 struct. + /// CLR version of SHADE Engine's Vector3 class that represents a 3-Dimensional Vector. + /// Designed to closely match Unity's Vector3 struct. /// [System::Runtime::InteropServices::StructLayout(System::Runtime::InteropServices::LayoutKind::Sequential)] public value struct Vector3 : public System::IEquatable @@ -36,49 +36,49 @@ namespace SHADE /// /// Shorthand for writing Vector3(0, 0, -1). /// - static initonly Vector3 Back = Vector3(0.0, 0.0, -1.0); + static initonly Vector3 Back = Vector3(0.0f, 0.0f, -1.0f); /// /// Shorthand for writing Vector3(0, -1, 0). /// - static initonly Vector3 Down = Vector3(0.0, -1.0, 0.0); + static initonly Vector3 Down = Vector3(0.0f, -1.0f, 0.0f); /// /// Shorthand for writing Vector3(0, 0, 1). /// - static initonly Vector3 Forward = Vector3(0.0, 0.0, 1.0); + static initonly Vector3 Forward = Vector3(0.0f, 0.0f, 1.0f); /// /// Shorthand for writing Vector3(-1, 0, 0). /// - static initonly Vector3 Left = Vector3(-1.0, 0.0, 0.0); + static initonly Vector3 Left = Vector3(-1.0f, 0.0f, 0.0f); /// - /// Shorthand for writing Vector3(double.NegativeInfinity, - /// double.NegativeInfinity, double.NegativeInfinity). + /// Shorthand for writing Vector3(float.NegativeInfinity, + /// float.NegativeInfinity, float.NegativeInfinity). /// - static initonly Vector3 NegativeInfinity = Vector3(std::numeric_limits::lowest(), - std::numeric_limits::lowest(), - std::numeric_limits::lowest()); + static initonly Vector3 NegativeInfinity = Vector3(std::numeric_limits::lowest(), + std::numeric_limits::lowest(), + std::numeric_limits::lowest()); /// /// Shorthand for writing Vector3(1, 1, 1). /// - static initonly Vector3 One = Vector3(1.0, 1.0, 1.0); + static initonly Vector3 One = Vector3(1.0f, 1.0f, 1.0f); /// - /// Shorthand for writing Vector3(double.PositiveInfinity, - /// double.PositiveInfinity, double.PositiveInfinity). + /// Shorthand for writing Vector3(float.PositiveInfinity, + /// float.PositiveInfinity, float.PositiveInfinity). /// - static initonly Vector3 PositiveInfinity = Vector3(std::numeric_limits::max(), - std::numeric_limits::max(), - std::numeric_limits::max()); + static initonly Vector3 PositiveInfinity = Vector3(std::numeric_limits::max(), + std::numeric_limits::max(), + std::numeric_limits::max()); /// /// Shorthand for writing Vector3(1, 0, 0). /// - static initonly Vector3 Right = Vector3(1.0, 0.0, 0.0); + static initonly Vector3 Right = Vector3(1.0f, 0.0f, 0.0f); /// /// Shorthand for writing Vector3(0, 1, 0). /// - static initonly Vector3 Up = Vector3(0.0, 1.0, 0.0); + static initonly Vector3 Up = Vector3(0.0f, 1.0f, 0.0f); /// /// Shorthand for writing Vector3(0, 0, 0). /// - static initonly Vector3 Zero = Vector3(0.0, 0.0, 0.0); + static initonly Vector3 Zero = Vector3(0.0f, 0.0f, 0.0f); #pragma endregion /*-----------------------------------------------------------------------------*/ @@ -87,39 +87,39 @@ namespace SHADE /// /// X-component of the Vector3. /// - double x; + float x; /// /// Y-component of the Vector3. /// - double y; + float y; /// /// Z-component of the Vector3. /// - double z; + float z; /*-----------------------------------------------------------------------------*/ /* Constructors */ /*-----------------------------------------------------------------------------*/ /// /// Constructor to construct a Vector3 with the specified components with the - /// Y and Z-component set to 0.0. + /// Y and Z-component set to 0.0f. /// /// X-coordinate to set. - Vector3(double _x); + Vector3(float _x); /// /// Constructor to construct a Vector3 with the specified components with the - /// Z-component set to 0.0. + /// Z-component set to 0.0f. /// /// X-coordinate to set. /// Y-coordinate to set. - Vector3(double _x, double _y); + Vector3(float _x, float _y); /// /// Constructor to construct a Vector3 with the specified components. /// /// X-coordinate to set. /// Y-coordinate to set. /// Z-coordinate to set. - Vector3(double _x, double _y, double _z); + Vector3(float _x, float _y, float _z); /// /// Conversion constructor to construct a Vector3 using a Vector2. /// @@ -149,24 +149,24 @@ namespace SHADE /// need the precise magnitude, consider using GetSqrMagnitude() instead. /// /// Returns the length of this Vector3. - double GetMagnitude(); + float GetMagnitude(); /// /// Calculates and returns the squared magnitude of this Vector3. /// /// Returns the squared length of this Vector3. - double GetSqrMagnitude(); + float GetSqrMagnitude(); /// /// Calculates and returns the angle of this vector from the right vector. This /// function returns values between -Math.PI and Math.PI. /// /// Returns the angle of this vector from the right vector in radians. - double Angle2DFromRightRadians(); + float Angle2DFromRightRadians(); /// /// Calculates and returns the angle of this vector from the right vector. This - /// function returns values between -180.0 and 180.0. + /// function returns values between -180.0f and 180.0f. /// /// Returns the angle of this vector from the right vector in degrees. - double Angle2DFromRightDegrees(); + float Angle2DFromRightDegrees(); /// /// Checks if a specified point is near this Vector3 that represents a point with /// a tolerance value of PLS_EPSILON. @@ -188,7 +188,7 @@ namespace SHADE /// True if this Vector3 representing a point and the specified point are within /// the range of the specified tolerance. False otherwise. /// - bool IsNearPoint(Vector3 point, double tolerance); + bool IsNearPoint(Vector3 point, float tolerance); /*-----------------------------------------------------------------------------*/ /* IEquatable */ @@ -208,12 +208,12 @@ namespace SHADE /// /// The unboxed object to compare with. /// True if both objects are the same. - bool Equals(Object^ o) override; + bool Equals(Object^ o) override; /// /// Gets a unique hash for this object. /// /// Unique hash for this object. - int GetHashCode() override; + int GetHashCode() override; /*-----------------------------------------------------------------------------*/ /* Static Functions */ @@ -236,14 +236,14 @@ namespace SHADE /// /// True if the two Vector3s are within the tolerance value specified /// - static bool IsNear(Vector3 lhs, Vector3 rhs, double tolerance); + static bool IsNear(Vector3 lhs, Vector3 rhs, float tolerance); /// /// Computes and returns the dot product of 2 specified Vector3s. /// /// Vector3 to calculate dot product with. /// Another Vector3 to calculate dot product with. /// Scalar value representing the dot product of the two Vector3s. - static double Dot(Vector3 lhs, Vector3 rhs); + static float Dot(Vector3 lhs, Vector3 rhs); /// /// Computes and returns the cross product of 2 specified Vector3s. /// @@ -274,7 +274,7 @@ namespace SHADE /// Angle to rotate the vector by in an anti-clockwise direction in radians. /// /// The Vector3 that represents the rotated vector. - static Vector3 RotateRadians(Vector3 vec, double radians); + static Vector3 RotateRadians(Vector3 vec, float radians); /// /// Rotates a Vector3 on the Z-axis by a specified angle in an anti-clockwise /// direction. @@ -284,7 +284,7 @@ namespace SHADE /// Angle to rotate the vector by in an anti-clockwise direction in degrees. /// /// The Vector3 that represents the rotated vector. - static Vector3 RotateDegrees(Vector3 vec, double degrees); + static Vector3 RotateDegrees(Vector3 vec, float degrees); /// /// Computes and returns a Vector3 that is made from the smallest components of /// the two specified Vector3s. @@ -312,25 +312,25 @@ namespace SHADE /// This is most commonly used to find a point some fraction of the way along a /// line between two endpoints. /// - /// The start Vector3, returned when t = 0.0. - /// The end Vector3, returned when t = 1.0. + /// The start Vector3, returned when t = 0.0f. + /// The end Vector3, returned when t = 1.0f. /// /// Value used to interpolate between a and b which is clamped to /// the range[0, 1]. /// /// The interpolated Vector3. - static Vector3 Lerp(Vector3 a, Vector3 b, double t); + static Vector3 Lerp(Vector3 a, Vector3 b, float t); /// /// Linearly interpolates between two specified points. /// This is most commonly used to find a point some fraction of the way along a /// line between two endpoints. /// Unlike Lerp(), t is not clamped to a range at all. /// - /// The start Vector3, returned when t = 0.0. - /// The end Vector3, returned when t = 1.0. + /// The start Vector3, returned when t = 0.0f. + /// The end Vector3, returned when t = 1.0f. /// Value used to interpolate between a and b. /// The interpolated Vector3. - static Vector3 LerpUnclamped(Vector3 a, Vector3 b, double t); + static Vector3 LerpUnclamped(Vector3 a, Vector3 b, float t); /// /// Moves a point current towards target. /// Similar to Lerp(), however, the function will ensure that the distance never @@ -341,7 +341,7 @@ namespace SHADE /// The target position to move to. /// Maximum distance moved per call. /// Vector representing the moved point. - static Vector3 MoveTowards(Vector3 current, Vector3 target, double maxDistanceDelta); + static Vector3 MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta); /*-----------------------------------------------------------------------------*/ /* Overloaded Operators */ @@ -375,7 +375,7 @@ namespace SHADE /// Vector3 to multiply with. /// Scalar to multiply with. /// The result of the scalar multiplication. - static Vector3 operator*(Vector3 lhs, double rhs); + static Vector3 operator*(Vector3 lhs, float rhs); /// /// Calculates the division of a Vector3 with a scalar value and returns /// the result. @@ -383,7 +383,7 @@ namespace SHADE /// Scalar to divide with. /// Vector3 to divide with. /// The result of the scalar division. - static Vector3 operator/(Vector3 lhs, double rhs); + static Vector3 operator/(Vector3 lhs, float rhs); /// /// Checks if two Vector3s are approximately equal. This is equivalent to /// calling Vector3.IsNear() with default tolerance values. diff --git a/SHADE_Managed/src/Utility/Convert.cxx b/SHADE_Managed/src/Utility/Convert.cxx index 661eb3e4..4646194b 100644 --- a/SHADE_Managed/src/Utility/Convert.cxx +++ b/SHADE_Managed/src/Utility/Convert.cxx @@ -35,10 +35,7 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ SHVec3 Convert::ToNative(Vector3 vec) { - const double X = vec.x; - const double Y = vec.y; - const double Z = vec.z; - return SHVec3(X, Y, Z); + return SHVec3(vec.x, vec.y, vec.z); } Vector3 Convert::ToCLI(const SHVec3& vec) { @@ -46,9 +43,7 @@ namespace SHADE } SHVec2 Convert::ToNative(Vector2 vec) { - const double X = vec.x; - const double Y = vec.y; - return SHVec2(X, Y); + return SHVec2(vec.x, vec.y); } Vector2 Convert::ToCLI(const SHVec2& vec) @@ -56,6 +51,16 @@ namespace SHADE return Vector2(vec.x, vec.y); } + SHQuaternion Convert::ToNative(Quaternion quat) + { + return SHQuaternion{ quat.x, quat.y, quat.z, quat.w }; + } + + Quaternion Convert::ToCLI(const SHQuaternion& quat) + { + return Quaternion{ quat.x, quat.y, quat.z, quat.w }; + } + /*---------------------------------------------------------------------------------*/ /* String Conversions */ /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Managed/src/Utility/Convert.hxx b/SHADE_Managed/src/Utility/Convert.hxx index f04cbf4b..d694ca6a 100644 --- a/SHADE_Managed/src/Utility/Convert.hxx +++ b/SHADE_Managed/src/Utility/Convert.hxx @@ -18,10 +18,12 @@ of DigiPen Institute of Technology is prohibited. #include "ECS_Base/Entity/SHEntity.h" #include "Math/Vector/SHVec2.h" #include "Math/Vector/SHVec3.h" +#include "Math/SHQuaternion.h" // Project Includes #include "Engine/Entity.hxx" #include "Math/Vector2.hxx" #include "Math/Vector3.hxx" +#include "Math/Quaternion.hxx" namespace SHADE { @@ -74,6 +76,18 @@ namespace SHADE /// The native Vector2 to convert from. /// Managed copy of a native Vector2. static Vector2 ToCLI(const SHVec2& vec); + /// + /// Converts from a managed Quaternion to a native Quaternion. + /// + /// The managed Quaternion to convert from. + /// Native copy of a managed Quaternion. + static SHQuaternion ToNative(Quaternion quat); + /// + /// Converts from a native Quaternion to a managed Quaternion. + /// + /// The native Quaternion to convert from. + /// Managed copy of a native Quaternion. + static Quaternion ToCLI(const SHQuaternion& quat); /*-----------------------------------------------------------------------------*/ /* String Conversions */ From 24c591ec27b186431ea145e7053d2d8542d23031 Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Mon, 24 Oct 2022 20:07:59 +0800 Subject: [PATCH 2/4] Modified Managed Transform Interface to use Quaternions --- SHADE_Managed/src/Components/Transform.cxx | 16 ++++++++-------- SHADE_Managed/src/Components/Transform.hxx | 21 ++++++++++----------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/SHADE_Managed/src/Components/Transform.cxx b/SHADE_Managed/src/Components/Transform.cxx index 0f0716dc..41298c21 100644 --- a/SHADE_Managed/src/Components/Transform.cxx +++ b/SHADE_Managed/src/Components/Transform.cxx @@ -29,13 +29,13 @@ namespace SHADE { GetNativeComponent()->SetLocalPosition(Convert::ToNative(val)); } - Vector3 Transform::LocalRotation::get() + Quaternion Transform::LocalRotation::get() { - return Convert::ToCLI(GetNativeComponent()->GetLocalRotation()); + return Convert::ToCLI(GetNativeComponent()->GetLocalOrientation()); } - void Transform::LocalRotation::set(Vector3 val) + void Transform::LocalRotation::set(Quaternion val) { - GetNativeComponent()->SetLocalRotation(Convert::ToNative(val)); + GetNativeComponent()->SetLocalOrientation(Convert::ToNative(val)); } Vector3 Transform::LocalScale::get() { @@ -54,13 +54,13 @@ namespace SHADE { GetNativeComponent()->SetWorldPosition(Convert::ToNative(val)); } - Vector3 Transform::GlobalRotation::get() + Quaternion Transform::GlobalRotation::get() { - return Convert::ToCLI(GetNativeComponent()->GetWorldRotation()); + return Convert::ToCLI(GetNativeComponent()->GetLocalOrientation()); } - void Transform::GlobalRotation::set(Vector3 val) + void Transform::GlobalRotation::set(Quaternion val) { - GetNativeComponent()->SetWorldRotation(Convert::ToNative(val)); + GetNativeComponent()->SetWorldOrientation(Convert::ToNative(val)); } Vector3 Transform::GlobalScale::get() { diff --git a/SHADE_Managed/src/Components/Transform.hxx b/SHADE_Managed/src/Components/Transform.hxx index 9efd2927..43118b1f 100644 --- a/SHADE_Managed/src/Components/Transform.hxx +++ b/SHADE_Managed/src/Components/Transform.hxx @@ -17,14 +17,14 @@ of DigiPen Institute of Technology is prohibited. // Project Includes #include "Components/Component.hxx" #include "Math/Vector3.hxx" -#include "Utility/Convert.hxx" +#include "Math/Quaternion.hxx" // External Dependencies #include "Math/Transform/SHTransformComponent.h" namespace SHADE { /// - /// CLR version of the the SHADE Engine's TransformComponent. + /// CLR version of the SHADE Engine's TransformComponent. /// public ref class Transform : public Component { @@ -52,12 +52,12 @@ namespace SHADE void set(Vector3 val); } /// - /// Local Z-axis rotation angle stored by this Transform in Radians. + /// Local rotation quaternion stored by this Transform. /// - property Vector3 LocalRotation + property Quaternion LocalRotation { - Vector3 get(); - void set(Vector3 val); + Quaternion get(); + void set(Quaternion val); } /// /// Local scale stored by this Transform. @@ -76,16 +76,15 @@ namespace SHADE void set(Vector3 val); } /// - /// Global Z-axis rotation angle stored by this Transform in Radians. + /// Global rotation quaternion stored by this Transform. /// - property Vector3 GlobalRotation + property Quaternion GlobalRotation { - Vector3 get(); - void set(Vector3 val); + Quaternion get(); + void set(Quaternion val); } /// /// Global scale stored by this Transform. - /// Note that this operation is expensive. /// property Vector3 GlobalScale { From ff025f212cc93e3668dd435015c8bab632b7889f Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Mon, 24 Oct 2022 22:41:36 +0800 Subject: [PATCH 3/4] Added Euler Angles properties to Managed Transforms --- SHADE_Managed/src/Components/Transform.cxx | 16 ++++++++++++++++ SHADE_Managed/src/Components/Transform.hxx | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/SHADE_Managed/src/Components/Transform.cxx b/SHADE_Managed/src/Components/Transform.cxx index 41298c21..98f0da4f 100644 --- a/SHADE_Managed/src/Components/Transform.cxx +++ b/SHADE_Managed/src/Components/Transform.cxx @@ -37,6 +37,14 @@ namespace SHADE { GetNativeComponent()->SetLocalOrientation(Convert::ToNative(val)); } + Vector3 Transform::LocalEulerAngles::get() + { + return Convert::ToCLI(GetNativeComponent()->GetLocalRotation()); + } + void Transform::LocalEulerAngles::set(Vector3 val) + { + GetNativeComponent()->SetLocalRotation(Convert::ToNative(val)); + } Vector3 Transform::LocalScale::get() { return Convert::ToCLI(GetNativeComponent()->GetLocalScale()); @@ -62,6 +70,14 @@ namespace SHADE { GetNativeComponent()->SetWorldOrientation(Convert::ToNative(val)); } + Vector3 Transform::GlobalEulerAngles::get() + { + return Convert::ToCLI(GetNativeComponent()->GetWorldRotation()); + } + void Transform::GlobalEulerAngles::set(Vector3 val) + { + GetNativeComponent()->SetWorldRotation(Convert::ToNative(val)); + } Vector3 Transform::GlobalScale::get() { return Convert::ToCLI(GetNativeComponent()->GetWorldScale()); diff --git a/SHADE_Managed/src/Components/Transform.hxx b/SHADE_Managed/src/Components/Transform.hxx index 43118b1f..bbe9fd19 100644 --- a/SHADE_Managed/src/Components/Transform.hxx +++ b/SHADE_Managed/src/Components/Transform.hxx @@ -60,6 +60,14 @@ namespace SHADE void set(Quaternion val); } /// + /// Local euler angle rotations stored by this Transform. + /// + property Vector3 LocalEulerAngles + { + Vector3 get(); + void set(Vector3 val); + } + /// /// Local scale stored by this Transform. /// property Vector3 LocalScale @@ -84,6 +92,14 @@ namespace SHADE void set(Quaternion val); } /// + /// Global euler angle rotations stored by this Transform. + /// + property Vector3 GlobalEulerAngles + { + Vector3 get(); + void set(Vector3 val); + } + /// /// Global scale stored by this Transform. /// property Vector3 GlobalScale From c2e51dc603118eb025aa95b4fcf64f44cc5cfca9 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Tue, 25 Oct 2022 14:31:28 +0800 Subject: [PATCH 4/4] Fixed deletion crash caused by Renderables --- .../Graphics/MiddleEnd/Batching/SHBatch.cpp | 74 ++++++++++++------- .../src/Graphics/MiddleEnd/Batching/SHBatch.h | 2 +- .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 4 +- .../MiddleEnd/Interface/SHRenderable.cpp | 24 +++--- .../MiddleEnd/Interface/SHRenderable.h | 4 +- 5 files changed, 67 insertions(+), 41 deletions(-) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp index 40826047..24dd76c5 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp @@ -59,7 +59,7 @@ namespace SHADE } // Add renderable in - subBatch->Renderables.insert(renderable); + subBatch->Renderables.insert(renderable->GetEID()); // Also add material instance in referencedMatInstances.insert(renderable->GetMaterial()); @@ -80,27 +80,34 @@ namespace SHADE if (subBatch == subBatches.end()) return; - subBatch->Renderables.erase(renderable); + subBatch->Renderables.erase(renderable->GetEID()); // Check if other renderables in subBatches contain the same material instance bool matUnused = true; for (const auto& sb : subBatches) - for (const auto& rend : sb.Renderables) + for (const auto& rendId : sb.Renderables) { - if (rend->GetMaterial() == renderable->GetMaterial()) + auto rend = SHComponentManager::GetComponent(rendId); + if (rend) { - matUnused = false; - break; + if (rend->GetMaterial() == renderable->GetMaterial()) + { + matUnused = false; + break; + } + } + else + { + SHLOG_WARNING("[SHBatch] Entity with a missing SHRenderable found!"); } } // Material is no longer in this library, so we remove it if (matUnused) - referencedMatInstances.erase(renderable->GetMaterial()); + referencedMatInstances.erase(renderable->WasMaterialChanged() ? renderable->GetPrevMaterial() : renderable->GetMaterial()); // Mark all as dirty - for (bool& dirt : isDirty) - dirt = true; + setAllDirtyFlags(); } void SHBatch::Clear() @@ -162,9 +169,17 @@ namespace SHADE // Build CPU Buffer char* propsCurrPtr = matPropsData.get(); for (auto& subBatch : subBatches) - for (const SHRenderable* renderable : subBatch.Renderables) + for (auto rendId : subBatch.Renderables) { - renderable->GetMaterial()->ExportProperties(propsCurrPtr); + const SHRenderable* renderable = SHComponentManager::GetComponent(rendId); + if (renderable) + { + renderable->GetMaterial()->ExportProperties(propsCurrPtr); + } + else + { + SHLOG_WARNING("[SHBatch] Entity with a missing SHRenderable found!"); + } propsCurrPtr += singleMatPropAlignedSize; } @@ -188,18 +203,18 @@ namespace SHADE // Populate on the CPU for (auto& subBatch : subBatches) - for (const SHRenderable* renderable : subBatch.Renderables) + for (auto rendId : subBatch.Renderables) { // Transform - auto transform = SHComponentManager::GetComponent(renderable->GetEID()); - if (!transform) + auto transform = SHComponentManager::GetComponent(rendId); + if (transform) { - SHLOG_WARNING("[SHBatch] Entity contianing a SHRenderable with no SHTransformComponent found!"); - transformData.emplace_back(); + transformData.emplace_back(transform->GetTRS()); } else { - transformData.emplace_back(transform->GetTRS()); + SHLOG_WARNING("[SHBatch] Entity contianing a SHRenderable with no SHTransformComponent found!"); + transformData.emplace_back(); } } @@ -221,10 +236,10 @@ namespace SHADE // Populate on the CPU for (auto& subBatch : subBatches) - for (const SHRenderable* renderable : subBatch.Renderables) - { - eidData.emplace_back(renderable->GetEID()); - } + for (auto rendId : subBatch.Renderables) + { + eidData.emplace_back(rendId); + } // Transfer to GPU if (eidBuffer[frameIndex]) @@ -305,11 +320,10 @@ namespace SHADE }); // Fill in buffers (CPU) - for (const SHRenderable* renderable : subBatch.Renderables) + for (auto rendId : subBatch.Renderables) { // Transform - EntityID eid = renderable->GetEID(); - auto transform = SHComponentManager::GetComponent_s(eid); + auto transform = SHComponentManager::GetComponent_s(rendId); if (!transform) { SHLOG_WARNING("[SHBatch] Entity contianing a SHRenderable with no SHTransformComponent found!"); @@ -320,12 +334,20 @@ namespace SHADE transformData.emplace_back(transform->GetTRS()); } - eidData.emplace_back(eid); + eidData.emplace_back(rendId); // Material Properties if (!EMPTY_MAT_PROPS) { - renderable->GetMaterial()->ExportProperties(propsCurrPtr); + const SHRenderable* renderable = SHComponentManager::GetComponent(rendId); + if (renderable) + { + renderable->GetMaterial()->ExportProperties(propsCurrPtr); + } + else + { + SHLOG_WARNING("[SHBatch] Entity with a missing SHRenderable found!"); + } propsCurrPtr += singleMatPropAlignedSize; } } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.h b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.h index 193fff0d..fa18fad5 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.h @@ -55,7 +55,7 @@ namespace SHADE /* Data Members */ /*-----------------------------------------------------------------------------*/ Handle Mesh; - std::unordered_set Renderables; + std::unordered_set Renderables; }; /***********************************************************************************/ /*! diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index 7ecd92d2..4280cafd 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -677,10 +677,10 @@ namespace SHADE continue; // Remove from old material's SuperBatch - Handle prevMaterial = renderable.GetPrevMaterial(); + Handle prevMaterial = renderable.GetPrevMaterial(); if (prevMaterial) { - Handle oldSuperBatch = prevMaterial->GetPipeline()->GetPipelineState().GetSubpass()->GetSuperBatch(); + Handle oldSuperBatch = prevMaterial->GetBaseMaterial()->GetPipeline()->GetPipelineState().GetSubpass()->GetSuperBatch(); oldSuperBatch->Remove(&renderable); } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderable.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderable.cpp index 0b1c1b66..9dcd391d 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderable.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderable.cpp @@ -31,15 +31,16 @@ namespace SHADE void SHRenderable::OnDestroy() { + // Remove from SuperBatch + Handle superBatch = sharedMaterial->GetBaseMaterial()->GetPipeline()->GetPipelineState().GetSubpass()->GetSuperBatch(); + superBatch->Remove(this); + + // Free resources if (material) { material.Free(); material = {}; } - - // Remove from SuperBatch - Handle superBatch = sharedMaterial->GetBaseMaterial()->GetPipeline()->GetPipelineState().GetSubpass()->GetSuperBatch(); - superBatch->Remove(this); } /*-----------------------------------------------------------------------------------*/ @@ -51,20 +52,23 @@ namespace SHADE if (!material && sharedMaterial == materialInstance) return; + // Flag that material was changed + materialChanged = true; + // Free copies of materials if any if (material) { + oldMaterial = material; material.Free(); material = {}; } - - // Flag that material was changed - materialChanged = true; - if (sharedMaterial) - oldMaterial = sharedMaterial->GetBaseMaterial(); + else if (sharedMaterial) + { + oldMaterial = sharedMaterial; + } // Update the material - sharedMaterial = materialInstance; + sharedMaterial = materialInstance; } Handle SHRenderable::GetMaterial() const diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderable.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderable.h index bc885ca2..5312cf82 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderable.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderable.h @@ -55,7 +55,7 @@ namespace SHADE /* Getter Functions */ /*-------------------------------------------------------------------------------*/ bool WasMaterialChanged() const noexcept { return materialChanged; } - Handle GetPrevMaterial() const noexcept { return oldMaterial; } + Handle GetPrevMaterial() const noexcept { return oldMaterial; } /*-------------------------------------------------------------------------------*/ /* Batcher Dispatcher Functions */ @@ -74,7 +74,7 @@ namespace SHADE Handle sharedMaterial; Handle material; bool materialChanged = true; - Handle oldMaterial; + Handle oldMaterial; }; }