Make an Entity in 3D face a specific angle?

Okay, so right now, my Entities have both a position and a rotation vector. I have a follow(Player) function, but as you may have imagined, the object is facing the same direction while it’s chasing the player. Ultimately, I would like the entity to face the player as he chases him. Here’s what I have in mind:


		double theta = Math.tan(dz / dx), angle = this.getRotY() + theta;
                //dz and dx are the sides of the triangle that I use for distance
			
			if (angle == 0)
				increaseRotation(0, 0, 0);
			else if (angle > 180) 
				increaseRotation(0, (float) (-angle), 0);
			else
				increaseRotation(0, (float) (angle), 0);

Any ideas? Sorry if this is super simple, but I’m having a tough time thinking this through (very tired, at the moment).

At least, your code should look like this:


double theta = Math.atan2(dz, dx), angle = this.getRotY() + Math.toDegrees(theta);
 //dz and dx are the sides of the triangle that I use for distance
			
if (angle == 0)
	increaseRotation(0, 0, 0);
else if (angle > 180) 
        increaseRotation(0, (float) (-angle), 0);
else
        increaseRotation(0, (float) (angle), 0);

Thanks for the reply, but the entity still spazzes out.

Please, post some more code.

Well, that’s all the code there is to it. I added a time constant to the angle, and it fixed the spazzing, but it looks at the player it’s following from the right or left side of the model instead of directly at it (if that makes sense). So, I modified my code to the following:


if (angle == 0)
    increaseRotation(0, 0, 0);
else
    increaseRotation(0, (float) ((-angle + 90) * DisplayManager.getFrameTimeSeconds() * 2), 0);

The only issue I have now is that it is looks at the player directly but when it is facing from 0 <= x <= 180, it stares at the player from the back part of the model.

I know I’m dealing with the discontinuity of atan function, but is there any way to bypass it? For example, once it hits 180 degrees, it just keeps going to make a full circle?

I have found the similar case. Hope it will help.

http://www.java-gaming.org/index.php?topic=21947.0

I’ll let you know.

Yeah, it didn’t completely fix it, but I did alter my code some. The following entity currently only turns once (when passing the angle 360). Here is the altered code:


double theta = Math.atan2(dz, dx), angle = this.getRotY() + Math.toDegrees(theta);

if (angle == 0)
	increaseRotation(0, 0, 0);
else 
	increaseRotation(0, (float) ((-angle + 90) * DisplayManager.getFrameTimeSeconds() * 5), 0);

Still haven’t fixed the problem. I got the enemy that’s following to make a complete circle perfectly fine, but after that complete circle, the enemy flips due to the tangent function. Any ideas? Here is the full method:


		float xPos = player.getPosition().x, zPos = player.getPosition().z;
		float minXPos = position.x, minZPos = position.z;

		float dx = xPos - minXPos, dz = zPos - minZPos;
		double distance = Math.sqrt(dx * dx + dz * dz);
		double theta = Math.toDegrees(Math.atan2(dz, dx)), angle = this.getRotY() + theta;
		
		if (angle < 0)
			angle += 360;

		if (distance < getRange()) {
			increasePosition((float) (DisplayManager.getFrameTimeSeconds() * walkSpeed * (dx / distance)), 0, 
					(float) (DisplayManager.getFrameTimeSeconds() * walkSpeed * (dz / distance)));

		increaseRotation(0, (float) ((-angle + 90) * DisplayManager.getFrameTimeSeconds() * 5), 0);