0

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?

Micho
  • 3,929
  • 13
  • 37
  • 40
Hash
  • 39
  • 4

2 Answers2

0

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)
Lutz Lehmann
  • 25,219
  • 2
  • 22
  • 51
0

You can use

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

e.getHeading() is enemy's heading.

timbre timbre
  • 12,648
  • 10
  • 46
  • 77