Java Code Examples for org.jbox2d.dynamics.Fixture#getBody()

The following examples show how to use org.jbox2d.dynamics.Fixture#getBody() . 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: PolyShapes.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public boolean reportFixture(Fixture fixture) {
  if (m_count == e_maxCount) {
    return false;
  }

  Body body = fixture.getBody();
  Shape shape = fixture.getShape();

  boolean overlap = p.getCollision().testOverlap(shape, 0, m_circle, 0, body.getTransform(),
      m_transform);

  if (overlap) {
    DrawFixture(fixture);
    ++m_count;
  }

  return true;
}
 
Example 3
Source File: RayCastTest.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public float reportFixture(Fixture fixture, Vec2 point, Vec2 normal, float fraction) {
  Body body = fixture.getBody();
  Object userData = body.getUserData();
  if (userData != null) {
    int index = (Integer) userData;
    if (index == 0) {
      // filter
      return -1f;
    }
  }

  m_hit = true;
  m_point = point;
  m_normal = normal;
  return fraction;
}
 
Example 4
Source File: RayCastTest.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public float reportFixture(Fixture fixture, Vec2 point, Vec2 normal, float fraction) {
  Body body = fixture.getBody();
  Object userData = body.getUserData();
  if (userData != null) {
    int index = (Integer) userData;
    if (index == 0) {
      // filter
      return -1f;
    }
  }

  m_hit = true;
  m_point = point;
  m_normal = normal;
  return 0f;
}
 
Example 5
Source File: RayCastTest.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public float reportFixture(Fixture fixture, Vec2 point, Vec2 normal, float fraction) {
  Body body = fixture.getBody();
  int index = 0;
  Object userData = body.getUserData();
  if (userData != null) {
    index = (Integer) userData;
    if (index == 0) {
      // filter
      return -1f;
    }
  }

  assert (m_count < e_maxCount);

  m_points[m_count].set(point);
  m_normals[m_count].set(normal);
  ++m_count;

  if (m_count == e_maxCount) {
    return 0f;
  }

  return 1f;
}
 
Example 6
Source File: TestbedTest.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public boolean reportFixture(Fixture argFixture) {
  Body body = argFixture.getBody();
  if (body.getType() == BodyType.DYNAMIC) {
    boolean inside = argFixture.testPoint(point);
    if (inside) {
      fixture = argFixture;

      return false;
    }
  }

  return true;
}
 
Example 7
Source File: ParticleSystem.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public boolean reportFixture(Fixture fixture) {
  if (fixture.isSensor()) {
    return true;
  }
  final Shape shape = fixture.getShape();
  Body b = fixture.getBody();
  Vec2 bp = b.getWorldCenter();
  float bm = b.getMass();
  float bI = b.getInertia() - bm * b.getLocalCenter().lengthSquared();
  float invBm = bm > 0 ? 1 / bm : 0;
  float invBI = bI > 0 ? 1 / bI : 0;
  int childCount = shape.getChildCount();
  for (int childIndex = 0; childIndex < childCount; childIndex++) {
    AABB aabb = fixture.getAABB(childIndex);
    final float aabblowerBoundx = aabb.lowerBound.x - system.m_particleDiameter;
    final float aabblowerBoundy = aabb.lowerBound.y - system.m_particleDiameter;
    final float aabbupperBoundx = aabb.upperBound.x + system.m_particleDiameter;
    final float aabbupperBoundy = aabb.upperBound.y + system.m_particleDiameter;
    int firstProxy =
        lowerBound(
            system.m_proxyBuffer,
            system.m_proxyCount,
            computeTag(system.m_inverseDiameter * aabblowerBoundx, system.m_inverseDiameter
                * aabblowerBoundy));
    int lastProxy =
        upperBound(
            system.m_proxyBuffer,
            system.m_proxyCount,
            computeTag(system.m_inverseDiameter * aabbupperBoundx, system.m_inverseDiameter
                * aabbupperBoundy));

    for (int proxy = firstProxy; proxy != lastProxy; ++proxy) {
      int a = system.m_proxyBuffer[proxy].index;
      Vec2 ap = system.m_positionBuffer.data[a];
      if (aabblowerBoundx <= ap.x && ap.x <= aabbupperBoundx && aabblowerBoundy <= ap.y
          && ap.y <= aabbupperBoundy) {
        float d;
        final Vec2 n = tempVec;
        d = fixture.computeDistance(ap, childIndex, n);
        if (d < system.m_particleDiameter) {
          float invAm =
              (system.m_flagsBuffer.data[a] & ParticleType.b2_wallParticle) != 0 ? 0 : system
                  .getParticleInvMass();
          final float rpx = ap.x - bp.x;
          final float rpy = ap.y - bp.y;
          float rpn = rpx * n.y - rpy * n.x;
          if (system.m_bodyContactCount >= system.m_bodyContactCapacity) {
            int oldCapacity = system.m_bodyContactCapacity;
            int newCapacity =
                system.m_bodyContactCount != 0
                    ? 2 * system.m_bodyContactCount
                    : Settings.minParticleBufferCapacity;
            system.m_bodyContactBuffer =
                BufferUtils.reallocateBuffer(ParticleBodyContact.class,
                    system.m_bodyContactBuffer, oldCapacity, newCapacity);
            system.m_bodyContactCapacity = newCapacity;
          }
          ParticleBodyContact contact = system.m_bodyContactBuffer[system.m_bodyContactCount];
          contact.index = a;
          contact.body = b;
          contact.weight = 1 - d * system.m_inverseDiameter;
          contact.normal.x = -n.x;
          contact.normal.y = -n.y;
          contact.mass = 1 / (invAm + invBm + invBI * rpn * rpn);
          system.m_bodyContactCount++;
        }
      }
    }
  }
  return true;
}
 
