Problem with rotating towards something
Oct 31, 2015 at 11:34pm UTC
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;
Nov 1, 2015 at 12:25am UTC
.
Last edited on Nov 1, 2015 at 7:41am UTC
Nov 1, 2015 at 12:35am UTC
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;
Nov 1, 2015 at 1:38am UTC
.
Last edited on Nov 1, 2015 at 7:41am UTC
Nov 1, 2015 at 2:46am UTC
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;
Nov 1, 2015 at 11:59pm UTC
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.