🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Tracking AI

Started by
3 comments, last by MarcusLM 23 years ago
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
Advertisement
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.
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.
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-
-Anders-Oredsson-Norway-
I''ll try this out. Thanks everyone for the replies!!

MarcusLM

This topic is closed to new replies.

Advertisement