Example 8
Source File: ParticleSystem.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public boolean reportFixture(Fixture fixture) {
  if (fixture.isSensor()) {
    return true;
  }
  final Shape shape = fixture.getShape();
  Body body = fixture.getBody();
  int childCount = shape.getChildCount();
  for (int childIndex = 0; childIndex < childCount; childIndex++) {
    AABB aabb = fixture.getAABB(childIndex);
    final float aabblowerBoundx = aabb.lowerBound.x - system.m_particleDiameter;
    final float aabblowerBoundy = aabb.lowerBound.y - system.m_particleDiameter;
    final float aabbupperBoundx = aabb.upperBound.x + system.m_particleDiameter;
    final float aabbupperBoundy = aabb.upperBound.y + system.m_particleDiameter;
    int firstProxy =
        lowerBound(
            system.m_proxyBuffer,
            system.m_proxyCount,
            computeTag(system.m_inverseDiameter * aabblowerBoundx, system.m_inverseDiameter
                * aabblowerBoundy));
    int lastProxy =
        upperBound(
            system.m_proxyBuffer,
            system.m_proxyCount,
            computeTag(system.m_inverseDiameter * aabbupperBoundx, system.m_inverseDiameter
                * aabbupperBoundy));

    for (int proxy = firstProxy; proxy != lastProxy; ++proxy) {
      int a = system.m_proxyBuffer[proxy].index;
      Vec2 ap = system.m_positionBuffer.data[a];
      if (aabblowerBoundx <= ap.x && ap.x <= aabbupperBoundx && aabblowerBoundy <= ap.y
          && ap.y <= aabbupperBoundy) {
        Vec2 av = system.m_velocityBuffer.data[a];
        final Vec2 temp = tempVec;
        Transform.mulTransToOutUnsafe(body.m_xf0, ap, temp);
        Transform.mulToOutUnsafe(body.m_xf, temp, input.p1);
        input.p2.x = ap.x + step.dt * av.x;
        input.p2.y = ap.y + step.dt * av.y;
        input.maxFraction = 1;
        if (fixture.raycast(output, input, childIndex)) {
          final Vec2 p = tempVec;
          p.x =
              (1 - output.fraction) * input.p1.x + output.fraction * input.p2.x
                  + Settings.linearSlop * output.normal.x;
          p.y =
              (1 - output.fraction) * input.p1.y + output.fraction * input.p2.y
                  + Settings.linearSlop * output.normal.y;

          final float vx = step.inv_dt * (p.x - ap.x);
          final float vy = step.inv_dt * (p.y - ap.y);
          av.x = vx;
          av.y = vy;
          final float particleMass = system.getParticleMass();
          final float ax = particleMass * (av.x - vx);
          final float ay = particleMass * (av.y - vy);
          Vec2 b = output.normal;
          final float fdn = ax * b.x + ay * b.y;
          final Vec2 f = tempVec2;
          f.x = fdn * b.x;
          f.y = fdn * b.y;
          body.applyLinearImpulse(f, p, true);
        }
      }
    }
  }
  return true;
}