Java Code Examples for org.jbox2d.dynamics.contacts.Contact#getFixtureA()

The following examples show how to use org.jbox2d.dynamics.contacts.Contact#getFixtureA() . 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: 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 4
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 5
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 6
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 7
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 8
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;
}