Jump to content

Understanding Parent-Child Behaviour


touslecoq
 Share

Recommended Posts

In the app I am building, the user can drag in different components that snap together (think lego).  As part of that I am introducing grouping (think Microsoft Powerpoint where you can add individual shapes but then group them and any transformations are then applied as a group).

The approach I have used so far is that when individual components (meshes) are added they have no parent.  When component A is snapped to component B it checks whether either has a parent. If neither has a parent (ie neither is yet part of a group) then it creates an invisible mesh and makes that the parent of both.  If one or more has a parent (ie they are already part of a group) then it combines them under one parent (if both have parents, some logic will be introduced to determine which would be the best parent).  This works to an extent.

The problem I am seeing is that the meshes may have been moved around prior to being grouped.  So their position will have a value.  What seems to be happening when the parent is set is that the component's position is being re-evaluated relative to the parent and so the component jumps to a new position in space.

I just wanted to first validate that this is how parenting should work and also get some advice on how to counter it's behaviour.  I don't want the child meshes to move when I set the parent.  However from that point on I do want any further movement or rotation to be done as a group.  I have already amended my onMouseMove function so that if a mesh has a parent, the movement is applied to the parent (and therefore ALL children) rather than just the mesh being moved.  This works a treat.

This little playground illustrates the behaviour I am seeing.  It creates 3 meshes ... m1, m2 and parent and gives them each a new position away from the world origin.  It writes their position and getAbsolutePosition to the console. It then sets m2.parent=parent and again writes their position to the log.  You can see that m2 is initially positioned at (10,0,0) and parent is at (20,0,0).  However when m2.parent is set to parent, m2 moves to (30,0,0).  So its position is now being evaluated relative to its parent (ie 20+10 along the x axis).  This is contrary to how I have seen the behaviour described elsewhere in which it seemed to suggest that position was nullified upon setting a parent.

Thoughts?

Thanks

Rich

 

Link to comment
Share on other sites

IGNORE - NOW RESOLVED!

Thanks Adam - now I am confused.

I may well be misunderstanding, but from that demo you linked it looks as though every 3 seconds box1 flips between being box2's parent or not.  Each time it does that, box2 moves position.  What I am trying to achieve is to prevent the real world movement of position when the parenting/unparenting happens.  Reading through the parentMesh function it does appear that this is what that function is trying to achieve also - is it actually working?

I created a simple version of your demo (without spinning boxes) to illustrate http://www.babylonjs-playground.com/#PV7DK#48 and copied the parentMesh function exactly.  If I comment out line 69 (child.parent=parent;) you can see that the result of the parent function is to move box2 to 0,0,0.  Therefore when line69 is uncommented box 2 seems to disappear because it is put in the same place as box1 (ie displacement from box1 position of 0,0,0).

Thanks

Rich

Link to comment
Share on other sites

IGNORE - NOW RESOLVED

Actually to my first para - I see what it is doing now.  it moves when it the parent is set, because the parent is rotating.  If I stop the child from rotating its much clearer what is happening.  Still not sure what is happening in my version then and why setting the parent still changes its real world position.

Link to comment
Share on other sites

Right I think there was a glitch in my playground (I may have hit back button when testing it) as I have pasted into a new version and it DOES seem to work now.

So .. no spinning boxes just 2 boxes that parents/unparent every 3 seconds and you can see there is no relative movement.  It prints the location to log just before and after parenting and you can see how the chid mesh's position does change to counteract the action of parenting.

http://www.babylonjs-playground.com/#2ATWYQ#2

Thanks Adam :).

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...