Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine whether it's faster to face an object rotating clockwise or counter clockwise?

I've been trying this to no avail for some days now, but basically I have some creatures and the player on the screen. What I want to happen is for the enemies to turn to face the player at a variable speed, rather than 'lock' into position and face the player immediately.

What I am trying to do is work out whether it is faster for a given enemy to rotate clockwise or counter clockwise to face the player, but it's proving to be beyond my capabilities with trigonometry.

Example:

enter image description here

x in these figures represents the 'shorter' path and the direction I want to rotate in each situation.

What is the simplest way to work out either 'clockwise' or 'counter-clockwise' in this situation, using any of the following:

  • The direction the enemy is facing.
  • The angle between the enemy to the player, and player to the enemy.
like image 915
Marty Avatar asked Jan 15 '23 06:01

Marty


1 Answers

There is no need to calculate angles or use trigonometric functions here, assuming you have a direction vector.

var pos_x, pos_y, dir_x, dir_y, target_x, target_y;
if ((pos_x - target_x) * dir_y > (pos_y - target_y) * dir_x) {
    // Target lies clockwise
} else {
    // Target lies anticlockwise
}

This simply draws an imaginary line through the object in the direction it's facing, and figures out which side of that line the target is on. This is basic linear algebra, so you should not need to use sin() or cos() etc. anywhere in this function, unless you need to calculate the direction vector from the angle.

This also uses a right-handed coordinate system, it will be backwards if you are using a left-handed coordinate system -- the formulas will be the same, but "clockwise" and "anticlockwise" will be swapped.

Deeper explanation: The function computes the outer product of the forward vector (dir_x, dir_y) and the vector to the target, (target_x - pos_x, target_y - pos_y). The resulting outer product is a pseudoscalar which is positive or negative, depending on whether the target is clockwise or anticlockwise.

Crash course on vectors

A vector is a magnitude and direction, e.g., 3 km north, or 6 centimeters down. You can represent a vector using cartesian coordinates (x, y), or you can represent it using polar coordinates (r,θ). Both representations give you the same vectors, but they use different numbers and different formulas. In general, you should stick with cartesian coordinates instead of polar coordinates. If you're writing a game, polar coordinates suck royally — they litter your code with sin() and cos() everywhere.

The code has three vectors in it:

  • The vector (pos_x, pos_y) is the position of the object, relative to the origin.

  • The vector (target_x, target_y) is the position of the target, relative to the origin.

  • The vector (dir_x, dir_y) is the direction that the object is facing.

like image 126
Dietrich Epp Avatar answered May 10 '23 20:05

Dietrich Epp