Skip to main content
added 758 characters in body
Source Link
Martin Sand
  • 123
  • 1
  • 1
  • 7

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;

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.

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;

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;
Source Link
Martin Sand
  • 123
  • 1
  • 1
  • 7

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.

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;