예제 #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();
  }
예제 #2
0
  @Override
  public void evaluate(ContactListener listener) {
    Body b1 = m_shape1.getBody();
    Body b2 = m_shape2.getBody();
    // Manifold m0 = m_manifold;
    Manifold m0 = new Manifold(m_manifold);
    // This next stuff might be unnecessary now [ewj: nope, we need it]
    for (int k = 0; k < m_manifold.pointCount; k++) {
      m0.points[k] = new ManifoldPoint(m_manifold.points[k]);
      m0.points[k].normalImpulse = m_manifold.points[k].normalImpulse;
      m0.points[k].tangentImpulse = m_manifold.points[k].tangentImpulse;
      m0.points[k].separation = m_manifold.points[k].separation;
      // m0.points[k].id.key = m_manifold.points[k].id.key;
      m0.points[k].id.features.set(m_manifold.points[k].id.features);
      // System.out.println(m_manifold.points[k].normalForce);
    }
    m0.pointCount = m_manifold.pointCount;

    CollidePoly.collidePolygons(
        m_manifold, (PolygonShape) m_shape1, b1.getXForm(), (PolygonShape) m_shape2, b2.getXForm());

    boolean[] persisted = {false, false};

    ContactPoint cp = new ContactPoint();
    cp.shape1 = m_shape1;
    cp.shape2 = m_shape2;
    cp.friction = m_friction;
    cp.restitution = m_restitution;

    // Match contact ids to facilitate warm starting.
    // Watch out (Java note):
    // m_manifold.pointCount != m_manifold.points.length!!!

    // Match contact ids to facilitate warm starting.
    if (m_manifold.pointCount > 0) {
      // Match old contact ids to new contact ids and copy the
      // stored impulses to warm start the solver.
      for (int i = 0; i < m_manifold.pointCount; ++i) {
        ManifoldPoint mp = m_manifold.points[i];
        mp.normalImpulse = 0.0f;
        mp.tangentImpulse = 0.0f;
        boolean found = false;
        ContactID id = new ContactID(mp.id);

        for (int j = 0; j < m0.pointCount; ++j) {
          if (persisted[j] == true) {
            continue;
          }

          ManifoldPoint mp0 = m0.points[j];

          if (mp0.id.isEqual(id)) {
            persisted[j] = true;
            mp.normalImpulse = mp0.normalImpulse;
            mp.tangentImpulse = mp0.tangentImpulse;

            // A persistent point.
            found = true;

            // Report persistent point.
            if (listener != null) {
              cp.position = b1.getWorldPoint(mp.localPoint1);
              Vec2 v1 = b1.getLinearVelocityFromLocalPoint(mp.localPoint1);
              Vec2 v2 = b2.getLinearVelocityFromLocalPoint(mp.localPoint2);
              cp.velocity = v2.sub(v1);
              cp.normal = m_manifold.normal.clone();
              cp.separation = mp.separation;
              cp.id = new ContactID(id);
              listener.persist(cp);
            }
            break;
          }
        }

        // Report added point.
        if (found == false && listener != null) {
          cp.position = b1.getWorldPoint(mp.localPoint1);
          Vec2 v1 = b1.getLinearVelocityFromLocalPoint(mp.localPoint1);
          Vec2 v2 = b2.getLinearVelocityFromLocalPoint(mp.localPoint2);
          cp.velocity = v2.sub(v1);
          cp.normal = m_manifold.normal.clone();
          cp.separation = mp.separation;
          cp.id = new ContactID(id);
          listener.add(cp);
        }
      }

      m_manifoldCount = 1;
    } else {
      m_manifoldCount = 0;
    }

    if (listener == null) {
      return;
    }

    // Report removed points.
    for (int i = 0; i < m0.pointCount; ++i) {
      if (persisted[i]) {
        continue;
      }

      ManifoldPoint mp0 = m0.points[i];
      cp.position = b1.getWorldPoint(mp0.localPoint1);
      Vec2 v1 = b1.getLinearVelocityFromLocalPoint(mp0.localPoint1);
      Vec2 v2 = b2.getLinearVelocityFromLocalPoint(mp0.localPoint2);
      cp.velocity = v2.sub(v1);
      cp.normal = m0.normal.clone();
      cp.separation = mp0.separation;
      cp.id = new ContactID(mp0.id);
      listener.remove(cp);
    }
  }
