Пример #1
0
  public void initTest(boolean argDeserialized) {
    if (argDeserialized) {
      return;
    }

    { // Floor
      FixtureDef fd = new FixtureDef();
      PolygonShape sd = new PolygonShape();
      sd.setAsBox(50.0f, 10.0f);
      fd.shape = sd;

      BodyDef bd = new BodyDef();
      bd.position = new Vec2(0.0f, -10.0f);
      getWorld().createBody(bd).createFixture(fd);
    }

    { // Platforms
      for (int i = 0; i < 4; i++) {
        FixtureDef fd = new FixtureDef();
        PolygonShape sd = new PolygonShape();
        sd.setAsBox(25.0f, 0.125f);
        fd.shape = sd;

        BodyDef bd = new BodyDef();
        bd.position = new Vec2(0.0f, 5f + 5f * i);
        getWorld().createBody(bd).createFixture(fd);
      }
    }

    {
      FixtureDef fd = new FixtureDef();
      PolygonShape sd = new PolygonShape();
      sd.setAsBox(0.125f, 2f);
      fd.shape = sd;
      fd.density = 25.0f;

      BodyDef bd = new BodyDef();
      bd.type = BodyType.DYNAMIC;
      float friction = .5f;
      int numPerRow = 25;

      for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < numPerRow; j++) {
          fd.friction = friction;
          bd.position = new Vec2(-14.75f + j * (29.5f / (numPerRow - 1)), 7.3f + 5f * i);
          if (i == 2 && j == 0) {
            bd.angle = -0.1f;
            bd.position.x += .1f;
          } else if (i == 3 && j == numPerRow - 1) {
            bd.angle = .1f;
            bd.position.x -= .1f;
          } else bd.angle = 0f;
          Body myBody = getWorld().createBody(bd);
          myBody.createFixture(fd);
        }
      }
    }
  }
Пример #2
0
  private Body initPhysicsBody(World world, float x, float y) {
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.DYNAMIC;
    bodyDef.position = new Vec2(0, 0);
    Body body = world.createBody(bodyDef);

    PolygonShape shape = new PolygonShape();
    Transform fx = new Transform();
    fx.position.set(100f, 100f);
    shape.centroid(fx);

    shape.setAsBox(
        sprite.layer().width() * GameScreen.M_PER_PIXEL / 2,
        sprite.layer().height() * GameScreen.M_PER_PIXEL / 2);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    fixtureDef.density = 0.4f;
    fixtureDef.friction = 0.1f;
    fixtureDef.restitution = 0f;
    body.createFixture(fixtureDef);
    body.setLinearDamping(0.2f);
    body.setTransform(new Vec2(x, y), 0f);

    //        MassData md = body.getMassData();
    //        massD.center.set(2f, 0); body.setMassData(massD);

    return body;
  }
Пример #3
0
  public void addAreaObject(String worldname, String objname, LayoutArea area, ObjectProps props) {
    Box2dData data = this.mBox2dWorlds.get(worldname);
    if (data == null) {
      return;
    }

    // Dynamic Body
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.DYNAMIC;
    bodyDef.position.set(area.mLocation[0], area.mLocation[1]);
    Body body = data.mWorld.createBody(bodyDef);

    FixtureDef fixtureDef = new FixtureDef();
    PolygonShape dynamicBox = new PolygonShape();
    dynamicBox.setAsBox(area.mBounds[0] / 2, area.mBounds[1] / 2);
    fixtureDef.shape = dynamicBox;

    fixtureDef.density = props.mDensity;
    fixtureDef.friction = props.mFriction;
    fixtureDef.restitution = props.mRestitution;
    // kinematicBody
    fixtureDef.filter.groupIndex = props.mGroupIndex;
    body.createFixture(fixtureDef);

    BodyData bodydata = new BodyData();
    bodydata.mArea = area;
    bodydata.mBody = body;
    bodydata.mProps = props;
    area.assignPsyData(bodydata);
    data.mAreaModels.add(bodydata);
  }
