Rotate arround parent axis

Hi there,

i have a node with some Geometry objects attached and i want to rotate the node on user input on the x, y, or z, each time by 90°

E.g. i want to rotate first on the x-Axis by 90° and after that on the y-Axis by 90°. But if i use the localRotation of the node, it rotates around the parent (or global) z-Axis.

I didn´t find a easy solution for that and i also don´t know how to solve it.

i´m afraid that i need a complex math calculation or maybe i it to easy and i´m only thinking to complex :smile:

maybe someone can help me or has an idea for me

So you basically want to retrieve e.g. the node’s local Z-axis, which always points to the node’s front, even if the node is rotated?

You can get the local axes by multiplying the node’s local rotation with the Vector3f constants:

Vector3f actualZAxis = node.getLocalRotation().mult(Vector3f.UNIT_Z);

You can then rotate the node around those axes.

i mean
if i look from the Z-axis to the node (Cam), and i hit rotateOnZAxis(). i will that the node is rotating on the z-axis im looking from
and not the z-axis from node

Ok. In this case you can get your rotation axis the same way, but use the camera’s rotation instead of the node’s rotation:

Vector3f rotationZAxis = cam.getRotation().mult(Vector3f.UNIT_Z);

i have now

Vector3f rotate = node.getParent().getLocalRotation().mult(axis);
quad = new Quaternion().fromAngleAxis(FastMath.PI/2, rotate);
node.rotate(quad);

axis is a given Vector3f for X,Y or Z Axis

Have there is the same problem, if i rotate only on one of these Axis, its working, but wen i rotate on a second axis then it rotating again arround the nodes local axis

Can you please post the complete code of what you are doing?

Do note that this is a cumulative effect meaning that the passed ‘quad’ rotation will be multiplied against the existing rotation. This may or may not be what you want… and it may be exactly backwards (ie: you really want the existing rotation post-multiplied with your new one).

I have trouble wrapping my head around the effect you are trying to achieve so I can’t be sure.

// the rotate function

public void rotateBlock(Vector3f axis){
    Vector3f rotate = node.getParent().getLocalRotation().mult(axis);
    Quaternion quad = new Quaternion().fromAngleAxis(FastMath.PI/2, rotate);
    node.rotate(quad);
}

// user input

private ActionListener actionListener = new ActionListener(){
     public void onAction(String name, boolean keyPressed, float tpf){
         if(name.equals("q") && keyPressed) {
             rotateBlock(new Vector3f(0,-1,0));
         }
         if name.equals("e") && keyPressed) {
             rotateBlock(new Vector3f(0,1,0));
         }
         if(name.equals("r") && keyPressed) {
             rotateBlock(new Vector3f(-1,0,0));
         }
         if(name.equals("f") && keyPressed) {
             rotateBlock(new Vector3f(1,0,0));
         }
     }
 }

Didn’t you want to rotate the block around the camera’s axes?

See, this is almost certainly wrong… first you get the axis relative to the Current rotation… then you make a rotation about that axis… then you multiply that axis by the current rotation… so it seems like you double-apply the rotation basically.

What is the actual effect you are trying to achieve? Can you describe it in terms of another game we might know?

no not around the camera´s axes, it should rotate on a fixed axes that has nothing to do with the block/node i want rotate
so if i want rotate around the Vector3f.UNIT_Z the local rotate of the block can be X,Y or Z Axis
the getParent() give be back the root node

hmmm, i mean like this
Spielhalle | NETZWELT

the block is rotating always the same global direction, not on the locals axis

Oh. In this case just calling the rotate(float, float, float) method of the Node should do the trick (and you don’t even have to worry about Quaternions):

node.rotate(FastMath.HALF_PI, 0, 0); // 90° rotation around x-axis

Since this changes the node’s local rotation, the effect in the game world is as if it had been rotated around it’s parent’s axes

but this is also only working for the same axes or?
so if i want to rotate

node yaw 90
node pitch 90

and i use the rotate() function
for me it looks like the node rotating

node yaw 90
node roll 90 <— and this is my problem

As I said earlier… you need to stop using rotate() and pre-multiply your new rotation with the old one. But I think my posts aren’t being seen.

Just in case my posts are being seen:

Quaternion toAdd = new Quaternion().fromAngles(whatever);
Quaternion existing = spatial.getLocalRotation();
spatial.setLocalRotation(toAdd.mult(existing));

…that will do the opposite of rotate.

Yeah, sorry. I totally missed the point and thought you wanted it to rotate around the parent axes.
So in this case: What pspeed says

no sorry i read your posts

working now, thank you very much :smile:

Maybe you are looking this Turn around static axes - #4 by pspeed

thanks but its already working for me with the solution from pspeed

but maybe i can need it on an other place :slight_smile: thanks