예제 #1
0
  public void onScannedRobot(ScannedRobotEvent e) {
    robotLocation = new Point2D.Double(getX(), getY());
    enemyAbsoluteBearing = getHeadingRadians() + e.getBearingRadians();
    enemyDistance = e.getDistance();
    enemyLocation = vectorToLocation(enemyAbsoluteBearing, enemyDistance, robotLocation);

    // Change direction at random
    if (Math.random() < 0.015) {
      movementLateralAngle *= -1;
    }
    move();
    execute();

    // radar
    setTurnRadarRightRadians(
        Utils.normalRelativeAngle(enemyAbsoluteBearing - getRadarHeadingRadians()) * 2);

    /*
     * Circular Gun from wiki
     */
    double absBearing = e.getBearingRadians() + getHeadingRadians();

    // Finding the heading and heading change.
    double enemyHeading = e.getHeadingRadians();
    double enemyHeadingChange = enemyHeading - oldEnemyHeading;
    oldEnemyHeading = enemyHeading;

    double deltaTime = 0;
    double predictedX = getX() + e.getDistance() * Math.sin(absBearing);
    double predictedY = getY() + e.getDistance() * Math.cos(absBearing);
    while ((++deltaTime) * BULLET_SPEED
        < Point2D.Double.distance(getX(), getY(), predictedX, predictedY)) {

      // Add the movement we think our enemy will make to our enemy's current X and Y
      predictedX += Math.sin(enemyHeading) * (e.getVelocity());
      predictedY += Math.cos(enemyHeading) * (e.getVelocity());

      // Find our enemy's heading changes.
      enemyHeading += enemyHeadingChange;

      // If our predicted coordinates are outside the walls, put them 18
      // distance units away from the walls as we know
      // that that is the closest they can get to the wall (Bots are
      // non-rotating 36*36 squares).
      predictedX = Math.max(Math.min(predictedX, getBattleFieldWidth() - 18), 18);
      predictedY = Math.max(Math.min(predictedY, getBattleFieldHeight() - 18), 18);
    }
    // Find the bearing of our predicted coordinates from us.
    double aim = Utils.normalAbsoluteAngle(Math.atan2(predictedX - getX(), predictedY - getY()));

    // Aim and fire.
    setTurnGunRightRadians(Utils.normalRelativeAngle(aim - getGunHeadingRadians()));
    setFire(BULLET_POWER);

    setTurnRadarRightRadians(Utils.normalRelativeAngle(absBearing - getRadarHeadingRadians()) * 2);
  }
  public void run() {
    setAllColors(Color.GREEN);
    setTurnRadarRight(Double.POSITIVE_INFINITY);

    double robotX, robotY;
    double robotHeading, angleToGoal, angleToObs;
    double adjustment;
    double obsAngle, obsAdjustment;
    double angleDiff;
    double speedToGoal, speedFromObs;

    Enemy temp;
    obstacles = new HashMap<String, Enemy>(10);

    while (true) {
      if (foundGoal) {
        robotX = getX();
        robotY = getY();
        goalX = obstacles.get(GOAL_NAME).x;
        goalY = obstacles.get(GOAL_NAME).y;

        // Adjust robocode's returned heading so that 0 aligns with the positive x-axis instead of
        // the positive y-axis.
        // Also make it so that positive angle indicates a counter clockwise rotation instead of the
        // clockwise style
        // returned by robocode.
        robotHeading = 360 - (getHeading() - 90);
        angleToGoal = Math.toDegrees(Math.atan2(goalY - robotY, goalX - robotX));
        if (angleToGoal < 0) {
          angleToGoal += 360;
        }

        adjustment = angleToGoal - robotHeading;
        adjustment = normalizeAngle(adjustment);
        speedToGoal = calcRobotSpeedLinear(robotX, robotY, goalX, goalY);

        // Calculate how the robot's heading and speed should be affected by objects that it has
        // located
        // as it explores the world.
        Iterator it = obstacles.entrySet().iterator();
        while (it.hasNext()) {
          System.out.println("Iterating through objects.");

          Map.Entry pairs = (Map.Entry) it.next();

          // If the current item in the Iterator isn't the goal.
          if (!pairs.getKey().equals(GOAL_NAME)) {
            temp = (Enemy) pairs.getValue();
            speedFromObs = calcObjRepulseSpeed(robotX, robotY, temp.x, temp.y);

            // If the robot is in range of the object to be affected by it's repulsion.
            if (speedFromObs != 0) {
              obsAngle = Math.toDegrees(Math.atan2(robotY - temp.y, robotX - temp.x));
              if (obsAngle < 0) obsAngle += 360;

              angleDiff = obsAngle - angleToGoal;
              angleDiff = normalizeAngle(angleDiff);
              adjustment += (angleDiff * (speedFromObs / speedToGoal));
              speedToGoal -= speedFromObs;
            }
          }

          // Was getting a null pointer exception when using this. The internet lied about its
          // usefulness.
          // it.remove(); // avoids a ConcurrentModificationException
        }

        adjustment = normalizeAngle(adjustment);
        setTurnLeft(adjustment);
        // ahead(speedToGoal);
        setAhead(speedToGoal);
      }

      execute();
    }
  }
예제 #3
0
 static double absoluteBearing(Point2D source, Point2D target) {
   return Math.atan2(target.getX() - source.getX(), target.getY() - source.getY());
 }
 /** Computes the angle to target from source */
 public static double absoluteBearing(Point2D.Double source, Point2D.Double target) {
   return Math.atan2(target.x - source.x, target.y - source.y);
 }