示例#1
0
  @Override
  public void start() {
    super.start();

    environment =
        new Continuous2D(1.0, config.getEnvironmentWidth(), config.getEnvironmentHeight());
    drawProxy = new DrawProxy(environment.getWidth(), environment.getHeight());
    environment.setObjectLocation(drawProxy, new Double2D());

    physicsWorld = new World(new Vec2());
    placementArea =
        new PlacementArea((float) environment.getWidth(), (float) environment.getHeight());
    placementArea.setSeed(config.getSimulationSeed());
    schedule.reset();
    System.gc();

    physicsWorld.setContactListener(contactListener);

    // Create ALL the objects
    createWalls();
    createTargetArea();
    robotFactory.placeInstances(
        placementArea.new ForType<>(), physicsWorld, config.getTargetAreaPlacement());
    config.getResourceFactory().placeInstances(placementArea.new ForType<>(), physicsWorld);

    // Now actually add the objects that have been placed to the world and schedule
    for (PhysicalObject object : placementArea.getPlacedObjects()) {
      drawProxy.registerDrawable(object.getPortrayal());
      schedule.scheduleRepeating(object);
    }

    schedule.scheduleRepeating(
        simState -> physicsWorld.step(TIME_STEP, VELOCITY_ITERATIONS, POSITION_ITERATIONS));
  }
示例#2
0
  // Walls are simply added to environment since they do not need updating
  private void createWalls() {
    int environmentWidth = config.getEnvironmentWidth();
    int environmentHeight = config.getEnvironmentHeight();
    // Left
    Double2D pos = new Double2D(0, environmentHeight / 2.0);
    Double2D v1 = new Double2D(0, -pos.y);
    Double2D v2 = new Double2D(0, pos.y);
    WallObject wall = new WallObject(physicsWorld, pos, v1, v2);
    drawProxy.registerDrawable(wall.getPortrayal());

    // Right
    pos = new Double2D(environmentWidth, environmentHeight / 2.0);
    wall = new WallObject(physicsWorld, pos, v1, v2);
    drawProxy.registerDrawable(wall.getPortrayal());

    // Top
    pos = new Double2D(environmentWidth / 2.0, 0);
    v1 = new Double2D(-pos.x, 0);
    v2 = new Double2D(pos.x, 0);
    wall = new WallObject(physicsWorld, pos, v1, v2);
    drawProxy.registerDrawable(wall.getPortrayal());

    // Bottom
    pos = new Double2D(environmentWidth / 2.0, environmentHeight);
    wall = new WallObject(physicsWorld, pos, v1, v2);
    drawProxy.registerDrawable(wall.getPortrayal());
  }
示例#3
0
  // create target area
  private void createTargetArea() {
    int environmentWidth = config.getEnvironmentWidth();
    int environmentHeight = config.getEnvironmentHeight();

    final int width, height;
    final Vec2 position;
    if (config.getTargetAreaPlacement() == SimConfig.Direction.SOUTH) {
      width = environmentWidth;
      height = config.getTargetAreaThickness();
      position = new Vec2(width / 2f, height / 2f);
    } else if (config.getTargetAreaPlacement() == SimConfig.Direction.NORTH) {
      width = environmentWidth;
      height = config.getTargetAreaThickness();
      position = new Vec2(environmentWidth - width / 2f, environmentHeight - height / 2f);
    } else if (config.getTargetAreaPlacement() == SimConfig.Direction.EAST) {
      width = config.getTargetAreaThickness();
      height = environmentHeight;
      position = new Vec2(environmentWidth - width / 2f, height / 2f);
    } else if (config.getTargetAreaPlacement() == SimConfig.Direction.WEST) {
      width = config.getTargetAreaThickness();
      height = environmentHeight;
      position = new Vec2(width / 2f, height / 2f);
    } else {
      return; // Don't know where to place this target area
    }

    targetArea =
        new TargetAreaObject(
            physicsWorld,
            position,
            width,
            height,
            config.getResourceFactory().getTotalResourceValue(),
            config.getSimulationIterations());

    // Add target area to placement area (trust that space returned since nothing else placed
    // yet).
    PlacementArea.Space space = placementArea.getRectangularSpace(width, height, position, 0f);
    placementArea.placeObject(space, targetArea);
  }