Added Transform Component

This commit is contained in:
Cocoa 2022-09-20 17:20:57 +08:00
parent 7f173b3207
commit cc6d1dd95b
7 changed files with 293 additions and 15 deletions

View File

@ -209,7 +209,8 @@ xcopy /s /r /y /q "$(SolutionDir)/Dependencies/dotnet/bin" "$(OutDir)"</Command
<ClInclude Include="src\Math\SHMathHelpers.hpp" />
<ClInclude Include="src\Math\SHMatrix.h" />
<ClInclude Include="src\Math\SHQuaternion.h" />
<ClInclude Include="src\Math\SHTransform.h" />
<ClInclude Include="src\Math\Transform\SHTransform.h" />
<ClInclude Include="src\Math\Transform\SHTransformComponent.h" />
<ClInclude Include="src\Math\Vector\SHVec2.h" />
<ClInclude Include="src\Math\Vector\SHVec3.h" />
<ClInclude Include="src\Math\Vector\SHVec4.h" />
@ -304,7 +305,8 @@ xcopy /s /r /y /q "$(SolutionDir)/Dependencies/dotnet/bin" "$(OutDir)"</Command
<ClCompile Include="src\Math\SHMathHelpers.cpp" />
<ClCompile Include="src\Math\SHMatrix.cpp" />
<ClCompile Include="src\Math\SHQuaternion.cpp" />
<ClCompile Include="src\Math\SHTransform.cpp" />
<ClCompile Include="src\Math\Transform\SHTransform.cpp" />
<ClCompile Include="src\Math\Transform\SHTransformComponent.cpp" />
<ClCompile Include="src\Math\Vector\SHVec2.cpp" />
<ClCompile Include="src\Math\Vector\SHVec3.cpp" />
<ClCompile Include="src\Math\Vector\SHVec4.cpp" />

View File

@ -420,7 +420,7 @@
<ClInclude Include="src\Math\SHQuaternion.h">
<Filter>Math</Filter>
</ClInclude>
<ClInclude Include="src\Math\SHTransform.h">
<ClInclude Include="src\Math\Transform\SHTransform.h">
<Filter>Math</Filter>
</ClInclude>
<ClInclude Include="src\Math\Vector\SHVec2.h">
@ -498,6 +498,7 @@
<ClInclude Include="src\Tools\SHUtilities.hpp">
<Filter>Tools</Filter>
</ClInclude>
<ClInclude Include="src\Math\Transform\SHTransformComponent.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\ECS_Base\Components\SHComponent.cpp">
@ -695,7 +696,7 @@
<ClCompile Include="src\Math\SHQuaternion.cpp">
<Filter>Math</Filter>
</ClCompile>
<ClCompile Include="src\Math\SHTransform.cpp">
<ClCompile Include="src\Math\Transform\SHTransform.cpp">
<Filter>Math</Filter>
</ClCompile>
<ClCompile Include="src\Math\Vector\SHVec2.cpp">
@ -738,5 +739,6 @@
<ClCompile Include="src\Tools\SHStringUtils.cpp">
<Filter>Tools</Filter>
</ClCompile>
<ClCompile Include="src\Math\Transform\SHTransformComponent.cpp" />
</ItemGroup>
</Project>

View File

@ -11,7 +11,7 @@
#pragma once
// Project Headers
#include "Math/SHTransform.h"
#include "Math/Transform/SHTransform.h"
#include "SH_API.h"
namespace SHADE

View File

@ -16,21 +16,41 @@
namespace SHADE
{
/*-----------------------------------------------------------------------------------*/
/* Stai Definitions */
/* Static Data Member Definitions */
/*-----------------------------------------------------------------------------------*/
const SHTransform SHTransform::Identity { SHVec3::Zero, SHVec3::Zero, SHVec3::One };
/*-----------------------------------------------------------------------------------*/
/* Constructors & Destructor Definitions */
/*-----------------------------------------------------------------------------------*/
SHTransform::SHTransform() noexcept
: position { SHVec3::Zero }
, rotation { SHVec3::Zero }
, scale { SHVec3::One }
: position { SHVec3::Zero }
, rotation { SHVec3::Zero }
, scale { SHVec3::One }
{}
SHTransform::SHTransform(const SHVec3& pos, const SHVec3& rot, const SHVec3& scl) noexcept
: position { pos }
, rotation { rot }
, scale { scl }
{}
/*-----------------------------------------------------------------------------------*/
/* Operator Overload Definitions */
/*-----------------------------------------------------------------------------------*/
bool SHTransform::operator==(const SHTransform& rhs) const noexcept
{
return !(position != rhs.position || rotation != rhs.rotation || scale != rhs.scale);
}
bool SHTransform::operator!=(const SHTransform& rhs) const noexcept
{
return (position != rhs.position || rotation != rhs.rotation || scale != rhs.scale);
}
/*-----------------------------------------------------------------------------------*/
/* Getter Function Definitions */
/*-----------------------------------------------------------------------------------*/
@ -40,7 +60,6 @@ namespace SHADE
return trs;
}
/*-----------------------------------------------------------------------------------*/
/* Public Function Member Definitions */
/*-----------------------------------------------------------------------------------*/

