From c5d786a578da3ac0a41c385d4f4948b00eed265e Mon Sep 17 00:00:00 2001 From: angeloaf20 Date: Mon, 30 Dec 2024 13:48:46 -0500 Subject: [PATCH 1/2] added RemoveChild method --- transform_hierarchy/object_transform.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/transform_hierarchy/object_transform.h b/transform_hierarchy/object_transform.h index a946c74..e0150e2 100644 --- a/transform_hierarchy/object_transform.h +++ b/transform_hierarchy/object_transform.h @@ -95,6 +95,29 @@ class ObjectTransform Parent->Children.push_back(this); } + inline ObjectTransform* RemoveChild(ObjectTransform* child) + { + // if the node has no children, return a null pointer + if (Children.empty()) + { + return nullptr; + } + + // find the child's position in the children list + auto childIter = std::find(Children.begin(), Children.end(), child); + + // if the children list doesn't contain the child, return a nullptr + if (childIter == Children.end()) + { + return nullptr; + } + + // remove the child + Children.erase(childIter); + + return child; + } + inline void Detach() { if (!GetParent()) From 71659ce308f223907e5d3ec2fc6ca9092122f640 Mon Sep 17 00:00:00 2001 From: angeloaf20 Date: Mon, 30 Dec 2024 14:12:22 -0500 Subject: [PATCH 2/2] added RemoveChild method and added example usage --- transform_hierarchy/main.cpp | 6 ++++ transform_hierarchy/object_transform.h | 48 ++++++++++++++------------ 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/transform_hierarchy/main.cpp b/transform_hierarchy/main.cpp index 5a9fdce..7ea92f3 100644 --- a/transform_hierarchy/main.cpp +++ b/transform_hierarchy/main.cpp @@ -32,6 +32,7 @@ #include "object_transform.h" #include +#include #include ObjectTransform PlayerBase; @@ -67,6 +68,8 @@ void GameInit() GunModel = LoadModel("resources/blasterD.glb"); OverlayTexture = LoadRenderTexture(GetScreenWidth(), GetScreenHeight()); + + } bool GameUpdate() @@ -92,6 +95,9 @@ bool GameUpdate() if (IsKeyDown(KEY_D)) PlayerBase.MoveH(GetFrameTime() * -10); + if (IsKeyPressed(KEY_SPACE)) + GunNode.Detach(); + return true; } diff --git a/transform_hierarchy/object_transform.h b/transform_hierarchy/object_transform.h index e0150e2..976cc05 100644 --- a/transform_hierarchy/object_transform.h +++ b/transform_hierarchy/object_transform.h @@ -49,6 +49,29 @@ class ObjectTransform ObjectTransform* Parent = nullptr; std::vector Children; + inline ObjectTransform* RemoveChild(ObjectTransform* child) + { + // if the node has no children, return a null pointer + if (Children.empty()) + { + return nullptr; + } + + // find the child's position in the children list + auto childIter = std::find(Children.begin(), Children.end(), child); + + // if the children list doesn't contain the child, return a nullptr + if (childIter == Children.end()) + { + return nullptr; + } + + // remove the child + Children.erase(childIter); + + return child; + } + public: ObjectTransform(bool faceY = true) @@ -95,29 +118,6 @@ class ObjectTransform Parent->Children.push_back(this); } - inline ObjectTransform* RemoveChild(ObjectTransform* child) - { - // if the node has no children, return a null pointer - if (Children.empty()) - { - return nullptr; - } - - // find the child's position in the children list - auto childIter = std::find(Children.begin(), Children.end(), child); - - // if the children list doesn't contain the child, return a nullptr - if (childIter == Children.end()) - { - return nullptr; - } - - // remove the child - Children.erase(childIter); - - return child; - } - inline void Detach() { if (!GetParent()) @@ -131,6 +131,8 @@ class ObjectTransform Orientation = QuaternionFromMatrix(WorldMatrix); + GetParent()->RemoveChild(this); + Reparent(nullptr); }