Esempio n. 1
0
  /**
   * Creates game objects and adds them to the world.
   *
   * <p>Basically the same shapes from the Shapes test in the TestBed.
   */
  protected void initializeWorld() {
    // create the world
    this.world = new World();

    // create all your bodies/joints

    // create the floor
    Rectangle floorRect = new Rectangle(15.0, 1.0);
    GameObject floor = new GameObject();
    floor.addFixture(new BodyFixture(floorRect));
    floor.setMass(MassType.INFINITE);
    // move the floor down a bit
    floor.translate(0.0, -4.0);
    this.world.addBody(floor);

    // create a triangle object
    Triangle triShape =
        new Triangle(new Vector2(0.0, 0.5), new Vector2(-0.5, -0.5), new Vector2(0.5, -0.5));
    GameObject triangle = new GameObject();
    triangle.addFixture(triShape);
    triangle.setMass(MassType.NORMAL);
    triangle.translate(-1.0, 2.0);
    // test having a velocity
    triangle.getLinearVelocity().set(5.0, 0.0);
    this.world.addBody(triangle);

    // try a rectangle
    Rectangle rectShape = new Rectangle(1.0, 1.0);
    GameObject rectangle = new GameObject();
    rectangle.addFixture(rectShape);
    rectangle.setMass(MassType.NORMAL);
    rectangle.translate(0.0, 2.0);
    rectangle.getLinearVelocity().set(-5.0, 0.0);
    this.world.addBody(rectangle);

    // try a polygon with lots of vertices
    Polygon polyShape = Geometry.createUnitCirclePolygon(10, 1.0);
    GameObject polygon = new GameObject();
    polygon.addFixture(polyShape);
    polygon.setMass(MassType.NORMAL);
    polygon.translate(-2.5, 2.0);
    // set the angular velocity
    polygon.setAngularVelocity(Math.toRadians(-20.0));
    this.world.addBody(polygon);

    GameObject issTri = new GameObject();
    issTri.addFixture(Geometry.createIsoscelesTriangle(1.0, 3.0));
    issTri.setMass(MassType.NORMAL);
    issTri.translate(2.0, 3.0);
    this.world.addBody(issTri);

    GameObject equTri = new GameObject();
    equTri.addFixture(Geometry.createEquilateralTriangle(2.0));
    equTri.setMass(MassType.NORMAL);
    equTri.translate(3.0, 3.0);
    this.world.addBody(equTri);

    GameObject rightTri = new GameObject();
    rightTri.addFixture(Geometry.createRightTriangle(2.0, 1.0));
    rightTri.setMass(MassType.NORMAL);
    rightTri.translate(4.0, 3.0);
    this.world.addBody(rightTri);
  }