0
votes

I'm implementing a homing bullet that targets the nearest enemy.

I'm able to calculate the angle between the player's forward vector and the vector from the player to the target by doing a dot product. If I put the result to acos(), it will give me the angle between the 2 vectors in terms of radian.

Now I want my object to rotate and face toward the enemy as it is flying. But here comes the problem, how do I know which side to rotate to? I know the angle between the 2 vectors, but I dot producted value doesnt tell me whether or not the target is on the left side or right side of the player.

I'm wondering if I'm taking a longer route, and I might not be aware of a easier way to determine how much and to which side I need to rotate to face the target.

2
I think this would work for you, I just scanned it though: stackoverflow.com/questions/11022446/… - MikeL

2 Answers

0
votes

You can calculate dot product for side vector like this:

vector Side( Front.Y, -Front.X );
float dotSide = dot( Target, Side );
if( dotSide <= 0 )
{
    // one side
}
else
{
    // another side
}
0
votes

If I understand you right, you want to get the direction, in which your spaceship vecS should rotate in order to look at the target vecT.

I would suggest to calculate the angle alpha, which vecS and the x-axis enclose, as well as the angle beta, which vecT and the x-axis enclose. If alpha is greater than beta you would rotate to the right, otherwise to the left.

Here is the pseudocode to calculate the angle and the proper rotation direction:

// Pseudocode:
alpha = arctan( vecS.y / vecS.x );
beta = arctan ( vecT.y / vecS.x );

if(alpha > beta)
  // Rotate right by alpha - beta
else
  // Rotate left by alpha - beta