Пример #4
0
  public void initWorld() {
    Vec2 gravity = new Vec2(0.0f, 9.8f);
    world = new World(gravity);

    // ground
    FixtureDef groundFixtureDef = new FixtureDef();
    PolygonShape groundShape = new PolygonShape();
    groundShape.setAsBox(20f, 0.1f);
    groundFixtureDef.shape = groundShape;
    groundFixtureDef.density = 25.0f;
    groundFixtureDef.filter = new Filter();
    groundFixtureDef.filter.categoryBits = 0x0001;

    BodyDef groundBodyDef = new BodyDef();
    groundBodyDef.position = new Vec2(0.0f, 3f);
    groundBodyDef.angle = 0.0f;
    groundBodyDef.type = BodyType.STATIC;

    groundBody = world.createBody(groundBodyDef);
    groundFixture = groundBody.createFixture(groundFixtureDef);
  }
Пример #5
0
  public void addDynObject(
      String worldname, String objname, GameonModelRef ref, ObjectProps props) {
    Box2dData data = this.mBox2dWorlds.get(worldname);
    if (data == null) {
      return;
    }

    // Dynamic Body
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.DYNAMIC;
    bodyDef.position.set(ref.mPosition[0], ref.mPosition[1]);
    Body body = data.mWorld.createBody(bodyDef);

    FixtureDef fixtureDef = new FixtureDef();
    if (props.mShape == SHAPE_BOX) {
      PolygonShape dynamicBox = new PolygonShape();
      dynamicBox.setAsBox(ref.mScale[0] / 2, ref.mScale[1] / 2);
      fixtureDef.shape = dynamicBox;
    } else {
      CircleShape dynamicCircle = new CircleShape();
      dynamicCircle.m_radius = ref.mScale[0] / 2;
      fixtureDef.shape = dynamicCircle;
    }

    fixtureDef.density = props.mDensity;
    fixtureDef.friction = props.mFriction;
    fixtureDef.restitution = props.mRestitution;
    // kinematicBody
    fixtureDef.filter.groupIndex = props.mGroupIndex;
    body.createFixture(fixtureDef);

    BodyData bodydata = new BodyData();
    bodydata.mRef = ref;
    bodydata.mBody = body;
    bodydata.mProps = props;
    ref.assignPsyData(bodydata);
    data.mDynModels.add(bodydata);
  }
Пример #6
0
  public PhysicsObject(float x, float y, int w, int h, BodyType t) {
    pos.x = x;
    pos.y = y;
    height = h;
    width = w;

    shape = new PolygonShape();
    shape.setAsBox(
        (width / 2) / PhysicsInvaders.PTM_RATIO, (height / 2) / PhysicsInvaders.PTM_RATIO);

    FixtureDef fd = new FixtureDef();
    fd.shape = shape;
    fd.density = 100;
    fd.friction = 0.9f;
    fd.restitution = 0.1f;

    BodyDef bodyDef = new BodyDef();
    bodyDef.position.set(pos.x / PhysicsInvaders.PTM_RATIO, pos.y / PhysicsInvaders.PTM_RATIO);
    bodyDef.type = t;

    body = PhysicsInvaders.world.createBody(bodyDef);
    body.createFixture(fd);
  }
