I completed position04 a little while ago and it's been a while since I've used trig functions. The most time was spent wrapping my head around radians again.
I'm quite proud of the code since it took a lot of diagramming and trial and error. It's a pretty trivial task (moving a robot to the middle of the battlefield), but it still took a while.
I was trying for a while to use the getBearing() function, but this requires an event and I looked up a few items on creating custom events, but gave up and decided to go with the brute math instead.
Something I want to look at later is using the least amount of turning to position the robot to face the center. As it is now, the robot rotates to a heading of 0 degrees before doing anything (this is to normalize the heading to a known heading of 0).
********************************************************************************
package kgl;
import robocode.Robot;
/**
* Position 04 Robot
*
* Move to the center of the playing field, spin around in a circle, and stop.
*
* @class ICS 413
* @author Kevin Leong
* @date 9/2/10
*/
public class Position04 extends Robot{
public void run(){
//Get battlefield dimensions
double width = getBattleFieldWidth();
double height = getBattleFieldHeight();
//Assign coordinates for center and starting position 'current'
Position center = new Position(width/2, height/2);
Position current = new Position(getX(), getY());
//Get starting heading
double currentHeading = getHeading();
//Calculate the Distance and angle from the starting point to the center of the
//battlefield
DistanceAngle ad = current.angleDistanceBetweenTwoPoints(center);
//Align to 0 degrees
turnRight(360-currentHeading);
//Turn the robot to face toward the center
turnRight(ad.angle);
//Move Robot ahead the correct distance
ahead(ad.distance);
//Spin - We're Done!
turnRight(360);
}
}
/**
* Position Class to store position of center and starting position
* as coordinates. Also calculates:
*
* - Distance between two points
* - Angle between two points in degrees
*/
class Position{
double x;
double y;
public Position(double x, double y){
this.x = x;
this.y = y;
}
DistanceAngle angleDistanceBetweenTwoPoints(Position p){
double a = this.x - p.x;
double b = this.y - p.y;
//Calculate distance using Pythagorean Theorem
double distance = Math.sqrt(Math.pow(a, 2) + Math.pow(b,2));
//Angle variables for converting angle to degrees
double angleRadians, angleDegrees;
//Get angle in radians to center
angleRadians = Math.atan(a/b);
System.out.println("AngleRadians: " + angleRadians);
//Convert radians to degrees. This represents angle
//if robot starts in quadrant III or IV (below the center of the battlefield)
angleDegrees = 180 * angleRadians/Math.PI;
//If robot starts in quadrant I or II
if(b > 0){
angleDegrees = (180 - Math.abs(angleDegrees));
//If we are in quadrant I
if(a > 0){
angleDegrees *= -1;
}
}
return new DistanceAngle(distance, angleDegrees);
}
}
/**
* Object to hold calculations on Distance and Angle
* Calculated in the Position Class
*/
class DistanceAngle{
double distance;
double angle;
public DistanceAngle(double distance, double angle){
this.distance = distance;
this.angle = angle;
}
}
No comments:
Post a Comment