Attach all objects with one common "Root" object
From what I understand, you want to connect objects together so that they can act like one object and be moved around. Here's a way to do this:
- Each object has a
parent
variable (similar to what you already tried). If that variable is empty, the object has no parent.
- If object
a
and object b
are about to be attached together, decide which one of them should be the parent. Let's assume for this example we want object a
to become the parent.
- Make object
b
's parent
variable point to object a
.
- Whenever the user is about to move, rotate or otherwise transform an object, search recursively through the object's
parent
value, until you find an object with no parent
value (the root object).
- Transform that root object, as their transformations should affect all children, making it look like they all move as one object.
Example
Let's say we have objects a
, b
(with a
as the parent
) and c
(with b
as the parent
).
- If you are about to move
a
, do it as normal, and it should affect all children.
- If you are about to move
b
, check its parent
which is a
, which doesn't have a parent
(it's the root object) so move that.
- Similarly if you are about to move
c
, check its parent
which is b
, whose parent is a
, which doesn't have a parent, so move a
again.