# Rotate Mesh but keep child axis orientation

## Recommended Posts

I've tried searching google on this but i can't seem to find a solution.
What i want to achieve is that if i set a rotated mesh as parent then translating it through an axis would move it in the same direction as if it didn't have a rotated parent. For example in this playground

I would like the sphere to go "up" rather than towards the camera.

##### Share on other sites

I would add an empty mesh in between the ground and the sphere and rotate it to negate the rotation of the ground.

##### Share on other sites

I don't understand your question. Could you explain actual usage?

Cheers,

DB

##### Share on other sites

8 minutes ago, dbawel said:

I don't understand your question. Could you explain actual usage?

Cheers,

DB

I create a mesh A, rotate it by 90° on the X axis

I create a mesh B and set A.parent = B

i translate B on the y axis, but it doesn't move on the global y axis ("up"), it moves on the parent's rotated axis

The additional mesh could work but i was wondering if there's a more "conventional" solution (like a special rotation function that uses world axis)

##### Share on other sites

The idea of a parent is that the child moves relative to the parent.

This page from the documentation describes some of the relationship between parent and child and positions and rotation.

This page from the documentation does not deal with parenting but does show rotation and translation  using the world axis

If you are simply doing a rotation of -PI/2 as in your PG then you know that the z axis of the ground in now screen up as in http://www.babylonjs-playground.com/#26DEBS#8

If you want something more complicated then please try to describe the situation you are aiming for, it might all come down to when you set the parent.

##### Share on other sites

I know the theory behind and i know i could just translate it over the right axis. My problem lies on the fact that i might have a function that should be "mesh agnostic" and does a translation over an axis. Say:

``````function goUp(mesh) {
mesh.position.y += 1;
}``````

if i pass to this function a mesh with a rotated parent (any kind of rotation) it won't move the mesh "up"

Mind that this is just an example (and i don't have a more concrete one) so using another approach is not the solution here

##### Share on other sites

Nothing, i'd just like to know if there's (or if it can be implemented) a "native" method to do that

##### Share on other sites

Another possible solution is rotating the vertices of the parent instead of rotating the coordinate system.

Edit: performance is much worse with this solution though.  If you are going to be rotating the parent every frame, I would go with the extra mesh solution.

##### Share on other sites

Thanks, i guess that's the closest one can get to a "native" function

##### Share on other sites

Looks like there actually might be a more conventional solution.  Look at changing the frame of reference at the bottom of this page.

Edit:  I didn't have any luck getting a playground working using TransformCoordinates.  Maybe someone else will.

##### Share on other sites

There are many good solutions that would work, however now that I understand what you want to do, simply parent both mesh A and B to a null or transperent object. Then rotate mesh A and translate mesh B all you want. And if both must translate together, then translate the parent object.

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

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.