# Rotate character to face at a certain direction

Hi guys. I’m having some trouble and I could really use some advice.

On my scene, the player controls a character in 3rd person view. The player model node is attached to another node called player node and I have added a CharacterControl to the player node. Also, there are enemies in the scene with the same properties as the player (enemy model node - enemy node - character control).

I find that the player is near the enemy using the distance between the PhysicsLocations of the two controls.

Now, the thing I want is this: When the player comes near an enemy, I want the enemy to slowly turn towards the player and stop when he faces at the player’s direction.

Can you give me some advice on how to do this? I have tried several things, but I haven’t managed to get it to work the way I want.

Thanks a lot for your time.

[java]enemy.lookAt(player.getWorldTranslation(), Vector3f.UNIT_Y);[/java] will force him to always look at the location of the player

1 Like

I want the enemy to slowly turn towards the player, not look at him all the time. With lookAt the enemy automatically turns towards the player, which is not what I want.

I have tried messing with view direction, but to no avail.

Any other ideas?

1 Like

ah right. Then you can do the lookAt to get the Quaternion of the rotation, reset the rotation back and slerp between them.

Or if you work out an x and a y angle of rotation needed (xShift and yShift) then you can:

[java]

// Apply calculated rotation

Vector3f up = cam.getUp();

Vector3f left = cam.getLeft();

Vector3f dir = cam.getDirection();

Matrix3f mat = new Matrix3f();

mat.fromAngleNormalAxis(yShift, left);

mat.mult(up, up);

mat.mult(left, left);

mat.mult(dir, dir);

// If we have turned upside down then use a fast spin for X ignoring the angle

if (up.y < 0) {

xShift = FastMath.HALF_PI*tpf;

}

mat.fromAngleNormalAxis(xShift, Vector3f.UNIT_Y);

mat.mult(up, up);

mat.mult(left, left);

mat.mult(dir, dir);

Quaternion q = new Quaternion();

q.fromAxes(left, up, dir);

q.normalizeLocal();

cam.setAxes(q);

[/java]

(Note that you may well have to tweak the upside down behaviour depending on what the rest of your code is doing).

Thanks a lot for the responses.

@wezrule said:
ah right. Then you can do the lookAt to get the Quaternion of the rotation, reset the rotation back and slerp between them.

Could you please explain this in some more detail? I cannot understand what I'm supposed to do...

@zarch
I don't understand what the camera has to do with all of this (since we're just dealing with two nodes with CharacterControls)...
Also, shouldn't I need to calculate a x and a z angle of rotation? How can I do that?
Finally, what's the use of the mat matrix? I have never used Matrix3f before.

Sorry for bothering you with my questions guys, I really don't have any previous experience in this...
@silentsword said:
Thanks a lot for the responses.
Could you please explain this in some more detail? I cannot understand what I'm supposed to do...

[java]Quaternion initialRotation = enemy.getLocalRotation().clone(); //store initial rotation
enemy.lookAt(player.getWorldTranslation(), Vector3f.UNIT_Y); //set enemy to final rotation
Quaternion endRotation = enemy.getLocalRotation().clone(); //store final rotation
enemy.setLocalRotation(initialRotation); //set enemy back to original

//now slerp between initial and final
float total = 0;
public void update(float tpf) {

if(total >= 1) { //enemy is facing the character
return;
}

total += tpf; //adjust this as neccessary, atm this will take 1 second to go from 1 rotation to another
enemy.setLocalRotation(initialRotation.slerp(endRotation, total)); //rotate a little bit each frame
}[/java]
1 Like
@silentsword said:

@zarch
I don't understand what the camera has to do with all of this (since we're just dealing with two nodes with CharacterControls)...
Also, shouldn't I need to calculate a x and a z angle of rotation? How can I do that?
Finally, what's the use of the mat matrix? I have never used Matrix3f before.

Sorry, my bad. I only read the most recent posts not the original and I thought you wanted the camera to turn towards the target.

Matrix3f is a matrix transformation - you can use them to rotate vectors.