digEmAll

How to set mesh.parent=null without losing rotation

Recommended Posts

Hi All,

the issue I'm facing is probably trivial to solve but I couldn't find a solution.

Basically I need to set the parent of a mesh (used as rotation pivot) to null after a rotation, because I need to use that pivot again for another group of meshes.

I tried to use the following approach:

var pos = box.getAbsolutePosition();
var rot = box.rotation;
box.parent = null;
box.setAbsolutePosition(pos);
box.rotation = rot;

but it seem to keep only the position, but not the rotation.

I have created a simple playground to reproduce the problem : https://www.babylonjs-playground.com/#S13YXG#2

As you can see, when the animation is completed the bigger box returns to the original rotation.

Any suggestion ?

Thanks a lot in advance

Share this post


Link to post
Share on other sites

Hi @digEmAll

As you're using rotationQuaternion, box.rotation won't do anything.

furthermore, you should clone the values instead of referencing them (reference will change as the box.parent is removed)
 

var pos = box.getAbsolutePosition().clone();
var rot = box.parent.rotationQuaternion.clone();

    box.parent = null;
    box.setAbsolutePosition(pos);
    box.rotationQuaternion = rot;

https://www.babylonjs-playground.com/#S13YXG#4

Share this post


Link to post
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...

  • Recently Browsing   0 members

    No registered users viewing this page.