Ejemplo n.º 1
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();
  }