我正试图直接去地图的中心。所以这意味着没有转向地图高度的中心,然后是地图宽度的中心,我想开始并面向中心然后去那里。一旦我在那里,我想停下来。我觉得我应该这样做,但它不能正常工作。
1)我是否走在正确的轨道上?
2)是否有更直接的方式去中心?就像拼出坐标的方法一样?
到目前为止,我有:
public void run() {
// Initialization of the robot should be put here
setColors(Color.black,Color.blue,Color.orange, Color.orange, Color.cyan); // body,gun,radar
double xMiddle = getBattleFieldWidth() / 2;
double yMiddle = getBattleFieldHeight() / 2;
double directionToTurn = 0;
// Robot main loop
while(true) {
if( (int)getX() != xMiddle && (int)getY() != yMiddle ){
if ( (int)getX() > xMiddle && (int)getY() > yMiddle ) { //Quadrant 1
directionToTurn = (int)getHeading() - 225;
System.out.println("Quadrant: ONE ");
} else if ( (int)getX() < xMiddle && (int)getY() > yMiddle ) { //Quadrant 2
directionToTurn = (int)getHeading() - 135;
System.out.println("Quadrant: TWO ");
} else if ( (int)getX() < xMiddle && (int)getY() < yMiddle ) { //Quadrant 3
directionToTurn = (int)getHeading() - 45;
System.out.println("Quadrant: THREE ");
} else if ( (int)getX() > xMiddle && (int)getY() < yMiddle ) { //Quadrant 4
directionToTurn = (int)getHeading() - 315;
System.out.println("Quadrant: FOUR ");
} else if ( (int)getX() > xMiddle && (int)getY() == yMiddle ) { // Right Center
directionToTurn = (int)getHeading() - 270;
System.out.println("Quadrant: Right Center ");
} else if ( (int)getX() < xMiddle && (int)getY() == yMiddle ) { // Left Center
directionToTurn = (int)getHeading() - 90;
System.out.println("Quadrant: Left Center ");
} else if ( (int)getX() == xMiddle && (int)getY() > yMiddle ) { // Top Center
directionToTurn = (int)getHeading() - 180;
System.out.println("Quadrant: Top Center ");
} else if ( (int)getX() == xMiddle && (int)getY() < yMiddle ) { // Bottom Center
directionToTurn = (int)getHeading() - 0;
System.out.println("Quadrant: Bottom Center ");
}
turnLeft( directionToTurn );
System.out.println("Position: ("+(int)(getX())+", "+(int)getY()+"), Facing: "+(int)getHeading() );
ahead(1);
}
}
}
答案 0 :(得分:2)
你想要计算的是一个方位。轴承是您的航向(您所面向的方向)与目标方向之间的差异。如果你转动轴承的数量,那么你将面向目标。
自从我玩Robocode以来已经很长时间了,所以我不记得确切的API了。以下是草图(因此,未经测试)。在编译之前,您必须修复函数名称和类型。
// heading (or course) [0, 2pi): 0 is up, rotation is clock-wise
// bearing (-pi, pi]: positive is clock-wise
// convert angle to (-pi, pi]
double norm(double a) {
// do something smarter with modulus (%) here
while (a <= -Math.PI) a += 2 * Math.PI;
while (Math.PI < a) a -= 2 * Math.PI;
return a;
}
// get bearing in radians to a target(x,y) from the current position/heading
double getBearing(int x, int y) {
// can rotate the coordinate system to avoid the addition of pi/2 if you like
double b = Math.PI/2 - Math.atan2(y - robot.y(), x - robot.x());
return norm(b - robot.getHeadingRadians());
}
// then bearing to centre is
double bearingToCentre = getBearing(xMiddle, yMiddle);
答案 1 :(得分:0)
如果有人需要这个:
private void moveTowardsCenter() {
double centerAngle = Math.atan2(getBattleFieldWidth()/2-getX(), getBattleFieldHeight()/2-getY());
setTurnRightRadians(Utils.normalRelativeAngle(centerAngle - getHeadingRadians()));
setAhead(100);
}
@ Adam-burry的解释,但测试和编译:D