[Home]Movement/GoToTester

Robo Home | Movement | Changes | Preferences | AllPages

package wiki;
import robocode.*;
import java.awt.geom.Point2D;

public class GoToBot extends AdvancedRobot {
   
	Point2D NextPosition;
	double Fieldx, Fieldy;
    public void run() {
		Fieldx=getBattleFieldWidth();
		Fieldy=getBattleFieldHeight();
		while (true) { 
			NextPosition=new Point2D.Double(Math.random()*(Fieldx-36)+18,Math.random()*(Fieldy-36)+18);
			out.println("Going to:"+NextPosition.getX()+", "+NextPosition.getY());
			goTo(NextPosition);
			execute();
			while (getDistanceRemaining()>0 || getTurnRemaining()>0) {execute();}
			out.println("Got to: "+getX()+", "+getY());
		}

    }

    private void goTo(Point2D destination) {
        Point2D location = new Point2D.Double(getX(), getY());
        double distance = location.distance(destination);
        double angle = normalRelativeAngle(absoluteBearing(location, destination) - getHeading());
        if (Math.abs(angle) > 90) {
            distance *= -1;
            if (angle > 0) {
                angle -= 180;
            }
            else {
                angle += 180;
            }
        }
        setTurnRight(angle);
        setAhead(distance);
    }

    private double absoluteBearing(Point2D source, Point2D target) {
        return Math.toDegrees(Math.atan2(target.getX() - source.getX(), target.getY() - source.getY()));
    }

    private double normalRelativeAngle(double angle) {
        angle = Math.toRadians(angle);
        return Math.toDegrees(Math.atan2(Math.sin(angle), Math.cos(angle))); 
    }
}

Robo Home | Movement | Changes | Preferences | AllPages
Edit text of this page | View other revisions
Last edited August 29, 2003 23:36 EST by Tango (diff)
Search: