• ### Popular Now

• 13
• 16
• 27
• 9
• 9

#### Archived

This topic is now archived and is closed to further replies.

# Enemies Need Intelligence

This topic is 6457 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Suppose a 2d bird-eye-view game. Both enemies and players can rotate freely on the plane. I just wanted my enemies to POINT toward the PLAYER but... I''ve go the vector that indicates the direction the enemy is pointing towards. I can compute the distance-vector between the enemy and the player. How could I find out whether the enemy should rotate clockwise or counter-clockwise to reach the player? I mean, which is the fastest method? Hoping you all can understand what I mean... Karmalaa

##### Share on other sites
I think that you can just cross the enemy''s direction vector with the enemy->player vector (in 3D) and then use the direction of the resulting vector to indicate the direction that the enemy should turn.
If the vector is up (in a right-handed coordinate system), then it means that the enemy should turn counter-clockwise -- down means that the enemy should turn clockwise.

...Syzygy

##### Share on other sites
Hello.

Im creating this now, so it may be not so accurate, but here is what I would do :

Given that I have the (x,y) coords of both points, I would take the enemy (the one who is gonna face somewhere) as 0,0 and the player as the relative coord compared to the enemy.
Say the enemy was at (1, 2) and the player at (4,6), so the relative position of the player to the enemy would be (3,4).
Now I would find out the distance between them (er, you said you have that value. Excellent), which would be, in this case, 5.

Now that I have the 3 sides of the square-triangle, I would calculate the angle between the enemy and the player by using some trigonometry (Being A the angle we want) :
sin A = 4/5 (4 is the "vertical" distance and 5 is the direct distance)

So :
A = arcsin (4/5)

Now that we have the angle, and you have already the angle the enemy is facing, just do some math to check which side is better to rotate. Maybe something like (being "A" the enemy-facing angle, "B" the angle between the enemy and the player, "t1" the test-angle 1 and "t2" the test-angle 2):

  t1 = MAX (A, B) - MIN (A, B);t2 = 360 - t1;if (MAX (A, B) == B){if (t1 > t2) *clockwise*else *anti-clockwise*}else{if (t1 > t2) *anti-clockwise*else *clockwise*}

Im not sure if this works (I think so, but not sure ), and it may look a bit confusing, but after you have both angles there are tons of ways of checking which side is easier, I think. Just create your own way.

Well, cya,
-RoTTer

(Fixed a couple typos)

Edited by - RoTTer on July 13, 2000 4:37:18 AM

##### Share on other sites
quote:
Original post by Syzygy

I think that you can just cross the enemy''s direction vector with the enemy->player vector (in 3D) and then use the direction of the resulting vector to indicate the direction that the enemy should turn.
If the vector is up (in a right-handed coordinate system), then it means that the enemy should turn counter-clockwise -- down means that the enemy should turn clockwise.

...Syzygy

POL''s Syzygy?
Heh, nice to see you here .
And keep the good work on POL .

Cya,
-RoTTer