예제 #3
0
  public void evaluate(ContactListener listener) {
    // CollideCircle.collideCircle(m_manifold, (CircleShape) m_shape1,
    //        (CircleShape) m_shape2, false);
    Body b1 = m_shape1.getBody();
    Body b2 = m_shape2.getBody();

    Manifold m0 = new Manifold(m_manifold);
    for (int k = 0; k < m_manifold.pointCount; k++) {
      m0.points[k] = new ManifoldPoint(m_manifold.points[k]);
      m0.points[k].normalImpulse = m_manifold.points[k].normalImpulse;
      m0.points[k].tangentImpulse = m_manifold.points[k].tangentImpulse;
      m0.points[k].separation = m_manifold.points[k].separation;
      // m0.points[k].id.key = m_manifold.points[k].id.key;
      m0.points[k].id.features.set(m_manifold.points[k].id.features);
      // System.out.println(m_manifold.points[k].id.key);
    }
    m0.pointCount = m_manifold.pointCount;

    CollideCircle.collideCircles(
        m_manifold, (CircleShape) m_shape1, b1.m_xf, (CircleShape) m_shape2, b2.m_xf);

    ContactPoint cp = new ContactPoint();
    cp.shape1 = m_shape1;
    cp.shape2 = m_shape2;
    cp.friction = m_friction;
    cp.restitution = m_restitution;

    if (m_manifold.pointCount > 0) {
      m_manifoldCount = 1;
      ManifoldPoint mp = m_manifold.points[0];
      if (m0.pointCount == 0) {
        mp.normalImpulse = 0.0f;
        mp.tangentImpulse = 0.0f;

        if (listener != null) {
          cp.position = b1.getWorldPoint(mp.localPoint1);
          Vec2 v1 = b1.getLinearVelocityFromLocalPoint(mp.localPoint1);
          Vec2 v2 = b2.getLinearVelocityFromLocalPoint(mp.localPoint2);
          cp.velocity = v2.sub(v1);
          cp.normal = m_manifold.normal.clone();
          cp.separation = mp.separation;
          cp.id = new ContactID(mp.id);
          listener.add(cp);
        }
      } else {
        ManifoldPoint mp0 = m0.points[0];
        mp.normalImpulse = mp0.normalImpulse;
        mp.tangentImpulse = mp0.tangentImpulse;

        if (listener != null) {
          cp.position = b1.getWorldPoint(mp.localPoint1);
          Vec2 v1 = b1.getLinearVelocityFromLocalPoint(mp.localPoint1);
          Vec2 v2 = b2.getLinearVelocityFromLocalPoint(mp.localPoint2);
          cp.velocity = v2.sub(v1);
          cp.normal = m_manifold.normal.clone();
          cp.separation = mp.separation;
          cp.id = new ContactID(mp.id);
          listener.persist(cp);
        }
      }
    } else {
      m_manifoldCount = 0;
      if (m0.pointCount > 0 && (listener != null)) {
        ManifoldPoint mp0 = m0.points[0];
        cp.position = b1.getWorldPoint(mp0.localPoint1);
        Vec2 v1 = b1.getLinearVelocityFromLocalPoint(mp0.localPoint1);
        Vec2 v2 = b2.getLinearVelocityFromLocalPoint(mp0.localPoint2);
        cp.velocity = v2.sub(v1);
        cp.normal = m0.normal.clone();
        cp.separation = mp0.separation;
        cp.id = new ContactID(mp0.id);
        listener.remove(cp);
      }
    }
  }