View File

@ -12,7 +12,7 @@
// Project Headers
#include "SH_API.h"
#include "SHMath.h"
#include "Math/SHMath.h"
namespace SHADE
{
@ -39,13 +39,20 @@ namespace SHADE
SHTransform (const SHTransform&) = default;
SHTransform (SHTransform&&) = default;
~SHTransform () = default;
SHTransform () noexcept;
SHTransform (const SHVec3& pos, const SHVec3& rot, const SHVec3& scl) noexcept;
/*---------------------------------------------------------------------------------*/
/* Operator Overloads */
/*---------------------------------------------------------------------------------*/
SHTransform& operator= (const SHTransform&) = default;
SHTransform& operator= (SHTransform&&) = default;
~SHTransform () = default;
SHTransform () noexcept;
[[nodiscard]] bool operator==(const SHTransform& rhs) const noexcept;
[[nodiscard]] bool operator!=(const SHTransform& rhs) const noexcept;
/*---------------------------------------------------------------------------------*/
/* Getter Functions */
@ -60,6 +67,10 @@ namespace SHADE
const SHMatrix& ComputeTRS();
private:
/*---------------------------------------------------------------------------------*/
/* Data Members */
/*---------------------------------------------------------------------------------*/
SHMatrix trs;
};

View File

@ -0,0 +1,129 @@
/****************************************************************************************
* \file SHTransformComponent.cpp
* \author Diren D Bharwani, diren.dbharwani, 390002520
* \brief Implementation for a Transform Component
*
* \copyright Copyright (C) 2022 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.
****************************************************************************************/
#include <SHpch.h>
// Primary Header
#include "SHTransformComponent.h"
namespace SHADE
{
/*-----------------------------------------------------------------------------------*/
/* Constructors & Destructor Definitions */
/*-----------------------------------------------------------------------------------*/
SHTransformComponent::SHTransformComponent() noexcept
: SHComponent {}
, parent { nullptr }
{}
/*-----------------------------------------------------------------------------------*/
/* Getter Function Definitions */
/*-----------------------------------------------------------------------------------*/
const SHTransform* SHTransformComponent::GetParent() const noexcept
{
return parent;
}
const SHVec3& SHTransformComponent::GetLocalPosition() const noexcept
{
return local.position;
}
const SHVec3& SHTransformComponent::GetLocalRotation() const noexcept
{
return local.rotation;
}
const SHVec3& SHTransformComponent::GetLocalScale() const noexcept
{
return local.scale;
}
const SHVec3& SHTransformComponent::GetWorldPosition() const noexcept
{
return world.position;
}
const SHVec3& SHTransformComponent::GetWorldRotation() const noexcept
{
return world.rotation;
}
const SHVec3& SHTransformComponent::GetWorldScale() const noexcept
{
return world.scale;
}
SHMatrix SHTransformComponent::GetLocalToWorld() const noexcept
{
return parent != nullptr ? parent->GetTRS() : SHMatrix::Identity;
}
SHMatrix SHTransformComponent::GetWorldToLocal() const noexcept
{
return parent != nullptr ? SHMatrix::Inverse(parent->GetTRS()) : SHMatrix::Identity;
}
const SHMatrix& SHTransformComponent::GetTRS() const noexcept
{
return world.GetTRS();
}
/*-----------------------------------------------------------------------------------*/
/* Setter Function Definitions */
/*-----------------------------------------------------------------------------------*/
void SHTransformComponent::SetParent(const SHTransform* parentTransform) noexcept
{
if (parent == nullptr)
SHLOG_WARNING("Removing parent transform from Entity {}", GetEID())
parent = parentTransform;
}
void SHTransformComponent::SetLocalPosition(const SHVec3& newLocalPosition) noexcept
{
local.position = newLocalPosition;
updateQueue.push({ UpdateCommandType::LOCAL_POSITION, newLocalPosition });
}
void SHTransformComponent::SetLocalRotation(const SHVec3& newLocalRotation) noexcept
{
local.rotation = newLocalRotation;
updateQueue.push({ UpdateCommandType::LOCAL_ROTATION, newLocalRotation });
}
void SHTransformComponent::SetLocalScale(const SHVec3& newLocalScale) noexcept
{
local.scale = newLocalScale;
updateQueue.push({ UpdateCommandType::LOCAL_SCALE, newLocalScale });
}
void SHTransformComponent::SetWorldPosition(const SHVec3& newWorldPosition) noexcept
{
world.position = newWorldPosition;
updateQueue.push({ UpdateCommandType::WORLD_POSITION, newWorldPosition });
}
void SHTransformComponent::SetWorldRotation(const SHVec3& newWorldRotation) noexcept
{
world.rotation = newWorldRotation;
updateQueue.push({ UpdateCommandType::WORLD_ROTATION, newWorldRotation });
}
void SHTransformComponent::SetWorldScale(const SHVec3& newWorldScale) noexcept
{
world.scale = newWorldScale;
updateQueue.push({ UpdateCommandType::WORLD_SCALE, newWorldScale });
}
} // namespace SHADE

