How do you know if it's faster to look at an object rotating clockwise or counterclockwise?

I've tried this to no avail for days, but basically I have some creatures and a player on the screen. I want the enemies to turn to face the player at variable speed instead of "blocking" the position and immediately in front of the player.

What I am trying to do is figure out if the enemy is faster to rotate clockwise or counterclockwise to counter the player, but it turned out to be above my ability with trigonometry.

Example:

enter image description here

x

these figures show the "shorter" path and the direction I want to rotate in each situation.

What is the simplest way to work out "clockwise" or "counterclockwise" in this situation, using any of the following:

  • The direction the enemy faces.
  • The angle between the opponent to the player and the player to the enemy.
+3


source to share


2 answers


no need to calculate angles or use trigonometric functions here if 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
}

      

It simply draws an imaginary line across the object in the direction it faces and determines which side of that line the target is on. This is basic linear algebra, so you don't need to use sin()

or cos()

etc. Anywhere in this function, unless you need to compute the direction vector from the angle.

This also uses a right-handed coordinate system, it will be reversed if you use a left-handed coordinate system - the formulas will be the same, but clockwise and counterclockwise will be replaced.

A deeper explanation: The function calculates the outer product of the vector forward (dir_x, dir_y)

and the vector to the target (target_x - pos_x, target_y - pos_y)

. The resulting external product is a pseudoscalar, which is positive or negative, depending on whether the target is clockwise or counterclockwise.



Vector crash rate

A vector is a magnitude and direction , such as 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 put your code into everything sin()

and cos()

.

There are three vectors in the code:

  • 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 the object is facing.

+7


source


const CLOCKWISE:int = 0;
const COUNTER_CLOCKWISE:int = 1;
const PI2:Number = Math.PI * 2


function determineSmallestAngle(from:Sprite, to:Sprite):int
{
    var a1:Number = Math.atan2(to.y - from.y, to.x - from.x);
    var a2:Number = from.rotation * Math.PI / 180;

    a2 -= Math.floor(a2 / PI2) * PI2;

    if(a2 > Math.PI) a2 -= PI2;

    a2 -= a1;

    if (a2 > Math.PI) a2 -= PI2;
    if (a2 < -1 * Math.PI) a2 += PI2;

    if (a2 > 0) return CLOCKWISE;
    return COUNTER_CLOCKWISE;

}

      



+1


source







All Articles