org.jbox2d.dynamics.Fixture Java Examples

The following examples show how to use org.jbox2d.dynamics.Fixture. 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: PbSerializer.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public PbFixture.Builder serializeFixture(Fixture argFixture) {
  final PbFixture.Builder builder = PbFixture.newBuilder();
  if (signer != null) {
    Long tag = signer.getTag(argFixture);
    if (tag != null) {
      builder.setTag(tag);
    }
  }

  builder.setDensity(argFixture.m_density);
  builder.setFriction(argFixture.m_friction);
  builder.setRestitution(argFixture.m_restitution);
  builder.setSensor(argFixture.m_isSensor);

  builder.setShape(serializeShape(argFixture.m_shape));

  builder.setFilter(serializeFilter(argFixture.m_filter));
  return builder;
}
 
Example #3
Source File: PbDeserializer.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public Fixture deserializeFixture(Body argBody, PbFixture argFixture) {
  PbFixture f = argFixture;

  FixtureDef fd = new FixtureDef();
  fd.density = f.getDensity();
  fd.filter.categoryBits = f.getFilter().getCategoryBits();
  fd.filter.groupIndex = f.getFilter().getGroupIndex();
  fd.filter.maskBits = f.getFilter().getMaskBits();
  fd.friction = f.getFriction();
  fd.isSensor = f.getSensor();
  fd.restitution = f.getRestitution();
  fd.shape = deserializeShape(f.getShape());

  Fixture fixture = argBody.createFixture(fd);
  if (listener != null && f.hasTag()) {
    listener.processFixture(fixture, f.getTag());
  }
  return fixture;
}
 
Example #4
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 #5
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 #6
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 #7
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 #8
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 #9
Source File: ConveyorBelt.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public Long getTag(Fixture argFixture) {
  if (argFixture == m_platform) {
    return platformTag;
  }
  return super.getTag(argFixture);
}
 
Example #10
Source File: EdgeShapes.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public float reportFixture(Fixture fixture, final Vec2 point, final Vec2 normal, float fraction) {
  m_fixture = fixture;
  m_point = point;
  m_normal = normal;

  return fraction;
}
 
Example #11
Source File: ConveyorBelt.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void processFixture(Fixture argFixture, Long argTag) {
  if(argTag == platformTag) {
    m_platform = argFixture;
    return;
  }
  super.processFixture(argFixture, argTag);
}
 
Example #12
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 #13
Source File: PolyShapes.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
void DrawFixture(Fixture fixture) {
  Color3f color = new Color3f(0.95f, 0.95f, 0.6f);
  final Transform xf = fixture.getBody().getTransform();

  switch (fixture.getType()) {
    case CIRCLE: {
      CircleShape circle = (CircleShape) fixture.getShape();

      Vec2 center = Transform.mul(xf, circle.m_p);
      float radius = circle.m_radius;

      debugDraw.drawCircle(center, radius, color);
    }
      break;

    case POLYGON: {
      PolygonShape poly = (PolygonShape) fixture.getShape();
      int vertexCount = poly.m_count;
      assert (vertexCount <= Settings.maxPolygonVertices);
      Vec2 vertices[] = new Vec2[Settings.maxPolygonVertices];

      for (int i = 0; i < vertexCount; ++i) {
        vertices[i] = Transform.mul(xf, poly.m_vertices[i]);
      }

      debugDraw.drawPolygon(vertices, vertexCount, color);
    }
      break;
    default:
      break;
  }
}
 
Example #14
Source File: JBox2DSimulation.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private static void render() {
    renderingContext2D.setFillStyle("white");
    renderingContext2D.setStrokeStyle("black");
    renderingContext2D.fillRect(0, 0, 600, 600);
    renderingContext2D.save();
    renderingContext2D.translate(0, 600);
    renderingContext2D.scale(1, -1);
    renderingContext2D.scale(100, 100);
    renderingContext2D.setLineWidth(0.01f);
    for (Body body = scene.getWorld().getBodyList(); body != null; body = body.getNext()) {
        final Vec2 center = body.getPosition();
        renderingContext2D.save();
        renderingContext2D.translate(center.x, center.y);
        renderingContext2D.rotate(body.getAngle());
        for (Fixture fixture = body.getFixtureList(); fixture != null; fixture = fixture.getNext()) {
            final Shape shape = fixture.getShape();
            if (shape.getType() == ShapeType.CIRCLE) {
                final CircleShape circle = (CircleShape) shape;
                renderingContext2D.beginPath();
                renderingContext2D.arc(circle.m_p.x, circle.m_p.y, circle.getRadius(), 0, Math.PI * 2, true);
                renderingContext2D.closePath();
                renderingContext2D.stroke();
            } else if (shape.getType() == ShapeType.POLYGON) {
                final PolygonShape poly = (PolygonShape) shape;
                final Vec2[] vertices = poly.getVertices();
                renderingContext2D.beginPath();
                renderingContext2D.moveTo(vertices[0].x, vertices[0].y);
                for (int i = 1; i < poly.getVertexCount(); ++i) {
                    renderingContext2D.lineTo(vertices[i].x, vertices[i].y);
                }
                renderingContext2D.closePath();
                renderingContext2D.stroke();
            }
        }
        renderingContext2D.restore();
    }
    renderingContext2D.restore();
}
 
