Пример #1
0
  /* (non-Javadoc)
   * @see org.dyn4j.samples.SimulationFrame#initializeWorld()
   */
  @Override
  protected void initializeWorld() {
    // no gravity on a top-down view of a billiards game
    this.world.setGravity(World.ZERO_GRAVITY);

    // create all your bodies/joints

    SimulationBody wallr = new SimulationBody();
    wallr.addFixture(Geometry.createRectangle(0.2, 10));
    wallr.translate(2, 0);
    wallr.setMass(MassType.INFINITE);
    world.addBody(wallr);

    SimulationBody ball1 = new SimulationBody();
    ball1.addFixture(
        Geometry.createCircle(0.028575), //  2.25 in diameter = 0.028575 m radius
        217.97925, //  0.126 oz/in^3 = 217.97925 kg/m^3
        0.08,
        0.9);
    ball1.translate(-1.0, 0.0);
    // ball1.setLinearVelocity(5.36448, 0.0); 		  // 12 mph = 5.36448 m/s
    ball1.setLinearVelocity(2, 0); //  so we can see the bouncing
    ball1.setMass(MassType.NORMAL);
    this.world.addBody(ball1);

    SimulationBody ball2 = new SimulationBody();
    ball2.addFixture(Geometry.createCircle(0.028575), 217.97925, 0.08, 0.9);
    ball2.translate(1.0, 0.0);
    ball2.setMass(MassType.NORMAL);
    this.world.addBody(ball2);
  }
Пример #2
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);
  }