How can I calculate the rotation value of a model to look at a position?

If you can only turn left or right to get there… then the target is either “turn to the left” or “turn to the right”.

How would you calculate that with a y angle?

With a couple of dot products it’s simple.

Given: Spatial target, Spatial player… we want to figure out if player should turn left or right:

// Find the direction we want
Vector3f relativeDir = target.getWorldTranslation().subtract(player.getWorldTranslation()).normalizeLocal();

// Find the direction we're looking and the left vector
Vector3f fwdDir = player.getWorldTranslation().mult(Vector3f.UNIT_Z);
Vector3f leftDir = player.getWorldTranslation().mult(Vector3f.UNIT_X):

// Is it in front of us or behind us and how much?
float front = fwd.dot(relativeDir);

// Is it to the left or right and how much?
float left = leftDir.dot(relativeDir);
// left < 0, target is to the right, left > 0 target is to the left

// If it's behind us then just max out left or right
if( front < 0 ) {
    left = FastMath.sign(left);
    // Except if it's _exactly_ behind us the just pick left or right
    if( left == 0 ) {
        left = 1;
    }
}

// Now turn left/right scaled by the left value -1 = max right turn, 1 = max left turn... 0.5 = half speed left turn, etc.
1 Like