Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove detached child from Children vector #11

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions transform_hierarchy/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "object_transform.h"

#include <vector>
#include <iostream>
#include <list>

ObjectTransform PlayerBase;
Expand Down Expand Up @@ -67,6 +68,8 @@ void GameInit()
GunModel = LoadModel("resources/blasterD.glb");

OverlayTexture = LoadRenderTexture(GetScreenWidth(), GetScreenHeight());


}

bool GameUpdate()
Expand All @@ -92,6 +95,9 @@ bool GameUpdate()
if (IsKeyDown(KEY_D))
PlayerBase.MoveH(GetFrameTime() * -10);

if (IsKeyPressed(KEY_SPACE))
GunNode.Detach();

return true;
}

Expand Down
25 changes: 25 additions & 0 deletions transform_hierarchy/object_transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,29 @@ class ObjectTransform
ObjectTransform* Parent = nullptr;
std::vector<ObjectTransform*> 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)
Expand Down Expand Up @@ -108,6 +131,8 @@ class ObjectTransform

Orientation = QuaternionFromMatrix(WorldMatrix);

GetParent()->RemoveChild(this);

Reparent(nullptr);
}

Expand Down