Пример #7
0
  // Constructor
  Box(PApplet parent, PBox2D box2d, float x, float y, float w_, float h_, boolean lock) {

    this.parent = parent;
    this.box2d = box2d;

    w = w_;
    h = h_;

    // Define and create the body
    BodyDef bd = new BodyDef();
    bd.position.set(box2d.coordPixelsToWorld(new Vec2(x, y)));
    if (lock) bd.type = BodyType.STATIC;
    else bd.type = BodyType.DYNAMIC;

    body = box2d.createBody(bd);

    // Define the shape -- a  (this is what we use for a rectangle)
    PolygonShape sd = new PolygonShape();
    float box2dW = box2d.scalarPixelsToWorld(w / 2);
    float box2dH = box2d.scalarPixelsToWorld(h / 2);
    sd.setAsBox(box2dW, box2dH);

    // Define a fixture
    FixtureDef fd = new FixtureDef();
    fd.shape = sd;
    // Parameters that affect physics
    fd.density = 1;
    fd.friction = 0.3f;
    fd.restitution = 0.5f;

    body.createFixture(fd);

    // Give it some initial random velocity
    body.setLinearVelocity(new Vec2(parent.random(-5, 5), parent.random(2, 5)));
    body.setAngularVelocity(parent.random(-5, 5));
  }
  /**
   * Create a convex hull from the given array of points. The count must be in the range [3,
   * Settings.maxPolygonVertices]. This method takes an arraypool for pooling
   *
   * @warning the points may be re-ordered, even if they form a convex polygon
   * @warning collinear points are handled but not removed. Collinear points may lead to poor
   *     stacking behavior.
   */
  public final void set(
      final Vec2[] verts, final int num, final Vec2Array vecPool, final IntArray intPool) {
    assert (3 <= num && num <= Settings.maxPolygonVertices);
    if (num < 3) {
      setAsBox(1.0f, 1.0f);
      return;
    }

    int n = MathUtils.min(num, Settings.maxPolygonVertices);

    // Copy the vertices into a local buffer
    Vec2[] ps = (vecPool != null) ? vecPool.get(n) : new Vec2[n];
    for (int i = 0; i < n; ++i) {
      ps[i] = verts[i];
    }

    // Create the convex hull using the Gift wrapping algorithm
    // http://en.wikipedia.org/wiki/Gift_wrapping_algorithm

    // Find the right most point on the hull
    int i0 = 0;
    float x0 = ps[0].x;
    for (int i = 1; i < num; ++i) {
      float x = ps[i].x;
      if (x > x0 || (x == x0 && ps[i].y < ps[i0].y)) {
        i0 = i;
        x0 = x;
      }
    }

    int[] hull =
        (intPool != null)
            ? intPool.get(Settings.maxPolygonVertices)
            : new int[Settings.maxPolygonVertices];
    int m = 0;
    int ih = i0;

    while (true) {
      hull[m] = ih;

      int ie = 0;
      for (int j = 1; j < n; ++j) {
        if (ie == ih) {
          ie = j;
          continue;
        }

        Vec2 r = pool1.set(ps[ie]).subLocal(ps[hull[m]]);
        Vec2 v = pool2.set(ps[j]).subLocal(ps[hull[m]]);
        float c = Vec2.cross(r, v);
        if (c < 0.0f) {
          ie = j;
        }

        // Collinearity check
        if (c == 0.0f && v.lengthSquared() > r.lengthSquared()) {
          ie = j;
        }
      }

      ++m;
      ih = ie;

      if (ie == i0) {
        break;
      }
    }

    this.m_count = m;

    // Copy vertices.
    for (int i = 0; i < m_count; ++i) {
      if (m_vertices[i] == null) {
        m_vertices[i] = new Vec2();
      }
      m_vertices[i].set(ps[hull[i]]);
    }

    final Vec2 edge = pool1;

    // Compute normals. Ensure the edges have non-zero length.
    for (int i = 0; i < m_count; ++i) {
      final int i1 = i;
      final int i2 = i + 1 < m_count ? i + 1 : 0;
      edge.set(m_vertices[i2]).subLocal(m_vertices[i1]);

      assert (edge.lengthSquared() > Settings.EPSILON * Settings.EPSILON);
      Vec2.crossToOutUnsafe(edge, 1f, m_normals[i]);
      m_normals[i].normalize();
    }

    // Compute the polygon centroid.
    computeCentroidToOut(m_vertices, m_count, m_centroid);
  }
Пример #9
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();
  }
Пример #10
0
 /**
  * Sets the shape as a box.
  *
  * @param x the half-width
  * @param y the half-height
  */
 void setAsBox(float x, float y) {
   mShape.m_type = ShapeType.POLYGON;
   mType = Box2DShapeType.BOX;
   ((PolygonShape) mShape).setAsBox(x, y);
 }
