Пример #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
  public void update(GameContainer arg0, int arg1) throws SlickException {
    world.step(timeStep, velocityIterations, positionIterations);

    Fixture fa = cDetect.fa;
    Fixture fb = cDetect.fb;

    if (fa != null && fb != null) {

      player1.checkDeleteFixture(fa, fb);
      player2.checkDeleteFixture(fa, fb);

      asteroids.damageAsteroid(fa);
      asteroids.damageAsteroid(fb);
    }

    player1.batteryLeft = 100f;

    player1.calcForce(input, arg1, player2.player.getPosition());
    player2.calcForceCPU(
        player1.player.getPosition(), asteroids.asteroids, new ArrayList<Vec2>(), arg1);

    player1.checkDeath();
    player2.checkDeath();

    if (player1.dead) {
      over = true;
    }
    if (player2.dead) {
      winner += 1; // In this case, winner will be used to determine score
      world.destroyBody(player2.player);
      player2 = new ShipCPU(player2Ship, world, tr, level);
      float jX = tr.width / tr.xscale;
      float jY = tr.height / tr.yscale;

      float max = jY / 2 - 15;
      float min = -jY / 2 + 15;
      float p2Y = (float) (Math.random() * (max - min) + min);

      player2.player.setTransform(new Vec2(jX / 2 - 15, p2Y), (float) Math.PI / 2);
    }

    if (input.isKeyDown(Input.KEY_ESCAPE)) {
      over = true;
    }
  }
Пример #3
0
 public void step(float step_speed) {
   world.step(step_speed, 10, 10);
 }
Пример #4
0
  public void start() {

    Vec2 gravity = new Vec2(0, 0);
    World world = new World(gravity);
    BodyDef groundBodyDef = new BodyDef();
    groundBodyDef.position.set(0, -25);
    Body groundBody = world.createBody(groundBodyDef);
    PolygonShape groundBox = new PolygonShape();
    groundBox.setAsBox(900, 10);
    groundBody.createFixture(groundBox, 0);

    // Dynamic Body
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.DYNAMIC;
    bodyDef.position.set(0, 0);
    Body body = world.createBody(bodyDef);
    PolygonShape dynamicBox = new PolygonShape();
    dynamicBox.setAsBox(12, 12);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = dynamicBox;
    fixtureDef.density = 1f;
    fixtureDef.friction = 0.3f;
    body.createFixture(fixtureDef);

    // Setup world
    float timeStep = 1.0f / 60.0f;
    int velocityIterations = 6;
    int positionIterations = 2;
    body.setLinearVelocity(new Vec2(15.0f, -15.0f));
    body.setLinearDamping(2f);
    Vec2 f = body.getWorldVector(new Vec2(0.0f, -30.0f));
    Vec2 p = body.getWorldPoint(body.getLocalCenter().add(new Vec2(-.2f, 0f)));
    // body.applyForce(new Vec2(-200,-200),new Vec2(-200,200));

    try {
      Display.setDisplayMode(new DisplayMode(screen_width, screen_height));
      Display.create();
    } catch (LWJGLException e) {
      e.printStackTrace();
      System.exit(0);
    }

    lastFPS = getTime();

    // standardBall[0] = new Ball(400,200,0,12);
    // standardBall[1] = new Ball(100,210,2,12);
    // standardBall[0].addImpulse(-10,0);

    standardBall[0] = new Ball(200, 300, 0, 12);
    standardBall[1] = new Ball(415, 100, 2, 12);
    standardBall[0].addImpulse(10, -9.99f);

    standardBall[2] = new Ball(620, 270, 2, 12);
    standardBall[3] = new Ball(640, 290, 2, 12);
    standardBall[4] = new Ball(660, 310, 2, 12);
    standardBall[5] = new Ball(680, 330, 2, 12);

    standardBall[6] = new Ball(620, 230, 2, 12);
    standardBall[7] = new Ball(640, 210, 2, 12);
    standardBall[8] = new Ball(660, 190, 2, 12);
    standardBall[9] = new Ball(680, 170, 2, 12);

    standardBall[10] = new Ball(640, 250, 1, 12);
    standardBall[11] = new Ball(660, 230, 2, 12);
    standardBall[12] = new Ball(660, 270, 2, 12);
    standardBall[13] = new Ball(680, 290, 2, 12);
    standardBall[14] = new Ball(680, 210, 2, 12);
    standardBall[15] = new Ball(680, 250, 2, 12);

    standardTable = new Table(0, 0, 800, 400, 25, 15);

    // init OpenGL
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GL11.glOrtho(0, 900, 0, 500, 1, -1);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);

    while (!Display.isCloseRequested()) {
      int delta = getDelta();
      world.step(timeStep, velocityIterations, positionIterations);
      Vec2 position = body.getPosition();
      float angle = body.getAngle();
      // System.out.printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);

      // Clear the screen and depth buffer
      GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);

      standardTable.draw();

      for (int i = 0; i < NUMBER_OF_BALLS; i++) {
        standardBall[i].draw();
        standardBall[i].update(delta);
      }
      standardBall[0].setX((position.x * 20) + offset_x);
      standardBall[0].setY((position.y * 20) + offset_y);

      update(delta);

      Display.update();

      Display.sync(60);
    }
    Display.destroy();
  }