org.jbox2d.dynamics.contacts.Contact Java Examples

The following examples show how to use org.jbox2d.dynamics.contacts.Contact. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Box2DContactListener.java    From Form-N-Fun with MIT License 6 votes vote down vote up
@Override
public void beginContact(Contact cp) {
    // TODO Auto-generated method stub
    Fixture f1 = cp.getFixtureA();
    Fixture f2 = cp.getFixtureB();

    // Get both bodies
    Body b1 = f1.getBody();
    Body b2 = f2.getBody();

    // Get objects that reference these bodies
    Object o1 = b1.getUserData();
    Object o2 = b2.getUserData();

    if ((o1.getClass() == Maze.class && o2.getClass() == Ball.class) || (o1.getClass() == Ball.class && o2.getClass() == Maze.class)) { //if ball touches maze
        balltosurfacecontact(); //play sound
    }

}
 
Example #2
Source File: OneSidedTest.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void preSolve(Contact contact, Manifold oldManifold) {
  super.preSolve(contact, oldManifold);

  Fixture fixtureA = contact.getFixtureA();
  Fixture fixtureB = contact.getFixtureB();

  if (fixtureA != m_platform && fixtureA != m_character) {
    return;
  }

  if (fixtureB != m_character && fixtureB != m_character) {
    return;
  }

  Vec2 position = m_character.getBody().getPosition();

  if (position.y < m_top + m_radius - 3.0f * Settings.linearSlop) {
    contact.setEnabled(false);
  }
}
 
Example #3
Source File: Breakable.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void postSolve(Contact contact, ContactImpulse impulse) {
  if (m_broke) {
    // The body already broke.
    return;
  }

  // Should the body break?
  int count = contact.getManifold().pointCount;

  float maxImpulse = 0.0f;
  for (int i = 0; i < count; ++i) {
    maxImpulse = MathUtils.max(maxImpulse, impulse.normalImpulses[i]);
  }

  if (maxImpulse > 40.0f) {
    // Flag the body for breaking.
    m_break = true;
  }
}
 
Example #4
Source File: Island.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void report(ContactVelocityConstraint[] constraints) {
  if (m_listener == null) {
    return;
  }

  for (int i = 0; i < m_contactCount; ++i) {
    Contact c = m_contacts[i];

    ContactVelocityConstraint vc = constraints[i];
    impulse.count = vc.pointCount;
    for (int j = 0; j < vc.pointCount; ++j) {
      impulse.normalImpulses[j] = vc.points[j].normalImpulse;
      impulse.tangentImpulses[j] = vc.points[j].tangentImpulse;
    }

    m_listener.postSolve(c, impulse);
  }
}
 
Example #5
Source File: TestbedTest.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void preSolve(Contact contact, Manifold oldManifold) {
  Manifold manifold = contact.getManifold();

  if (manifold.pointCount == 0) {
    return;
  }

  Fixture fixtureA = contact.getFixtureA();
  Fixture fixtureB = contact.getFixtureB();

  Collision.getPointStates(state1, state2, oldManifold, manifold);

  contact.getWorldManifold(worldManifold);

  for (int i = 0; i < manifold.pointCount && pointCount < MAX_CONTACT_POINTS; i++) {
    ContactPoint cp = points[pointCount];
    cp.fixtureA = fixtureA;
    cp.fixtureB = fixtureB;
    cp.position.set(worldManifold.points[i]);
    cp.normal.set(worldManifold.normal);
    cp.state = state2[i];
    cp.normalImpulse = manifold.points[i].normalImpulse;
    cp.tangentImpulse = manifold.points[i].tangentImpulse;
    cp.separation = worldManifold.separations[i];
    ++pointCount;
  }
}
 
Example #6
Source File: JBox2DTest.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Test
public void testCircleContactStack() {
    System.out.println("A");
    DefaultWorldPool thePool = new DefaultWorldPool(10, 10);
    System.out.println("B");
    IDynamicStack<Contact> theCircleStack = thePool.getCircleContactStack();
    System.out.println("C");
    Contact theContact = theCircleStack.pop();
    System.out.println("D");
    Assert.assertTrue(theContact instanceof CircleContact);
    System.out.println("E");
}
 
Example #7
Source File: ConveyorBelt.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void preSolve(Contact contact, Manifold oldManifold) {
  super.preSolve(contact, oldManifold);

  Fixture fixtureA = contact.getFixtureA();
  Fixture fixtureB = contact.getFixtureB();

  if (fixtureA == m_platform || fixtureB == m_platform) {
    contact.setTangentSpeed(5.0f);
  }
}
 
