Rotate node specific value on Z Axis based on the camera


If this is a duplicate let me know and please keep in mind im still new to jme3 and all its api´s.

since my last question got anwered pretty fast, for now i have only one more question.

I have a ChaseCamera based on SimpleApplication#cam.

My question is: when i press w for example. I want a node/spatial to rotate away from the Camera like in most 3 person games on the Z axis.
Im pretty sure, there is a way. but i was unsuccessful until now.


Rotate a spatial on the z-axis (which is roll by the way since y is up):

spatial.rotate(0, 0, radiansToRotate);

If you really mean to steer… then you probably want to rotate around the y (or up) axis:
spatial.rotate(0, radiansToRotate, 0);

Edit: Or you may need to better explain what you mean.

For reference, in JME’s default reference frame:
x-axis runs through your ears
y-axis runs through your neck out the top of your head
z-axis runs out your nose

Sry, i didnt clarify something,
while in theory i mean the Y axis, as you pointed out, pratically i need to rotate on the Z axis.
That comes from the fact that by default the model has a wrong rotaion.
And my question is, how do i get the radiansToRotate value.
I am sorry for the mistake

How far do you want to rotate it?

To the Opposite direction of the direction the camera is looking.
For example the character is pointing exactly towards the camera on the Y axis(JME3 standard) = 180 degrees

Edit: Basically like in most third person games, u press w, the character turns away from the camera

Do you mean the camera snaps to true north or south or just 180 from its current location?

from its current location.
true north would not be that hard to do iirc :smiley:

As Paul gave you the answer you sought, I will point to the wiki references.

If you click the wiki link in the forum nav menus you will find links to the main topics.

The links to the dummies topics are must reads.

Thats not my problem, i know how to rotate the node by a specific amount of degrees.
Its just, in my case i dont know how to get that amount of degrees.

íts probably starting with cam.rotation.y

@mitm @pspeed
First i fixed my wrongly roated model problem. So im only using the Y axis
Than i tried this:

 val camRot = manager.getCam().rotation.normalizeLocal()
 val modelRot = charNode.localRotation.normalizeLocal()
 modelRot.set(modelRot.x, camRot.y, modelRot.z, modelRot.w)
 charNode.localRotation = modelRot 

This actualy does what i want for the most part, though when camRot.y is between -0.8 and 0.9 it doesnt work.
Please dont be confused this is Kotlin and not java

Quaternions are a magic black box. Do not set the values directly. They do not mean what you think they mean.

To rotate a spatial 180 degrees… you rotate it PI. PI = 180 degrees.

spatial.rotate(0, FastMath.PI, 0);

…will turn a spatial around 180 degrees.

And if you mean 180 degrees in world space as related to its local space, blah blah… then there is still ways to do that, too… you just have to change basis. But you will need to explain in more detail what you are actually trying to accomplish because I don’t understand what’s so difficult. So the problem is probably my misunderstanding the question.

I think I see now.

Take the camera quaternion… set it to the spatial. Then call spatial.rotate(0, 0, FastMath.PI)… or if you want you can compose the full rotation with the original and another quaternion.

But if you find yourself setting quaternion values directly then you are doing something wrong. Else, you have some higher order understanding of four dimensional space and I wish you could explain it to the rest of us. :wink:

Though now i need a way to negate the X axis, because the model now falls to the front.

var camRot = manager.getCam().rotation.normalizeLocal()
charNode.localRotation = camRot

this Snippet just works fine, just i need to substract the X axis value of the rotation.

var camRot = manager.getCam().rotation.normalizeLocal()
charNode.localRotation = camRot
charNode.rotate(0f, FastMath.PI, 0f)

The only difference here is that the node gets turned towards the camera, equvalent of pressing S, short after W in a third person game = the character rotates towards the camera

So whats the best way to negate that bad side effect?

@pspeed Yeah the Problem is that by rotating all axis like the camera, also X and Z are altered which should not happen.
Cause only the Y Axis should be altered.
Maybe Node#rotateUpTo might be helpful?

    walkDirection.set(0f, 0f, 0f)
    if (left) walkDirection.addLocal(camLeft)
    if (right) walkDirection.addLocal(camLeft.negate())
    if (up) walkDirection.addLocal(camDir)
    if (down) walkDirection.addLocal(camDir.negate())

    if (sprinting) {
        walkDirection = walkDirection.mult(3f)
    player.walkDirection = walkDirection

This is what moves the character, maybe here i can get the angle in degrees or rads and rotate the character node around the Y axis just as much as i need? Here is the full code btw: hastebin

I mean, if you can explain the problem clearly when you post the first time then we can avoid a lot of back and forth. “Is this your answer?” “Is this your answer?” “Is this your answer?” “Is this your answer?”

You keep adding requirements so we’ll have to keep responding.

It sounds like you only want to rotate “something” to negative up axis rotation of the camera. But you controlled the camera in the first place so probably already knew that value… and if not then you should have.

Just calculate the camera’s position from yaw and pitch in the first place and then none of this is an issue.

Else you will have to do some error prone Quaternion.toAngles()/fromAngles() stuff and then deal with the artifacts.

I dont keep adding requirements.
Yes i made the mistake by saying i want to rotate on the Z axis while i meant the Y axis.

What i want is a Node, that rotates on the Y Axis and ONLY on the Y Axis based on the Y Axis value from the Camera, SimpleApplication#cam.
I tried to explain it by giving the examplee of most third person games, how the character rotates when pressing W,S,A or d.
When pressing W, the character turns his back towards the camera before starting to move.
When pressing S, the Character faces towards the Camera before starting to move.

But thanks for your answer.
“Just calculate the camera’s position from yaw and pitch in the first place and then none of this is an issue.”, how?
I also added in the first post thn im quite new to jme3, ofc i have a basic understanding of a 3d world with 3 Axis, but yeah thats it.

Thanks for helping me.


On a new project, it can be fun to poke around the javadoc just to see what’s there sometimes.

Sorry for causing so much trouble :frowning:

But can you maybe give me an whole example how to use that function?
I basically have no idea which arguments to pass into the Quaternion#fromAngles function neither how to process after.