Robocode Responsive Turning

241 Views Asked by At

I'm trying to make a robot for robocode that always turns perpendicular when it sees a robot.

If you don't know what robocode is just imagine a top down game where direction is recorded in degrees

I've already made an equation that does what I want but it is very inefficient

(e.getHeading is the direction the target is facing, getHeading is the direction I'm facing note that also there is a method getBearing that shows angle relative to the enemy)

turnRight( (e.getHeading() + 90) - getHeading());

The problem though is that it will sometimes go the long way around rather than the shortest route. What better equation could I use to always turn the right way?

2

There are 2 best solutions below

0
On

You would have to normalize the result of

degdiff = (e.getHeading() + 90) - getHeading()

so that it is in the range -180..180. This is either

if(degdiff >  180) degdiff -=360;
if(degdiff < -180) degdiff +=360;

or

degdiff = (degdiff%360 + 540)%360 -180

and then

turnRight(degdiff)
0
On

You can use

setTurnRightRadians(normalRelativeAngle(e.getBearingRadians() + Math.PI / 2))

e.getHeading() is enemy's heading.