Пример #11
0
  @Override
  public void initTest(boolean argDeserialized) {
    Body ground = null;
    {
      BodyDef bd = new BodyDef();
      ground = getWorld().createBody(bd);

      EdgeShape shape = new EdgeShape();
      shape.set(new Vec2(50.0f, 0.0f), new Vec2(-50.0f, 0.0f));
      ground.createFixture(shape, 0.0f);
    }

    {
      CircleShape circle1 = new CircleShape();
      circle1.m_radius = 1.0f;

      PolygonShape box = new PolygonShape();
      box.setAsBox(0.5f, 5.0f);

      CircleShape circle2 = new CircleShape();
      circle2.m_radius = 2.0f;

      BodyDef bd1 = new BodyDef();
      bd1.type = BodyType.STATIC;
      bd1.position.set(10.0f, 9.0f);
      Body body1 = m_world.createBody(bd1);
      body1.createFixture(circle1, 5.0f);

      BodyDef bd2 = new BodyDef();
      bd2.type = BodyType.DYNAMIC;
      bd2.position.set(10.0f, 8.0f);
      Body body2 = m_world.createBody(bd2);
      body2.createFixture(box, 5.0f);

      BodyDef bd3 = new BodyDef();
      bd3.type = BodyType.DYNAMIC;
      bd3.position.set(10.0f, 6.0f);
      Body body3 = m_world.createBody(bd3);
      body3.createFixture(circle2, 5.0f);

      RevoluteJointDef jd1 = new RevoluteJointDef();
      jd1.initialize(body2, body1, bd1.position);
      Joint joint1 = m_world.createJoint(jd1);

      RevoluteJointDef jd2 = new RevoluteJointDef();
      jd2.initialize(body2, body3, bd3.position);
      Joint joint2 = m_world.createJoint(jd2);

      GearJointDef jd4 = new GearJointDef();
      jd4.bodyA = body1;
      jd4.bodyB = body3;
      jd4.joint1 = joint1;
      jd4.joint2 = joint2;
      jd4.ratio = circle2.m_radius / circle1.m_radius;
      m_world.createJoint(jd4);
    }

    {
      CircleShape circle1 = new CircleShape();
      circle1.m_radius = 1.0f;

      CircleShape circle2 = new CircleShape();
      circle2.m_radius = 2.0f;

      PolygonShape box = new PolygonShape();
      box.setAsBox(0.5f, 5.0f);

      BodyDef bd1 = new BodyDef();
      bd1.type = BodyType.DYNAMIC;
      bd1.position.set(-3.0f, 12.0f);
      Body body1 = m_world.createBody(bd1);
      body1.createFixture(circle1, 5.0f);

      RevoluteJointDef jd1 = new RevoluteJointDef();
      jd1.bodyA = ground;
      jd1.bodyB = body1;
      ground.getLocalPointToOut(bd1.position, jd1.localAnchorA);
      body1.getLocalPointToOut(bd1.position, jd1.localAnchorB);
      jd1.referenceAngle = body1.getAngle() - ground.getAngle();
      m_joint1 = (RevoluteJoint) m_world.createJoint(jd1);

      BodyDef bd2 = new BodyDef();
      bd2.type = BodyType.DYNAMIC;
      bd2.position.set(0.0f, 12.0f);
      Body body2 = m_world.createBody(bd2);
      body2.createFixture(circle2, 5.0f);

      RevoluteJointDef jd2 = new RevoluteJointDef();
      jd2.initialize(ground, body2, bd2.position);
      m_joint2 = (RevoluteJoint) m_world.createJoint(jd2);

      BodyDef bd3 = new BodyDef();
      bd3.type = BodyType.DYNAMIC;
      bd3.position.set(2.5f, 12.0f);
      Body body3 = m_world.createBody(bd3);
      body3.createFixture(box, 5.0f);

      PrismaticJointDef jd3 = new PrismaticJointDef();
      jd3.initialize(ground, body3, bd3.position, new Vec2(0.0f, 1.0f));
      jd3.lowerTranslation = -5.0f;
      jd3.upperTranslation = 5.0f;
      jd3.enableLimit = true;

      m_joint3 = (PrismaticJoint) m_world.createJoint(jd3);

      GearJointDef jd4 = new GearJointDef();
      jd4.bodyA = body1;
      jd4.bodyB = body2;
      jd4.joint1 = m_joint1;
      jd4.joint2 = m_joint2;
      jd4.ratio = circle2.m_radius / circle1.m_radius;
      m_joint4 = (GearJoint) m_world.createJoint(jd4);

      GearJointDef jd5 = new GearJointDef();
      jd5.bodyA = body2;
      jd5.bodyB = body3;
      jd5.joint1 = m_joint2;
      jd5.joint2 = m_joint3;
      jd5.ratio = 1f / circle2.m_radius;
      m_joint5 = (GearJoint) m_world.createJoint(jd5);
    }
  }