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 a946c74..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) @@ -108,6 +131,8 @@ class ObjectTransform Orientation = QuaternionFromMatrix(WorldMatrix); + GetParent()->RemoveChild(this); + Reparent(nullptr); }