Example #15
Source File: OneSidedTest.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public Long getTag(Fixture fixture) {
  if (fixture == m_platform)
    return PLATFORM_TAG;
  if (fixture == m_character)
    return CHARACTER_TAG;
  return super.getTag(fixture);
}
 
Example #16
Source File: OneSidedTest.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void processFixture(Fixture fixture, Long tag) {
  if (tag == PLATFORM_TAG) {
    m_platform = fixture;
  } else if (tag == CHARACTER_TAG) {
    m_character = fixture;
  } else {
    super.processFixture(fixture, tag);
  }
}
 
Example #17
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 #18
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 #19
Source File: PbSerializer.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public SerializationResult serialize(Fixture argFixture) {
  final PbFixture fixture = serializeFixture(argFixture).build();
  return new SerializationResult() {
    @Override
    public void writeTo(OutputStream argOutputStream) throws IOException {
      fixture.writeTo(argOutputStream);
    }

    @Override
    public Object getValue() {
      return fixture;
    }
  };
}
 
Example #20
Source File: Contact.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** initialization for pooling */
public void init(Fixture fA, int indexA, Fixture fB, int indexB) {
  m_flags = ENABLED_FLAG;

  m_fixtureA = fA;
  m_fixtureB = fB;

  m_indexA = indexA;
  m_indexB = indexB;

  m_manifold.pointCount = 0;

  m_prev = null;
  m_next = null;

  m_nodeA.contact = null;
  m_nodeA.prev = null;
  m_nodeA.next = null;
  m_nodeA.other = null;

  m_nodeB.contact = null;
  m_nodeB.prev = null;
  m_nodeB.next = null;
  m_nodeB.other = null;

  m_toiCount = 0;
  m_friction = Contact.mixFriction(fA.m_friction, fB.m_friction);
  m_restitution = Contact.mixRestitution(fA.m_restitution, fB.m_restitution);

  m_tangentSpeed = 0;
}
 
Example #21
Source File: ContactFilter.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Return true if contact calculations should be performed between these two shapes.
 * @warning for performance reasons this is only called when the AABBs begin to overlap.
 * @param fixtureA
 * @param fixtureB
 * @return
 */
public boolean shouldCollide(Fixture fixtureA, Fixture fixtureB){
	Filter filterA = fixtureA.getFilterData();
	Filter filterB = fixtureB.getFilterData();

	if (filterA.groupIndex == filterB.groupIndex && filterA.groupIndex != 0){
		return filterA.groupIndex > 0;
	}

	boolean collide = (filterA.maskBits & filterB.categoryBits) != 0 &&
					  (filterA.categoryBits & filterB.maskBits) != 0;
	return collide;
}
 
Example #22
Source File: PolygonContact.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void init(Fixture fixtureA, Fixture fixtureB) {
  super.init(fixtureA, 0, fixtureB, 0);
  assert (m_fixtureA.getType() == ShapeType.POLYGON);
  assert (m_fixtureB.getType() == ShapeType.POLYGON);
}
 
Example #23
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 #24
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;
}
 
Example #25
Source File: PbSerializer.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public PbBody.Builder serializeBody(Body argBody) {
  PbBody.Builder builder = PbBody.newBuilder();
  if (signer != null) {
    Long id = signer.getTag(argBody);
    if (id != null) {
      builder.setTag(id);
    }
  }
  switch (argBody.getType()) {
    case DYNAMIC:
      builder.setType(PbBodyType.DYNAMIC);
      break;
    case KINEMATIC:
      builder.setType(PbBodyType.KINEMATIC);
      break;
    case STATIC:
      builder.setType(PbBodyType.STATIC);
      break;
    default:
      UnsupportedObjectException e = new UnsupportedObjectException("Unknown body type: "
          + argBody.getType(), Type.BODY);
      if (listener == null || listener.isUnsupported(e)) {
        throw e;
      }
      return null;
  }
  builder.setPosition(vecToPb(argBody.getPosition()));
  builder.setAngle(argBody.getAngle());
  builder.setLinearVelocity(vecToPb(argBody.getLinearVelocity()));
  builder.setAngularVelocity(argBody.getAngularVelocity());
  builder.setLinearDamping(argBody.getLinearDamping());
  builder.setAngularDamping(argBody.getAngularDamping());
  builder.setGravityScale(argBody.getGravityScale());

  builder.setBullet(argBody.isBullet());
  builder.setAllowSleep(argBody.isSleepingAllowed());
  builder.setAwake(argBody.isAwake());
  builder.setActive(argBody.isActive());
  builder.setFixedRotation(argBody.isFixedRotation());

  Fixture curr = argBody.m_fixtureList;
  while (curr != null) {
    builder.addFixtures(serializeFixture(curr));
    curr = curr.m_next;
  }

  return builder;
}
 
Example #26
Source File: PbDeserializer.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public Fixture deserializeFixture(Body argBody, InputStream argInput) throws IOException {
  PbFixture fixture = PbFixture.parseFrom(argInput);
  return deserializeFixture(argBody, fixture);
}
 
Example #27
Source File: TestbedTest.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void processFixture(Fixture argFixture, Long argTag) {
  listener.processFixture(argFixture, argTag);
}
 
Example #28
Source File: TestbedTest.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public Long getTag(Fixture argFixture) {
  return delegate.getTag(argFixture);
}
 
Example #29
Source File: TestbedTest.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void processFixture(Fixture fixture, Long tag) {}
 
Example #30
Source File: TestbedTest.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public Long getTag(Fixture fixture) {
  return null;
}