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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.