Skip to content

Commit

Permalink
Corrected the parenting logic
Browse files Browse the repository at this point in the history
  • Loading branch information
RiverHillbug committed Mar 2, 2024
1 parent 7e7c18c commit 788acf4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 26 deletions.
56 changes: 37 additions & 19 deletions Minigin/GameObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,44 +37,68 @@ void GameObject::Update(const float deltaTime)
}
}

glm::vec2 GameObject::GetWorldPosition() const
{
if (m_pParent)
{
return m_LocalTransform.GetPosition() + m_pParent->GetWorldPosition();
}

return m_LocalTransform.GetPosition();
}

void GameObject::SetWorldPosition(const float x, const float y)
{
SetWorldPosition(glm::vec2(x, y));
}

void GameObject::SetWorldPosition(const glm::vec2& position)
{
m_Transform.SetPosition(position);
m_LocalTransform.SetPosition(position);

if (m_pParent)
{
m_LocalPosition = m_pParent->GetTransform().GetPosition() - m_Transform.GetPosition();
m_LocalTransform.SetPosition(- m_pParent->GetTransform().GetPosition() + m_LocalTransform.GetPosition());
}
}

void GameObject::SetLocalPosition(const glm::vec2& position)
{
m_LocalTransform.SetPosition(position);
//m_LocalPositionIsDirty = true;
}

void GameObject::SetLocalPosition(const float x, const float y)
{
m_LocalTransform.SetPosition(x, y);
//m_LocalPositionIsDirty = true;
}

void GameObject::SetParent(GameObject* pParent, bool keepWorldPosition)
{
// Parent should not be one of the children, the GameObject itself, or the current parent
if (IsChild(pParent) || (!pParent && pParent == this) || m_pParent == pParent)
return;

if (pParent == nullptr)
{
// Set new localPosition as current worldPosition
m_Transform = m_LocalPosition;
}
else
if (keepWorldPosition)
{
if (keepWorldPosition)
if (m_pParent != nullptr)
{
// Set localPosition as (worldPosition - parent.WorldPositon)
m_LocalPosition = m_Transform.GetPosition() - m_pParent->GetTransform().GetPosition();
// Set new localPosition as current worldPosition
m_LocalTransform.SetPosition(m_LocalTransform.GetPosition() + m_pParent->GetWorldPosition());
}

// SetPositionDirty()
// did not add this right now because we need the worldPosition immeadiately for Render()
if (pParent != nullptr)
{
// Set localPosition as (worldPosition - parent.WorldPositon)
m_LocalTransform.SetPosition(m_LocalTransform.GetPosition() - pParent->GetWorldPosition());

}
}

// SetPositionDirty()
// did not add this right now because we need the worldPosition immeadiately for Render()

if (m_pParent)
{
m_pParent->RemoveChild(this);
Expand Down Expand Up @@ -115,9 +139,3 @@ bool GameObject::IsChild(const GameObject* pChild)
//std::ranges::find does not work here, some issue with move constructor, which weak_ptr doesn't have
return std::find(m_Children.begin(), m_Children.end(), pChild) != m_Children.end();
}

void GameObject::SetLocalPosition(const glm::vec3& position)
{
m_LocalPosition.SetPosition(position);
//m_LocalPositionIsDirty = true;
}
10 changes: 5 additions & 5 deletions Minigin/GameObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ class GameObject final

void SetParent(GameObject* pParent, bool keepWorldPosition);

inline Transform GetTransform() const { return m_Transform; }
inline Transform GetTransform() const { return m_LocalTransform; }
glm::vec2 GetWorldPosition() const;
void SetWorldPosition(const float x, const float y);
void SetWorldPosition(const glm::vec2& position);
void SetLocalPosition(const float x, const float y);
void SetLocalPosition(const glm::vec2& position);
inline bool IsDestroyed() const { return m_IsDestroyed; }
//inline bool IsLocalPositionDirty() const { return m_LocalPositionIsDirty; }

Expand All @@ -47,8 +50,7 @@ class GameObject final
GameObject& operator=(GameObject&& other) = delete;

private:
Transform m_Transform{}; //WorldPosition
Transform m_LocalPosition{};
Transform m_LocalTransform{}; //WorldPosition
//bool m_LocalPositionIsDirty{ false };
bool m_IsDestroyed{ false };

Expand All @@ -60,6 +62,4 @@ class GameObject final
void AddChild(GameObject* pChild);
void RemoveChild(GameObject* pChild);
bool IsChild(const GameObject* pChild);

void SetLocalPosition(const glm::vec3& position);
};
2 changes: 1 addition & 1 deletion Minigin/Sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void Sprite::Render() const

if (const auto owner{ GetOwner() })
{
const auto& pos = owner->GetTransform().GetPosition();
const auto& pos = owner->GetWorldPosition();
Renderer::GetInstance().RenderTexture(*m_pTexture, pos.x, pos.y);
}
}
Expand Down
2 changes: 1 addition & 1 deletion Minigin/Text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void Text::Render() const

if (const auto owner{ GetOwner() })
{
const auto& pos = owner->GetTransform().GetPosition();
const auto& pos = owner->GetWorldPosition();
Renderer::GetInstance().RenderTexture(*m_pTextTexture, pos.x, pos.y);
}
}
Expand Down

0 comments on commit 788acf4

Please sign in to comment.