View File

@ -0,0 +1,115 @@
/****************************************************************************************
* \file SHTransformComponent.h
* \author Diren D Bharwani, diren.dbharwani, 390002520
* \brief Interface for a Transform Component
*
* \copyright Copyright (C) 2022 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
#include <queue>
// Project Headers
#include "SH_API.h"
#include "ECS_Base/Components/SHComponent.h"
#include "SHTransform.h"
namespace SHADE
{
/*-----------------------------------------------------------------------------------*/
/* Type Definitions */
/*-----------------------------------------------------------------------------------*/
class SH_API SHTransformComponent : public SHComponent
{
public:
/*---------------------------------------------------------------------------------*/
/* Constructors & Destructor */
/*---------------------------------------------------------------------------------*/
~SHTransformComponent () override = default;
SHTransformComponent () noexcept;
// TODO(Diren): Do I implement these in a specific manner or delete?
SHTransformComponent (const SHTransformComponent&) = delete;
SHTransformComponent (SHTransformComponent&&) = delete;
SHTransformComponent& operator=(const SHTransformComponent&) = delete;
SHTransformComponent& operator=(SHTransformComponent&&) = delete;
/*---------------------------------------------------------------------------------*/
/* Getter Functions */
/*---------------------------------------------------------------------------------*/
[[nodiscard]] const SHTransform* GetParent () const noexcept;
[[nodiscard]] const SHVec3& GetLocalPosition () const noexcept;
[[nodiscard]] const SHVec3& GetLocalRotation () const noexcept;
[[nodiscard]] const SHVec3& GetLocalScale () const noexcept;
[[nodiscard]] const SHVec3& GetWorldPosition () const noexcept;
[[nodiscard]] const SHVec3& GetWorldRotation () const noexcept;
[[nodiscard]] const SHVec3& GetWorldScale () const noexcept;
[[nodiscard]] SHMatrix GetLocalToWorld () const noexcept;
[[nodiscard]] SHMatrix GetWorldToLocal () const noexcept;
[[nodiscard]] const SHMatrix& GetTRS () const noexcept;
/*---------------------------------------------------------------------------------*/
/* Setter Functions */
/*---------------------------------------------------------------------------------*/
void SetParent (const SHTransform* parentTransform) noexcept;
void SetLocalPosition (const SHVec3& newLocalPosition) noexcept;
void SetLocalRotation (const SHVec3& newLocalRotation) noexcept;
void SetLocalScale (const SHVec3& newLocalScale) noexcept;
void SetWorldPosition (const SHVec3& newWorldPosition) noexcept;
void SetWorldRotation (const SHVec3& newWorldRotation) noexcept;
void SetWorldScale (const SHVec3& newWorldScale) noexcept;
private:
/*---------------------------------------------------------------------------------*/
/* Type Definitions */
/*---------------------------------------------------------------------------------*/
enum class UpdateCommandType
{
LOCAL_POSITION
, LOCAL_ROTATION
, LOCAL_SCALE
, WORLD_POSITION
, WORLD_ROTATION
, WORLD_SCALE
};
struct UpdateCommand
{
public:
/*-------------------------------------------------------------------------------*/
/* Data Members */
/*-------------------------------------------------------------------------------*/
UpdateCommandType type;
SHVec3 data;
};
using UpdateQueue = std::queue<UpdateCommand>;
/*---------------------------------------------------------------------------------*/
/* Data Members */
/*---------------------------------------------------------------------------------*/
const SHTransform* parent;
SHTransform local;
SHTransform world;
UpdateQueue updateQueue;
};
} // namespace SHADE