Problem with rotating towards something

I am trying to have something constantly update itself to point towards the player, but I am having a problem with it. The problem happens whenever the angles are close to each other but one is at 360 and one is at 0. Whenever that happens it rotates all the way around instead of just moving a little. Is there a simple way to fix this? Here is the code I have currently.

1
2
3
4
5
if (enemy.shootDir <= enemy.targetDir)
	enemy.shootDir += 3;
else if (enemy.shootDir > enemy.targetDir)
	enemy.shootDir -= 3;
closed account (48T7M4Gy)
.
Last edited on
What do you mean? I tried this but still have the same problem

1
2
3
4
if (enemy.shootDir - enemy.targetDir <= enemy.targetDir - enemy.shootDir)
			enemy.shootDir += 3;
		else
			enemy.shootDir -= 3;
closed account (48T7M4Gy)
.
Last edited on
I tried this, and it doesn't work at all... I have no idea what you're talking about. Can you show some code to explain it?

1
2
3
4
5
6
7
int difference = enemy.shootDir - enemy.targetDir;

if (enemy.shootDir + 3 - enemy.targetDir > difference)
	enemy.shootDir += 3;
else
	enemy.shootDir -= 3;
Ok I figured it out. If anyone else has this problem I just compared the distances of going clockwise and counterclockwise and chose the closer one.

1
2
3
4
if (GetDistance(enemy.x + 5 * cos((enemy.shootDir + 3) * (PI / 180)), enemy.y + 5 * sin((enemy.shootDir + 3) * (PI / 180)), enemy.x + 5 * cos(enemy.targetDir * (PI / 180)), enemy.y + 5 * sin(enemy.targetDir * (PI / 180))) < GetDistance(enemy.x + 5 * cos(enemy.shootDir * (PI / 180)), enemy.y + 5 * sin(enemy.shootDir * (PI / 180)), enemy.x + 5 * cos(enemy.targetDir * (PI / 180)), enemy.y + 5 * sin(enemy.targetDir * (PI / 180))))
			enemy.shootDir += 3;
		else
			enemy.shootDir -= 3;
Topic archived. No new replies allowed.