algorithm - Getting the computer to realise 360 degrees = 0 degrees, rotating a gun turret -


I am creating a game and it can rotate a turret gun gun 360 degree in a computer controlled gun turret.

This gun (objdeg) uses trig to detect the need for the angle and angle of the gun is stored in the current angle (gundeg)

The following code sets one Will rotate the gun at the speed of

  if (objdeg> gundeg) {gundeg ++; } If (objdeg  

The problem is that if there is an object at 10 degrees, the gun rotates, shoots it and destroys it, if another target appears at 320 degrees , Then the gun will rotate 310 degrees digit

How can I fix my code so that it is not stupid?

If you need to rotate more than 180 degrees in one direction to target the turret It will be faster to rotate the other direction.

I'll check for just this and then to rotate in the right direction

  if (objdeg! = Gundeg) {if ((gundeg - objdeg)> 180) Gundeg ++; And Gundag -; }  

Edit: New Solution

I have refined my solution based on feedback in comments. This determines whether the target is for the left or right of the turret or not, and decides how to turn. If this goal is more than 180 degrees away, then it is unchanged in this direction.

  if (objdeg! = Gundeg) {int change = 0; Int diff = gundeg - objdeg; If (difference <0) change = 1; Other changes = -1; If (Math.Abase (difference)> 180) change = 0 - change; Gundeg + = change; }  

Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -