createObjectGroup() Explanation!
-
Hi folks. It's come to our attention due to recent reports from users the object groups aren't behaving in the way some users are expecting.
At first we assumed this was perhaps a bug, but I believe I misunderstood the report and instead I'd like to offer an explanation of how object groups work.
This information will be added to the F4NS Help Pages for the relevant functions in the near future.
Group Space vs. World Space
When positioning an object in 3D space, we are using World Space coordinates by default. Consider the following code:
obj_1 = placeObject(cube, {-2, 0, 0}, {1, 1, 1}) obj_2 = placeObject(cube, {2, 0, 0}, {1, 1, 1})
We are positioning two cubes at -2 and +2 on the X axis in World Space.
Now let's create a group at a particular location. Let's say we want these cubes to be in a group at location {10, 0, 0}
group = createObjectGroup({10, 0, 0})
When we do the above, we are creating a special object (a group) at a position in World Space. So far so good, however the confusion occurs when we decide to set our cubes as children to the group:
setObjectParent(obj_1, group) setObjectParent(obj_2, group)
At this point, the cube's positions are converted to Group Space as opposed to World Space. This means that the cubes now appear at -2 and +2 in relation to the group's position. This means they will be at {8, 0, 0} and {12, 0, 0} in World Space. However, they are still at {-2, 0, 0} and {2, 0, 0} in Group Space.
Why?!
To some this might seem counter-intuitive. I've already positioned my objects, why should they change position now I've decided they should be in a group?
Well, it might require simply changing your approach to making object groups. If you're going to make an object group, you must bear this in mind from the beginning.
The great part is that when creating a complex grouped objects, we don't need to worry about remembering where the group is going to be and positioning all our child objects in the right place in World Space, we simply need to think about them as Group Space (which is effectively like building the object always at {0, 0, 0}.
We then just put the group wherever we need it!
Unlinking Objects
When unlinking objects we run into more potential confusion.
when an object becomes unlinked, its position is converted back into world space. This will mean the object appears to "jump" back to its position in world space as defined at the start of the post. In this example that'd be either {-2, 0, 0} or {2, 0, 0}.
Let's say you've moved your group somewhere else in World Space and now you want to unlink. If we want to retain the object's new position as it should be after being moved in its group, you could simply update the object's position by using the group position plus the group space location of the object. That was a confusing sentence. Here's an example:
unlinkObject(obj_1) setObjectPos(obj_1, groupPos + {-2, 0, 0})
Let me know below if I can clear anything up regarding this. Also, as mentioned at the start there will be an update to these help pages in the next patch.
Thanks!