Archived

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

Tracking AI

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

Recommended Posts

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

Share on other sites
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.

Share on other sites
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.

Share on other sites
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-

Share on other sites
I''ll try this out. Thanks everyone for the replies!!

MarcusLM

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 16
• 12
• 20
• 12
• 18
• Forum Statistics

• Total Topics
632160
• Total Posts
3004500

×