Jump to content

How do keep transformations after de-parenting mesh


ozRocker
 Share

Recommended Posts

I have a mesh that is sometimes grouped with another mesh via parenting.  That way you can move both meshes together.  However I want to be able to separate those meshes and have the meshes stay the exact same size, position and direction as they were when they had a parent.  I've demonstrated the problem here http://babylonjs-playground.com/#ZXCRV

At the last step, when I remove the parent, the mesh snaps back to its original spot instead of stays where its at.  Is there a way in Babylon.js to have it retain its transformations?  If not in this framework, is there a mathematical way of doing it?  I tried using a rotation matrix to and I got it working when there's a change in one axis only, but can't get it working if there's a change in multiple axes.

Link to comment
Share on other sites

hmm..I need to get this working on a server.  Since Babylon.js doesn't work in headless mode I need to find a way to implement this myself.  Is there a way I can calculate absolute position of child from parent position, child position and a rotation vector without using Babylon.js functions?  I've tried using functions from here http://gamedev.stackexchange.com/questions/25567/transform-coordinates-from-3d-to-2d-without-matrix-or-built-in-methods but I just can't get it to work if the parent has been rotated on all 3 axis.  It'll work on 2 axis, but not all 3.

Link to comment
Share on other sites

22 hours ago, Temechon said:

child.rotation.addInPlace(parent.rotation) maybe ? Same for scaling... Didn't try it though.

I'm trying this one out.  My rotation vector is zero because I'm using quaternions but I couldn't figure out how to add rotation quaternions properly so I converted them to euler angles then added those.  I think there's some big rounding errors with that though 'cos it doesn't match it completely.  You can see it here http://babylonjs-playground.com/#ZXCRV#1

 

Link to comment
Share on other sites

  • 2 weeks later...
On 10/07/2016 at 7:25 PM, ozRocker said:

hmm..I need to get this working on a server.  Since Babylon.js doesn't work in headless mode I need to find a way to implement this myself.  Is there a way I can calculate absolute position of child from parent position, child position and a rotation vector without using Babylon.js functions?  I've tried using functions from here http://gamedev.stackexchange.com/questions/25567/transform-coordinates-from-3d-to-2d-without-matrix-or-built-in-methods but I just can't get it to work if the parent has been rotated on all 3 axis.  It'll work on 2 axis, but not all 3.

For anyone that cares, I figured out why I was getting the rotation wrong.  I had the multiplication order wrong.  I built a rotation matrix using the order X, Y, Z.  It should be X, Z, Y.  To do this I had to multiply Y rotation matrix with Z rotation matrix then with X rotation matrix.

Link to comment
Share on other sites

ok, I have a complete working solution now.  This is in C# (because that's what my server is running) but its easy to port to any language.  I'm using quaternions for rotation now so the previous post doesn't apply.

The following code will unparent a mesh and let that mesh retain its position, rotation (quaternion) and scale:

Quaternion rotatedPoint = objParent.rotation.multiply(objChild.position);
objChild.position.x = rotatedPoint.x * objParent.scale.x + objParent.position.x;
objChild.position.y = rotatedPoint.y * objParent.scale.y + objParent.position.y;
objChild.position.z = rotatedPoint.z * objParent.scale.z + objParent.position.z;

objChild.scale.x *= objParent.scale.x;
objChild.scale.y *= objParent.scale.y;
objChild.scale.z *= objParent.scale.z;
objChild.rotation = objParent.rotation.add(objChild.rotation);

My quaternion rotation code is here:

	public Quaternion multiply(Vector3 point)
	{
		Quaternion[] arrQ = new Quaternion[2];
		arrQ[0] = new Quaternion(point.x, point.y, point.z, 0);
		arrQ[1] = new Quaternion(-this.x, -this.y, -this.z, this.w);

		var qSoFar = new Quaternion(this.x, this.y, this.z, this.w);
		for (var i = 0; i < arrQ.Length; i++)
		{
			var temp = arrQ[i];
			var next = new Quaternion(temp.x, temp.y, temp.z, temp.w);

			double w = qSoFar.w * next.w - qSoFar.x * next.x - qSoFar.y * next.y - qSoFar.z * next.z;
			double x = qSoFar.x * next.w + qSoFar.w * next.x + qSoFar.y * next.z - qSoFar.z * next.y;
			double y = qSoFar.y * next.w + qSoFar.w * next.y + qSoFar.z * next.x - qSoFar.x * next.z;
			double z = qSoFar.z * next.w + qSoFar.w * next.z + qSoFar.x * next.y - qSoFar.y * next.x;

			qSoFar.x = x;
			qSoFar.y = y;
			qSoFar.z = z;
			qSoFar.w = w;
		}
		return qSoFar;
	}

 

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...