0
\$\begingroup\$

For a game I'm making there is an AI bot in it. I want it to rotate so that it is facing it's opponent. The direction it is facing is determined by a vector (it will face the direction that the vector represents.) I can rotate it to face it's opponent like so:

 glm::vec3 opponentRefPos = opponentPos - position;      //for comparing AI's opponent's position to AI's position

glm::vec3 directionTarget = glm::normalize(opponentRefPos);
direction = directionTarget;

Where direction is the direction that the AI bot is facing (and it is a vec3) I want to gradually rotate it to face directionTarget. So let's say that I have a constant variable called SPEED (and deltaTime, which is the time between frames that SPEED will be multiplied by to keep the speed that it rotates at constant), which is the speed that I want the AI bot to rotate at. How can I rotate it towards this vector at that speed?

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Here it is how I did it. I store the angle of my enemy in a variable m_Angle. Then you update every tick (elapsedTime) the target angle towards your opponent. Please keep in mind that you need to switch direction at +/- 180, so turn right or left (I assume your opponent moves as well). The general approach is to turn the angle of the sprite only partly based on your m_Turnspeed instead of directly letting your sprite facing your opponent. As long as the target angle is not the actual angle you add a fraction to the angle towards your opponent.

Start with initializing m_Angle, e.g.45.0f. And here is the update function for every tick:

// Buffer the direct angle from your bot to the opponent
float targetAngle = ((std::atan2(opponentLocation->y - m_Position.y, opponentLocation->x - m_Position.x) * 180) / 3.141);

// Calculate the difference
float diff = targetAngle - m_Angle;

// Make sure you turn less than 180 degree
while (diff < -180){
    diff += 360;
}
while (diff > 180){
    diff -= 360;
}

// Based on the difference, turn left or right. 
// Wrote it without trying, probably need to switch statements
if (diff > 0){
    m_Angle += m_Turnspeed * elapsedTime;
} else if (diff < 0) {
    m_Angle -= m_Turnspeed * elapsedTime;
}

My enemies are tanks. So I consider their speed in the calcualtion. I do not know if this is helpful to you as well, but take this is a hint. The faster the enemy moves the slower it turns.

m_Angle += m_Turnspeed * (1 - 0.5f * (m_Speed/m_Maxspeed)) * elapsedTime;
\$\endgroup\$
2
  • \$\begingroup\$ Could you explain how you get m_Angle? With the only way I know how to get the angle between vectors (angle between position and the direction of opponentPos - position) I always get an angle between 0 and PI radians (or 0-180 degrees) so with my method to get the angle this would not properly work. \$\endgroup\$ Commented May 25, 2017 at 22:06
  • \$\begingroup\$ I am currently away from my computer. I will edit my answer accordingly. m_Angle is just a placeholder. Initialize with whatever you want, eg. 45. The trick is that m_Angle slowly increases (or decreases) to your target vector towards your opponent. The target vector is calculated each tick and points towards your opponent \$\endgroup\$ Commented May 26, 2017 at 6:25
0
\$\begingroup\$

Seems you have the current direction and the desired direction figured out. The difference between these vectors is the steering vector.

After normalizing the steering vector you can multiply it by your desired speed. Add this result to the current direction... and you're done. Make sure the result has the same length as your initial vector and you're set.

\$\endgroup\$
1
  • \$\begingroup\$ What if your desired direction is 180 degrees from your current direction? Then the difference between them is a zero vector. More generally, this will actually turn at different speeds, because the steering vector isn't always tuning the current direction along its perpendicular - sometimes it has a parallel component that gets eaten by the length-correction step. \$\endgroup\$
    – DMGregory
    Commented Nov 6, 2017 at 17:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .