• Create Account

### #ActualPunCrathod

Posted 07 August 2012 - 02:45 PM

I'll assume you are using c# and XNA. You should use Math.atan2(x2-x1,y2-y1) instead of atan(dx/dy) so you don't have to worry about special case dy=0 and don't have to find wich side the target is. Also what I like to use in cases like these I like to use the following code.
targetangle=Math.Atan2(target.x-this.x,target.y-this.y)//get the angle to the target

turnangle=targetangle-currentangle;//get the difference between the current angle and the target angle

if(turnangle>2*Math.PI)//make sure to wrap the angle between 0 and 2PI
turnangle-=2*Math.PI
if(turnangle<0)
turnagnel+=2*Math.PI

turnright();
}
{
turnleft();
}


This should always turn to the direction that is closer to the target. Deadzone is the amount of radians(you get radians from degrees with degrees/360*(2*PI)) the seeker can be off course before it starts to correct its course and should always be between 0 and PI radians(0 and 180 degrees) for this to work correctly.
If there are any errors in the code or elsewhere I blame the late hours.

### #1PunCrathod

Posted 07 August 2012 - 02:32 PM

I'll assume you are using c# and XNA. You should use Math.atan2(x2-x1,y2-y1) instead of atan(dx/dy) so you don't have to worry about special case dy=0 and don't have to find wich side the target is. Also what I like to use in cases like these I like to use the following code.
[source lang="csharp"]targetangle=Math.Atan2(target.x-this.x,target.y-this.y)//get the angle to the targetturnangle=targetangle-currentangle;//get the difference between the current angle and the target angleif(turnangle>2*Math.PI)//make sure to wrap the angle between 0 and 2PI turnangle-=2*Math.PIif(turnangle<0) turnagnel+=2*Math.PIif(turnangle<Math.PI&&turnangle>Deadzone){//Check wich side you should turn to get to your target. turnright();}else if(turnangle>Math.PI&&turnangle<(2*Math.PI)-Deadzone){ turnleft();}[/source]
This should always turn to the direction that is closer to the target. Deadzone is the amount of radians(you get radians from degrees with degrees/360*(2*PI)) the seeker can be off course before it starts to correct its course and should always be between 0 and PI radians(0 and 180 degrees) for this to work correctly.
If there are any errors in the code or elsewhere I blame the late hours.

PARTNERS