Example #8
Source File: World.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void pushContact(Contact contact) {
  Fixture fixtureA = contact.getFixtureA();
  Fixture fixtureB = contact.getFixtureB();

  if (contact.m_manifold.pointCount > 0 && !fixtureA.isSensor() && !fixtureB.isSensor()) {
    fixtureA.getBody().setAwake(true);
    fixtureB.getBody().setAwake(true);
  }

  ShapeType type1 = fixtureA.getType();
  ShapeType type2 = fixtureB.getType();

  IDynamicStack<Contact> creator = contactStacks[type1.ordinal()][type2.ordinal()].creator;
  creator.push(contact);
}
 
Example #9
Source File: World.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void addType(IDynamicStack<Contact> creator, ShapeType type1, ShapeType type2) {
  ContactRegister register = new ContactRegister();
  register.creator = creator;
  register.primary = true;
  contactStacks[type1.ordinal()][type2.ordinal()] = register;

  if (type1 != type2) {
    ContactRegister register2 = new ContactRegister();
    register2.creator = creator;
    register2.primary = false;
    contactStacks[type2.ordinal()][type1.ordinal()] = register2;
  }
}
 
Example #10
Source File: Fixture.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Call this if you want to establish collision that was previously disabled by
 * ContactFilter::ShouldCollide.
 */
public void refilter() {
  if (m_body == null) {
    return;
  }

  // Flag associated contacts for filtering.
  ContactEdge edge = m_body.getContactList();
  while (edge != null) {
    Contact contact = edge.contact;
    Fixture fixtureA = contact.getFixtureA();
    Fixture fixtureB = contact.getFixtureB();
    if (fixtureA == this || fixtureB == this) {
      contact.flagForFiltering();
    }
    edge = edge.next;
  }

  World world = m_body.getWorld();

  if (world == null) {
    return;
  }

  // Touch each proxy so that new pairs may be created
  BroadPhase broadPhase = world.m_contactManager.m_broadPhase;
  for (int i = 0; i < m_proxyCount; ++i) {
    broadPhase.touchProxy(m_proxies[i].proxyId);
  }
}
 
Example #11
Source File: Island.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void add(Contact contact) {
  assert (m_contactCount < m_contactCapacity);
  m_contacts[m_contactCount++] = contact;
}
 
Example #12
Source File: Body.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Destroy a fixture. This removes the fixture from the broad-phase and destroys all contacts
 * associated with this fixture. This will automatically adjust the mass of the body if the body
 * is dynamic and the fixture has positive density. All fixtures attached to a body are implicitly
 * destroyed when the body is destroyed.
 * 
 * @param fixture the fixture to be removed.
 * @warning This function is locked during callbacks.
 */
public final void destroyFixture(Fixture fixture) {
  assert (m_world.isLocked() == false);
  if (m_world.isLocked() == true) {
    return;
  }

  assert (fixture.m_body == this);

  // Remove the fixture from this body's singly linked list.
  assert (m_fixtureCount > 0);
  Fixture node = m_fixtureList;
  Fixture last = null; // java change
  boolean found = false;
  while (node != null) {
    if (node == fixture) {
      node = fixture.m_next;
      found = true;
      break;
    }
    last = node;
    node = node.m_next;
  }

  // You tried to remove a shape that is not attached to this body.
  assert (found);

  // java change, remove it from the list
  if (last == null) {
    m_fixtureList = fixture.m_next;
  } else {
    last.m_next = fixture.m_next;
  }

  // Destroy any contacts associated with the fixture.
  ContactEdge edge = m_contactList;
  while (edge != null) {
    Contact c = edge.contact;
    edge = edge.next;

    Fixture fixtureA = c.getFixtureA();
    Fixture fixtureB = c.getFixtureB();

    if (fixture == fixtureA || fixture == fixtureB) {
      // This destroys the contact and removes it from
      // this body's contact list.
      m_world.m_contactManager.destroy(c);
    }
  }

  if ((m_flags & e_activeFlag) == e_activeFlag) {
    BroadPhase broadPhase = m_world.m_contactManager.m_broadPhase;
    fixture.destroyProxies(broadPhase);
  }

  fixture.destroy();
  fixture.m_body = null;
  fixture.m_next = null;
  fixture = null;

  --m_fixtureCount;

  // Reset the mass data.
  resetMassData();
}
 
Example #13
Source File: ContactManager.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void destroy(Contact c) {
  Fixture fixtureA = c.getFixtureA();
  Fixture fixtureB = c.getFixtureB();
  Body bodyA = fixtureA.getBody();
  Body bodyB = fixtureB.getBody();

  if (m_contactListener != null && c.isTouching()) {
    m_contactListener.endContact(c);
  }

  // Remove from the world.
  if (c.m_prev != null) {
    c.m_prev.m_next = c.m_next;
  }

  if (c.m_next != null) {
    c.m_next.m_prev = c.m_prev;
  }

  if (c == m_contactList) {
    m_contactList = c.m_next;
  }

  // Remove from body 1
  if (c.m_nodeA.prev != null) {
    c.m_nodeA.prev.next = c.m_nodeA.next;
  }

  if (c.m_nodeA.next != null) {
    c.m_nodeA.next.prev = c.m_nodeA.prev;
  }

  if (c.m_nodeA == bodyA.m_contactList) {
    bodyA.m_contactList = c.m_nodeA.next;
  }

  // Remove from body 2
  if (c.m_nodeB.prev != null) {
    c.m_nodeB.prev.next = c.m_nodeB.next;
  }

  if (c.m_nodeB.next != null) {
    c.m_nodeB.next.prev = c.m_nodeB.prev;
  }

  if (c.m_nodeB == bodyB.m_contactList) {
    bodyB.m_contactList = c.m_nodeB.next;
  }

  // Call the factory.
  pool.pushContact(c);
  --m_contactCount;
}
 
Example #14
Source File: Box2DContactListener.java    From Form-N-Fun with MIT License 4 votes vote down vote up
@Override
public void endContact(Contact arg0) {
    // TODO Auto-generated method stub

}
 
Example #15
Source File: Box2DContactListener.java    From Form-N-Fun with MIT License 4 votes vote down vote up
@Override
public void postSolve(Contact arg0, ContactImpulse arg1) {
    // TODO Auto-generated method stub

}
 
Example #16
Source File: World.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Call this to draw shapes and other debug draw data.
 */
public void drawDebugData() {
  if (m_debugDraw == null) {
    return;
  }


  int flags = m_debugDraw.getFlags();
  boolean wireframe = (flags & DebugDraw.e_wireframeDrawingBit) != 0;

  if ((flags & DebugDraw.e_shapeBit) != 0) {
    for (Body b = m_bodyList; b != null; b = b.getNext()) {
      xf.set(b.getTransform());
      for (Fixture f = b.getFixtureList(); f != null; f = f.getNext()) {
        if (b.isActive() == false) {
          color.set(0.5f, 0.5f, 0.3f);
          drawShape(f, xf, color, wireframe);
        } else if (b.getType() == BodyType.STATIC) {
          color.set(0.5f, 0.9f, 0.3f);
          drawShape(f, xf, color, wireframe);
        } else if (b.getType() == BodyType.KINEMATIC) {
          color.set(0.5f, 0.5f, 0.9f);
          drawShape(f, xf, color, wireframe);
        } else if (b.isAwake() == false) {
          color.set(0.5f, 0.5f, 0.5f);
          drawShape(f, xf, color, wireframe);
        } else {
          color.set(0.9f, 0.7f, 0.7f);
          drawShape(f, xf, color, wireframe);
        }
      }
    }
    drawParticleSystem(m_particleSystem);
  }

  if ((flags & DebugDraw.e_jointBit) != 0) {
    for (Joint j = m_jointList; j != null; j = j.getNext()) {
      drawJoint(j);
    }
  }

  if ((flags & DebugDraw.e_pairBit) != 0) {
    color.set(0.3f, 0.9f, 0.9f);
    for (Contact c = m_contactManager.m_contactList; c != null; c = c.getNext()) {
      Fixture fixtureA = c.getFixtureA();
      Fixture fixtureB = c.getFixtureB();
      fixtureA.getAABB(c.getChildIndexA()).getCenterToOut(cA);
      fixtureB.getAABB(c.getChildIndexB()).getCenterToOut(cB);
      m_debugDraw.drawSegment(cA, cB, color);
    }
  }

  if ((flags & DebugDraw.e_aabbBit) != 0) {
    color.set(0.9f, 0.3f, 0.9f);

    for (Body b = m_bodyList; b != null; b = b.getNext()) {
      if (b.isActive() == false) {
        continue;
      }

      for (Fixture f = b.getFixtureList(); f != null; f = f.getNext()) {
        for (int i = 0; i < f.m_proxyCount; ++i) {
          FixtureProxy proxy = f.m_proxies[i];
          AABB aabb = m_contactManager.m_broadPhase.getFatAABB(proxy.proxyId);
          if (aabb != null) {
            Vec2[] vs = avs.get(4);
            vs[0].set(aabb.lowerBound.x, aabb.lowerBound.y);
            vs[1].set(aabb.upperBound.x, aabb.lowerBound.y);
            vs[2].set(aabb.upperBound.x, aabb.upperBound.y);
            vs[3].set(aabb.lowerBound.x, aabb.upperBound.y);
            m_debugDraw.drawPolygon(vs, 4, color);
          }
        }
      }
    }
  }

  if ((flags & DebugDraw.e_centerOfMassBit) != 0) {
    for (Body b = m_bodyList; b != null; b = b.getNext()) {
      xf.set(b.getTransform());
      xf.p.set(b.getWorldCenter());
      m_debugDraw.drawTransform(xf);
    }
  }

  if ((flags & DebugDraw.e_dynamicTreeBit) != 0) {
    m_contactManager.m_broadPhase.drawTree(m_debugDraw);
  }

  m_debugDraw.flush();
}
 
Example #17
Source File: Box2DContactListener.java    From Form-N-Fun with MIT License 4 votes vote down vote up
@Override
public void preSolve(Contact arg0, Manifold arg1) {
    // TODO Auto-generated method stub

}
 
Example #18
Source File: DefaultWorldPool.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public IDynamicStack<Contact> getChainPolyContactStack() {
  return chpstack;
}
 
Example #19
Source File: DefaultWorldPool.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public IDynamicStack<Contact> getChainCircleContactStack() {
  return chcstack;
}
 
Example #20
Source File: DefaultWorldPool.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public IDynamicStack<Contact> getEdgePolyContactStack() {
  return epstack;
}
 
Example #21
Source File: DefaultWorldPool.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public IDynamicStack<Contact> getEdgeCircleContactStack() {
  return ecstack;
}
 
Example #22
Source File: DefaultWorldPool.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public final IDynamicStack<Contact> getPolyCircleContactStack() {
  return cpstack;
}
 
Example #23
Source File: DefaultWorldPool.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public final IDynamicStack<Contact> getCircleContactStack() {
  return ccstack;
}
 
Example #24
Source File: DefaultWorldPool.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public final IDynamicStack<Contact> getPolyContactStack() {
  return pcstack;
}
 
Example #25
Source File: ContactListener.java    From jbox2d with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * This lets you inspect a contact after the solver is finished. This is useful
 * for inspecting impulses.
 * Note: the contact manifold does not include time of impact impulses, which can be
 * arbitrarily large if the sub-step is small. Hence the impulse is provided explicitly
 * in a separate data structure.
 * Note: this is only called for contacts that are touching, solid, and awake.
 * @param contact
 * @param impulse this is usually a pooled variable, so it will be modified after
 * this call
 */
public void postSolve(Contact contact, ContactImpulse impulse);
 
Example #26
Source File: ContactListener.java    From jbox2d with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * Called when two fixtures begin to touch.
 * @param contact
 */
public void beginContact(Contact contact);
 
Example #27
Source File: ContactListener.java    From jbox2d with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * This is called after a contact is updated. This allows you to inspect a
 * contact before it goes to the solver. If you are careful, you can modify the
 * contact manifold (e.g. disable contact).
 * A copy of the old manifold is provided so that you can detect changes.
 * Note: this is called only for awake bodies.
 * Note: this is called even when the number of contact points is zero.
 * Note: this is not called for sensors.
 * Note: if you set the number of contact points to zero, you will not
 * get an EndContact callback. However, you may get a BeginContact callback
 * the next step.
 * Note: the oldManifold parameter is pooled, so it will be the same object for every callback
 * for each thread.
 * @param contact
 * @param oldManifold
 */
public void preSolve(Contact contact, Manifold oldManifold);
 
Example #28
Source File: ContactListener.java    From jbox2d with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * Called when two fixtures cease to touch.
 * @param contact
 */
public void endContact(Contact contact);
 
Example #29
Source File: World.java    From jbox2d with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * Get the world contact list. With the returned contact, use Contact.getNext to get the next
 * contact in the world list. A null contact indicates the end of the list.
 * 
 * @return the head of the world contact list.
 * @warning contacts are created and destroyed in the middle of a time step. Use ContactListener
 *          to avoid missing contacts.
 */
public Contact getContactList() {
  return m_contactManager.m_contactList;
}
 
Example #30
Source File: IWorldPool.java    From jbox2d with BSD 2-Clause "Simplified" License votes vote down vote up
public IDynamicStack<Contact> getPolyContactStack();