[Home]UtilityClass

Robo Home | Changes | Preferences | AllPages

Pretty much every bot utilizes a set of utility functions, often compiling them in a Utility or BotMath class. There are several examples on the wiki of posted utility classes, but this pages attempts to consolidate all of these utility functions onto one page.

(Subpage each function. Within subpage provide as many examples as possible, with goal to find both a most stable function and a minimum codesize function)


What is the point of normalizing within [0, 2pi]? I don't see how it makes angles more useful. -- Jonathan

Its usually used for calculating the absolute heading of the enemy. You use normalHeading(e.getBearing() + getHeading()) and it will return the exact angle from the norm of the enemy, and then that can be used to calculate its position. In practice I have very rarely used it, but because it is such a standard in most utility classes I added it here (its even included in the Robocode utilities class). -- Jokester

But when do you need to get an angle within [0, 2pi]? -- Jonathan

Note - the reason the robocode utilities class has it is because it needs to be sure to return angles in that range to the robot sometimes. -- Kawigi

Note - I'd have used [-pi, pi] and the mathematical convention 0 rad=x, pi/2 rad=y.

How about the following?

-- Jonathan

Read what I had to say up there for Angles. Distance is basically pythagoras theorem (a^2+b^2=c^2) you do something like this: Math.sqrt(dX*dX+dY*Y). -- AvihooI

Well what do you guys use? I havent put everything here to use, I just started listing things that are in my BotMath class (which has been compiled from most of the classes I have seen). These have just been the basic ones that I have seen every where. I plan on including some more of them. I also have a ton of game physics stuff in my botmath if anyone thinks that should be included (I can make calls to bot math to to see the maximum speed, turning etc of my or other bots). Include whichever you can think of (just list the functions if you want). I chose to do it this way because they are a page per function, but if you feel it would be better to combine similar functions on one page I can do that too. -- Jokester

Normalizing angles is something you just need if you want your bot to make sense. Here's the method I mentioned:

double normalize(double angle, double min, double max) {
	return angle - (max - min) * Math.floor((angle - min) / (max - min));
}
Try normalize(angle, -Math.PI, Math.PI), which is equivalent to:
double normalize(double angle) {
	return angle - 2 * Math.PI * Math.floor((angle + Math.PI) / (2 * Math.PI));
}
PHNRD uses it to stay perpendicular to bullets.

If you have a vector class, it makes sense to put at least cartesian<->polar conversion in there. -- Jonathan

Here's a Vector2D class that I wrote, feel free to use it (AvihooI):

/*For this class, sin and cos are inversed because robocode requires it
 *Also, the angles are in Radians */

public class Vector2D
{
	double x,y; //Vector coordinates
	
	//Cartesian Representation
	public double getX()
	{
		return x;
	}
	
	public double getY()
	{
		return y;
	}
	
	public void setX(double sX)
	{
		x = sX;
	}
	
	public void setY(double sY)
	{
		y = sY;
	}
	
	//Polar representation
	public double getMagnitude()
	{
		return Math.sqrt(Math.pow(x,2) + Math.pow(y,2));
	}
	
	public double getDirection()
	{
		return Math.atan2(x,y);
	}
	
	public void setMagnitude(double sMagnitude)
	{
		double theta  = getDirection();
		x = sMagnitude * Math.sin(theta);
		y = sMagnitude * Math.cos(theta);
	}
	
	public void setDirection(double sDirection)
	{
		double magnitude = getMagnitude();
		x = magnitude * Math.sin(sDirection);
		y = magnitude * Math.cos(sDirection);
	}
	
	//Vectorial manipulations
	
	public Vector2D Normalise()
	{
		double theta  = getDirection();
		return new Vector2D(Math.sin(theta), Math.cos(theta));
	}
	
	public Vector2D Add(Vector2D sVector2D)
	{
		return new Vector2D(x + sVector2D.x, y + sVector2D.y);
	}
	
	public Vector2D Substract(Vector2D sVector2D)
	{
		return new Vector2D( x- sVector2D.x, y - sVector2D.y);
	}
	
	public Vector2D Inverse()
	{
		return new Vector2D(-x,-y);
	}
	
	public double Dot(Vector2D sVector2D)
	{
		return Math.sqrt(x * sVector2D.x + y * sVector2D.y);
	}
	
	public Vector2D Multiply(double sScalar)
	{
		return new Vector2D(x * sScalar, y * sScalar);
	}
	
	public Vector2D(double sX, double sY)
	{
		x = sX;
		y = sY;
	}
	
	public Point2D.Double translateToPoint()
	{
		return new Point2D.Double(x,y);
	}
	
	public Point2D.Double translateToPoint(Point2D.Double source)
	{
		return new Point2D.Double(source.x + x, source.y + y);
	}
	
}

Robo Home | Changes | Preferences | AllPages
Edit text of this page | View other revisions
Last edited March 11, 2006 14:45 EST by AvihooI (diff)
Search: