[Home]RobocodeTrigTutorial

Robo Home | Changes | Preferences | AllPages

I'll give it a quick go. This is assumes you know normal trig before reading. Beware... ;)

The main difference between Robocode trig and normal is where 0 degrees is and the direction of the unit circle.

In normal trig, 0 degrees is at the right edge of the circle and you then traverse counter-clockwise around the unit circle to increase the angle. Robocode places 0 degrees at the top of the circle and you traverse clockwise to increase the angle. Basically, to convert from Polar to Rect (Cartesian) in robocode, you do the following:

X = Math.sin(Angle) * Magnitude of Vector

Y = Math.cos(Angle) * Magnitude of Vector

The Sin / Cos are backwards from normal trig where the Y value would use Sin() and X would use Cos().

Other useful info: To convert from Rect To Polar

Angle = Math.atan2(X, Y);

Magnitude = Point2D.Double.Distance(X, Y, 0, 0);

Note: Point2D is located in java.awt.geom. Also, all trig functions assume Radians, not degrees which is why almost all bots use radians as their default value. Other people: feel free to add more, correct, or place some more info anywhere you want! -- Miked0801

If you want to use degrees, you may find it helpful to define the following methods:

public static double sin(double angle) { return Math.sin(Math.toRadians(angle)); }
public static double cos(double angle) { return Math.cos(Math.toRadians(angle)); }
public static double atan2(double x, double y) { return Math.toDegrees(Math.atan2(x, y)); }
-- Jomel

Can someone explain more about point2D ?? --X-KAM-X

The best way to learn about Point2D is to read the Java API maybe. Anyway Point2D is an interface specifying a number of useful function regarding 2D points. The most often used (in Robocode) instanciating class is Point2D.Double. Search the pages about Gouldingi/Code and GoToBot/Code for "Point2D" and you should see some uses. -- PEZ

OK! I got it. Thanks PEZ ! --X-KAM-X

Err, dont angles work counter clockwise in robocode too? was this a change from the original.

Nope, I'm pretty sure Robocode has used polar trig from the beginning (the idea is that it goes the same way as North-South-East-West in nautical directions - 0 degrees is North, 90 degrees is East, etc. The effect is that instead of adding 90 degrees to all your uses of trig functions, you just swap your sines and cosines. -- Kawigi


Robo Home | Changes | Preferences | AllPages
Edit text of this page | View other revisions
Last edited July 6, 2005 5:15 EST by Kawigi (diff)
Search: