• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Tracking AI

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1MarcusLM  Members

Posted 22 June 2001 - 05:33 AM

I am creating a simple 2D game using OpenGL. I want to create turrets that track and fire at the player. I have figured out how to determine the angle the turret needs to rotate to but I can''t seem to figur out which direction (to the turrets left or roght) that the turret shoulf roatate in to best arrive at the desired angle. If I just assume always rotate to one direction or the other then sometimes the turret takes the long way around, depending on where the player is. Can someone please help me figure out how to determine which way the turret should rotate. I know the exact position of the player,the facing direction of the turret, and the angle the turret should face to be facing the player. Any help appreciated. Thanks. Marcus

### #2Seaman  Members

Posted 22 June 2001 - 10:32 AM

Ok I have no idea where this comes from or exactly how it needs to be implemented, but I''m pretty sure if you take the sign of the dotproduct of you turret forward and a vector from turret to player, you can use this to decided wether to rotate clockwise or counter clockwise around your axis, to get the shortest rotation.
Hope this helps in some way.

### #3TerranFury  Members

Posted 27 June 2001 - 10:11 AM

If the angle the gun needs to rotate to is greater than the angle of the gun, rotate clockwise. If it''s less, rotate counter-clockwise. (Or it might be the other way around).

That should do it.

### #4uncutno  Members

Posted 27 June 2001 - 10:44 AM

The broblem is that the turret rotetes more then 180 degrees, witch is the longest to rotate, if more the other way is better!

dif_angle = target_angle - facing_angle;

if dif_angle is greater then zero, and greater then 180
facing_angle -= rotation_speed

if dif_angle is greater then zero, and lesser then 180
facing_ += rotation_speed

if dif_angle is lesser then zero, and lesser then -180
facing_ += rotation_speed

if dif_angle is lesser then zero and greater then -180
facing_ -= rotation_speed

This should solve the rotate_the shortest way problem.
The 180 must be replased with PI if you use radians!

-Anders-Oredsson-Norway-

### #5MarcusLM  Members

Posted 28 June 2001 - 01:44 AM

I''ll try this out. Thanks everyone for the replies!!

MarcusLM

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.