c++rotationgame-ai

C++ AI Rotation Issue


For a game I'm making, I want 3 ships which will all race around the map following a collection of points. It works perfectly fine, except for one point in the map, where the ships decide to rotate almost 360 degrees counter clockwise even though only 10 degrees clockwise should be enough.

The code for calculating the rotation:

vec2 distance = *desiredPosition - position;
float rot = atan2(distance.y, distance.x);
rot = rot * 180.f / PI + 90.f;

if (rot < angle)
{
    angle -= dAngle;
    boat->RotateImage(-dAngle);
}
if (rot > angle)
{
    angle += dAngle;
    boat->RotateImage(dAngle);
}

velocity += vec2(acceleration * cos((angle - 90) * PI / 180.0), acceleration * sin((angle - 90) * PI / 180.0));

How do I ensure it won't rotate in the wrong direction there?


Thanks to Richard Byron (accepted answer below), the problem is fixed. Taking the dot product is better than using degrees.

The final code:

vec2 distance = desiredPosition - position;
normal = vec2(sin((angle - 90) * PI / 180.0), cos((angle - 90) * PI / 180.0) * -1);
float dir = normal.x * distance.x + normal.y * distance.y;

//turn
if (dir > 0)
{
    angle -= dAngle;
    boat->RotateImage(-dAngle);
}
if (dir < 0)
{
    angle += dAngle;
    boat->RotateImage(dAngle);
}

velocity += vec2(acceleration * cos((angle - 90) * PI / 180.0), acceleration * sin((angle - 90) * PI / 180.0));

Solution

  • There are a couple of problems with your updated code. Firstly, it should be rot2 = 360 - rot1; (rot1 + 360 is exactly the same angle as rot1).

    The second issue is that you are not taking into account that 1 and 359 degrees are almost the same angle. So if abs(rot1 - angle) > 180, then you really want to use 360 - abs(rot1 - angle) in that case. Your later comparisons with rot and angle are a problem for the same reason, and you need to handle angle incrementing above 360 and decrementing below 0.

    I could write out code for this, but there's actually a much simpler and faster way to do this. If you take the dot product of the vector (desiredPosition - position) and a vector at right angles to the ships current heading, then you can turn based on the sign of that result. If it's not clear how to do this, let me know and I can expand on it in the comments.