Autorotate a CameraNode

Hi,

I have a Player with a CameraNode, this player with a key pression follows a path that bring it to its destination.

Having the destination how can I rotate my character so that it look at this direction (the direction in which it moves)?



thanks.

Seems like Spatial.lookAt(position, upVector) should do what you want.

If I understand your question correctly you want to just be able to rotate your character so that it faces the direction to a point?



If this is the case, then the first thing you should do is to calculate the direction vector between your player and the desired point (if you havn't already done that).



You would also need a vector defined for your character, which points in the direction which the character currently is looking. This will of course have to be updated every time you rotate your character.



Last thing you need to do is calculate the angle between the direction vector towards the point the character is moving and the vector which defines the direction the character is looking. This can be done as described here:

Maths - angle between vectors - Martin Baker



Of course you have to check the part for 3d vectors.



There may be some built in features in JME to help you with this, but I'm a beginner myself in JME, so I don't know.

yeah, that seams to be it. Apperantly you have to make sure that the direction you want to be the forward direction of the character is in the direction of the z-axis.



This is because this lookAt function will calculate the direction vector between your object and the point it should look at, then it will call a function which "computes the rotation to transform the z-axis to point into 'direction' and the y-axis to 'up'." in the local coordinatesystem for the object.

Yeah, thanks KevinK this is the solution: Spatial.lookAt(position, upVector)



Thanks to dj_danne too for the math support :wink: