1

我想直接去地图的中心。所以这意味着不要转向地图高度的中心,然后是地图宽度的中心,我想开始并面向中心然后去那里。一旦我在那里,我想停下来。我觉得我应该做的,但它不能正常工作。

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);
        }
    }
}
4

2 回答 2

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);
于 2013-09-15T16:29:33.633 回答
0

如果有人需要这个:

private void moveTowardsCenter() {
    double centerAngle = Math.atan2(getBattleFieldWidth()/2-getX(), getBattleFieldHeight()/2-getY());
    setTurnRightRadians(Utils.normalRelativeAngle(centerAngle - getHeadingRadians()));
    setAhead(100);
}

它是@Adam-burry 解释的,但经过测试和编译:D

于 2016-09-17T20:15:11.017 回答