org.jbox2d.collision.shapes.CircleShape Java Examples

The following examples show how to use org.jbox2d.collision.shapes.CircleShape. 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: ConfinedTest.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void createCircle()
{
	float radius = 2.0f;
	CircleShape shape = new CircleShape();
	shape.m_p.setZero();
	shape.m_radius = radius;

	FixtureDef fd = new FixtureDef();
	fd.shape = shape;
	fd.density = 1.0f;
	fd.friction = 0.0f;

	Vec2 p = new Vec2((float)Math.random(), 3.0f + (float)Math.random());
	BodyDef bd = new BodyDef();
	bd.type = BodyType.DYNAMIC;
	bd.position = p;
	//bd.allowSleep = false;
	Body body = getWorld().createBody(bd);

	body.createFixture(fd);
}
 
Example #2
Source File: ShapeEditing.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void keyPressed(char key, int argKeyCode) {
  switch (key) {
    case 'c':
      if (m_fixture2 == null) {
        CircleShape shape = new CircleShape();
        shape.m_radius = 3.0f;
        shape.m_p.set(0.5f, -4.0f);
        m_fixture2 = m_body.createFixture(shape, 10.0f);
        m_body.setAwake(true);
      }
      break;

    case 'd':
      if (m_fixture2 != null) {
        m_body.destroyFixture(m_fixture2);
        m_fixture2 = null;
        m_body.setAwake(true);
      }
      break;
  }
}
 
Example #3
Source File: Ball.java    From Form-N-Fun with MIT License 6 votes vote down vote up
public Ball(float _x,float _y,float _r, Createbox2d _box2d) //used to create balls
{
    x = _x;
    y = _y;
    r = _r;
    box2d = _box2d;
    BodyDef bd = new BodyDef();// define body
    bd.type = BodyType.DYNAMIC; //make body dynamic
    bd.position.set(box2d.coordPixelsToWorld(x,y)); //intial position to ball center
    b1 = box2d.createBody(bd); //create body
    CircleShape cs = new CircleShape(); //create box2d circle shape
    cs.m_radius = box2d.scalarPixelsToWorld(r);//cricle radius to ball radius
    FixtureDef fd = new FixtureDef();// body properites
    fd.shape = cs;
    fd.density = 1; //density to 1
    fd.friction = 0.2f;//0.2f;//0.0f;
    fd.restitution = 0.5f;//0.7f;//1.0f;
    b1.createFixture(fd); //add properties to body
    b1.setUserData(this);
}
 
Example #4
Source File: JBox2DTest.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
@Test
public void testNewWorld() {
    World world = new World(new Vec2(0, -9.8f));

    BodyDef axisDef = new BodyDef();
    axisDef.type = BodyType.STATIC;
    axisDef.position = new Vec2(3, 3);
    Body axis = world.createBody(axisDef);

    CircleShape axisShape = new CircleShape();
    axisShape.setRadius(0.02f);
    axisShape.m_p.set(0, 0);

    //FixtureDef axisFixture = new FixtureDef();
    //axisFixture.shape = axisShape;
    //axis.createFixture(axisFixture);

}
 
Example #5
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 #6
Source File: SensorTest.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void step(TestbedSettings settings) {
  // TODO Auto-generated method stub
  super.step(settings);

  // Traverse the contact results. Apply a force on shapes
  // that overlap the sensor.
  for (int i = 0; i < e_count; ++i) {
    if (m_touching[i].tf == false) {
      continue;
    }

    Body body = m_bodies[i];
    Body ground = m_sensor.getBody();

    CircleShape circle = (CircleShape) m_sensor.getShape();
    Vec2 center = ground.getWorldPoint(circle.m_p);

    Vec2 position = body.getPosition();

    Vec2 d = center.sub(position);
    if (d.lengthSquared() < Settings.EPSILON * Settings.EPSILON) {
      continue;
    }

    d.normalize();
    Vec2 F = d.mulLocal(100f);
    body.applyForce(F, position);
  }
}
 
Example #7
Source File: ChainAndCircleContact.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void evaluate(Manifold manifold, Transform xfA, Transform xfB) {
  ChainShape chain = (ChainShape) m_fixtureA.getShape();
  chain.getChildEdge(edge, m_indexA);
  pool.getCollision().collideEdgeAndCircle(manifold, edge, xfA,
      (CircleShape) m_fixtureB.getShape(), xfB);
}
 
Example #8
Source File: VerticalStack.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void keyPressed(char argKeyChar, int argKeyCode) {
  switch (argKeyChar) {
    case ',':
      if (m_bullet != null) {
        getWorld().destroyBody(m_bullet);
        m_bullet = null;
      }

      {
        CircleShape shape = new CircleShape();
        shape.m_radius = 0.25f;

        FixtureDef fd = new FixtureDef();
        fd.shape = shape;
        fd.density = 20.0f;
        fd.restitution = 0.05f;

        BodyDef bd = new BodyDef();
        bd.type = BodyType.DYNAMIC;
        bd.bullet = true;
        bd.position.set(-31.0f, 5.0f);

        m_bullet = getWorld().createBody(bd);
        m_bullet.createFixture(fd);

        m_bullet.setLinearVelocity(new Vec2(400.0f, 0.0f));
      }
      break;
  }
}
 
Example #9
Source File: TestbedTest.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void launchBomb(Vec2 position, Vec2 velocity) {
  if (bomb != null) {
    m_world.destroyBody(bomb);
    bomb = null;
  }
  // todo optimize this
  BodyDef bd = new BodyDef();
  bd.type = BodyType.DYNAMIC;
  bd.position.set(position);
  bd.bullet = true;
  bomb = m_world.createBody(bd);
  bomb.setLinearVelocity(velocity);

  CircleShape circle = new CircleShape();
  circle.m_radius = 0.3f;

  FixtureDef fd = new FixtureDef();
  fd.shape = circle;
  fd.density = 20f;
  fd.restitution = 0;

  Vec2 minV = new Vec2(position);
  Vec2 maxV = new Vec2(position);

  minV.subLocal(new Vec2(.3f, .3f));
  maxV.addLocal(new Vec2(.3f, .3f));

  aabb.lowerBound.set(minV);
  aabb.upperBound.set(maxV);

  bomb.createFixture(fd);
}
 
Example #10
Source File: Collision.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Compute the collision manifold between two circles.
 * 
 * @param manifold
 * @param circle1
 * @param xfA
 * @param circle2
 * @param xfB
 */
public final void collideCircles(Manifold manifold, final CircleShape circle1,
    final Transform xfA, final CircleShape circle2, final Transform xfB) {
  manifold.pointCount = 0;
  // before inline:
  // Transform.mulToOut(xfA, circle1.m_p, pA);
  // Transform.mulToOut(xfB, circle2.m_p, pB);
  // d.set(pB).subLocal(pA);
  // float distSqr = d.x * d.x + d.y * d.y;

  // after inline:
  Vec2 circle1p = circle1.m_p;
  Vec2 circle2p = circle2.m_p;
  float pAx = (xfA.q.c * circle1p.x - xfA.q.s * circle1p.y) + xfA.p.x;
  float pAy = (xfA.q.s * circle1p.x + xfA.q.c * circle1p.y) + xfA.p.y;
  float pBx = (xfB.q.c * circle2p.x - xfB.q.s * circle2p.y) + xfB.p.x;
  float pBy = (xfB.q.s * circle2p.x + xfB.q.c * circle2p.y) + xfB.p.y;
  float dx = pBx - pAx;
  float dy = pBy - pAy;
  float distSqr = dx * dx + dy * dy;
  // end inline

  final float radius = circle1.m_radius + circle2.m_radius;
  if (distSqr > radius * radius) {
    return;
  }

  manifold.type = ManifoldType.CIRCLES;
  manifold.localPoint.set(circle1p);
  manifold.localNormal.setZero();
  manifold.pointCount = 1;

  manifold.points[0].localPoint.set(circle2p);
  manifold.points[0].id.zero();
}
 
Example #11
Source File: JBox2DTest.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimpleBall() {
    World world = new World(new Vec2(0, -9.8f));

    float ballRadius = 0.15f;

    BodyDef ballDef = new BodyDef();
    ballDef.type = BodyType.DYNAMIC;
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.friction = 0.3f;
    fixtureDef.restitution = 0.3f;
    fixtureDef.density = 0.2f;
    CircleShape shape = new CircleShape();
    shape.m_radius = ballRadius;
    fixtureDef.shape = shape;

    int i=0;
    int j=0;

    float x = (j + 0.5f) * (ballRadius * 2 + 0.01f);
    float y = (i + 0.5f) * (ballRadius * 2 + 0.01f);
    ballDef.position.x = 3 + x;
    ballDef.position.y = 3 + y;
    Body theBall = world.createBody(ballDef);
    theBall.createFixture(fixtureDef);

    for (int k=0;k<100;k++) {
        world.step(0.01f, 20, 40);
    }

    Vec2 thePosition = theBall.getPosition();
    int theX = (int)(thePosition.x * 1000);
    int theY = (int) (thePosition.y * 1000);

    System.out.println("Finally ended at ");
    System.out.println(theX);
    System.out.println(theY);
}
 
Example #12
Source File: JBox2DTest.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private void initBalls() {
    float ballRadius = 0.15f;

    BodyDef ballDef = new BodyDef();
    ballDef.type = BodyType.DYNAMIC;
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.friction = 0.3f;
    fixtureDef.restitution = 0.3f;
    fixtureDef.density = 0.2f;
    CircleShape shape = new CircleShape();
    shape.m_radius = ballRadius;
    fixtureDef.shape = shape;

    for (int i = 0; i < 5; ++i) {
        for (int j = 0; j < 5; ++j) {
            float x = (j + 0.5f) * (ballRadius * 2 + 0.01f);
            float y = (i + 0.5f) * (ballRadius * 2 + 0.01f);
            ballDef.position.x = 3 + x;
            ballDef.position.y = 3 + y;
            Body body = world.createBody(ballDef);
            body.createFixture(fixtureDef);

            ballDef.position.x = 3 - x;
            ballDef.position.y = 3 + y;
            body = world.createBody(ballDef);
            body.createFixture(fixtureDef);

            ballDef.position.x = 3 + x;
            ballDef.position.y = 3 - y;
            body = world.createBody(ballDef);
            body.createFixture(fixtureDef);

            ballDef.position.x = 3 - x;
            ballDef.position.y = 3 - y;
            body = world.createBody(ballDef);
            body.createFixture(fixtureDef);
        }
    }
}
 
Example #13
Source File: JBox2DTest.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private void initAxis() {
    BodyDef axisDef = new BodyDef();
    axisDef.type = BodyType.STATIC;
    axisDef.position = new Vec2(3, 3);
    axis = world.createBody(axisDef);

    CircleShape axisShape = new CircleShape();
    axisShape.setRadius(0.02f);
    axisShape.m_p.set(0, 0);

    FixtureDef axisFixture = new FixtureDef();
    axisFixture.shape = axisShape;
    axis.createFixture(axisFixture);
}
 
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: JBox2DSimulation.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private void initBalls() {
    final float ballRadius = 0.15f;

    final BodyDef ballDef = new BodyDef();
    ballDef.type = BodyType.DYNAMIC;
    final FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.friction = 0.3f;
    fixtureDef.restitution = 0.3f;
    fixtureDef.density = 0.2f;
    final CircleShape shape = new CircleShape();
    shape.m_radius = ballRadius;
    fixtureDef.shape = shape;

    for (int i = 0; i < 6; ++i) {
        for (int j = 0; j < 6; ++j) {
            final float x = (j + 0.5f) * (ballRadius * 2 + 0.01f);
            final float y = (i + 0.5f) * (ballRadius * 2 + 0.01f);
            ballDef.position.x = 3 + x;
            ballDef.position.y = 3 + y;
            Body body = world.createBody(ballDef);
            body.createFixture(fixtureDef);

            ballDef.position.x = 3 - x;
            ballDef.position.y = 3 + y;
            body = world.createBody(ballDef);
            body.createFixture(fixtureDef);

            ballDef.position.x = 3 + x;
            ballDef.position.y = 3 - y;
            body = world.createBody(ballDef);
            body.createFixture(fixtureDef);

            ballDef.position.x = 3 - x;
            ballDef.position.y = 3 - y;
            body = world.createBody(ballDef);
            body.createFixture(fixtureDef);
        }
    }
}
 
Example #16
Source File: JBox2DSimulation.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private void initAxis() {
    final BodyDef axisDef = new BodyDef();
    axisDef.type = BodyType.STATIC;
    axisDef.position = new Vec2(3, 3);
    axis = world.createBody(axisDef);

    final CircleShape axisShape = new CircleShape();
    axisShape.setRadius(0.02f);
    axisShape.m_p.set(0, 0);

    final FixtureDef axisFixture = new FixtureDef();
    axisFixture.shape = axisShape;
    axis.createFixture(axisFixture);
}
 
Example #17
Source File: Box2d.java    From j2cl with Apache License 2.0 5 votes vote down vote up
private void spawnBall() {
  float dx = (float) Math.random() * 10f;
  float dy = (float) Math.random() * 10f - 5f;
  // Create a Ball.
  BodyDef ballBodyDef = new BodyDef();
  ballBodyDef.type = BodyType.DYNAMIC;
  ballBodyDef.position.set(5 + dx, 5 + dy); // Centre of the ground box.

  // Create the body for the ball within the World.
  Body ball = world.createBody(ballBodyDef);

  float radius = 0.5f + (float) Math.random();

  // Create the actual fixture representing the box.
  CircleShape ballShape = new CircleShape();
  ballShape.m_radius = radius; // Diameter of 1m.
  // ballShape.m_p is the offset relative to body. Default of (0,0)

  FixtureDef ballFixtureDef = new FixtureDef();
  ballFixtureDef.density = 1.0f; // Must have a density or else it won't
  // be affected by gravity.
  ballFixtureDef.restitution = 0.4f; // Define how bouncy the ball is.
  ballFixtureDef.friction = 0.2f;
  ballFixtureDef.shape = ballShape;

  // Add the fixture to the ball body.
  ball.createFixture(ballFixtureDef);
}
 
Example #18
Source File: MoBike.java    From kAndroid with Apache License 2.0 4 votes vote down vote up
private Shape createCircleBody(View childView) {
    CircleShape circleShape = new CircleShape();
    //半径为 宽、高的一半
    circleShape.setRadius(mappingView2Body(childView.getHeight() / 2));
    return circleShape;
}
 
Example #19
Source File: PbDeserializer.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public Shape deserializeShape(PbShape argShape) {
  PbShape s = argShape;

  Shape shape = null;
  switch (s.getType()) {
    case CIRCLE:
      CircleShape c = new CircleShape();
      c.m_p.set(pbToVec(s.getCenter()));
      shape = c;
      break;
    case POLYGON:
      PolygonShape p = new PolygonShape();
      p.m_centroid.set(pbToVec(s.getCentroid()));
      p.m_count = s.getPointsCount();
      for (int i = 0; i < p.m_count; i++) {
        p.m_vertices[i].set(pbToVec(s.getPoints(i)));
        p.m_normals[i].set(pbToVec(s.getNormals(i)));
      }
      shape = p;
      break;
    case EDGE:
      EdgeShape edge = new EdgeShape();
      edge.m_vertex0.set(pbToVec(s.getV0()));
      edge.m_vertex1.set(pbToVec(s.getV1()));
      edge.m_vertex2.set(pbToVec(s.getV2()));
      edge.m_vertex3.set(pbToVec(s.getV3()));
      edge.m_hasVertex0 = s.getHas0();
      edge.m_hasVertex3 = s.getHas3();
      shape = edge;
      break;
    case CHAIN: {
      ChainShape chain = new ChainShape();
      chain.m_count = s.getPointsCount();
      chain.m_vertices = new Vec2[chain.m_count];
      for (int i = 0; i < chain.m_count; i++) {
        chain.m_vertices[i] = new Vec2(pbToVec(s.getPoints(i)));
      }
      chain.m_hasPrevVertex = s.getHas0();
      chain.m_hasNextVertex = s.getHas3();
      chain.m_prevVertex.set(pbToVec(s.getPrev()));
      chain.m_nextVertex.set(pbToVec(s.getNext()));
      shape = chain;
      break;
    }
    default: {
      UnsupportedObjectException e =
          new UnsupportedObjectException("Unknown shape type: " + s.getType(), Type.SHAPE);
      if (ulistener == null || ulistener.isUnsupported(e)) {
        throw e;
      }
      return null;
    }
  }
  shape.m_radius = s.getRadius();

  if (listener != null && s.hasTag()) {
    listener.processShape(shape, s.getTag());
  }
  return shape;
}
 
Example #20
Source File: PbSerializer.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public PbShape.Builder serializeShape(Shape argShape) {
  final PbShape.Builder builder = PbShape.newBuilder();
  if (signer != null) {
    Long tag = signer.getTag(argShape);
    if (tag != null) {
      builder.setTag(tag);
    }
  }
  builder.setRadius(argShape.m_radius);

  switch (argShape.m_type) {
    case CIRCLE:
      CircleShape c = (CircleShape) argShape;
      builder.setType(PbShapeType.CIRCLE);
      builder.setCenter(vecToPb(c.m_p));
      break;
    case POLYGON:
      PolygonShape p = (PolygonShape) argShape;
      builder.setType(PbShapeType.POLYGON);
      builder.setCentroid(vecToPb(p.m_centroid));
      for (int i = 0; i < p.m_count; i++) {
        builder.addPoints(vecToPb(p.m_vertices[i]));
        builder.addNormals(vecToPb(p.m_normals[i]));
      }
      break;
    case EDGE:
      EdgeShape e = (EdgeShape) argShape;
      builder.setType(PbShapeType.EDGE);
      builder.setV0(vecToPb(e.m_vertex0));
      builder.setV1(vecToPb(e.m_vertex1));
      builder.setV2(vecToPb(e.m_vertex2));
      builder.setV3(vecToPb(e.m_vertex3));
      builder.setHas0(e.m_hasVertex0);
      builder.setHas3(e.m_hasVertex3);
      break;
    case CHAIN:
      ChainShape h = (ChainShape) argShape;
      builder.setType(PbShapeType.CHAIN);
      for (int i = 0; i < h.m_count; i++) {
        builder.addPoints(vecToPb(h.m_vertices[i]));
      }
      builder.setPrev(vecToPb(h.m_prevVertex));
      builder.setNext(vecToPb(h.m_nextVertex));
      builder.setHas0(h.m_hasPrevVertex);
      builder.setHas3(h.m_hasNextVertex);
      break;
    default:
      UnsupportedObjectException ex = new UnsupportedObjectException(
          "Currently only encodes circle and polygon shapes", Type.SHAPE);
      if (listener == null || listener.isUnsupported(ex)) {
        throw ex;
      }
      return null;
  }

  return builder;
}
 
Example #21
Source File: CollisionProcessing.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void initTest(boolean deserialized) {
  if (deserialized) {
    return;
  }
  // Ground body
  {
    EdgeShape shape = new EdgeShape();
    shape.set(new Vec2(-50.0f, 0.0f), new Vec2(50.0f, 0.0f));

    FixtureDef sd = new FixtureDef();
    sd.shape = shape;

    BodyDef bd = new BodyDef();
    Body ground = getWorld().createBody(bd);
    ground.createFixture(sd);
  }

  float xLo = -5.0f, xHi = 5.0f;
  float yLo = 2.0f, yHi = 35.0f;

  // Small triangle
  Vec2 vertices[] = new Vec2[3];
  vertices[0] = new Vec2(-1.0f, 0.0f);
  vertices[1] = new Vec2(1.0f, 0.0f);
  vertices[2] = new Vec2(0.0f, 2.0f);

  PolygonShape polygon = new PolygonShape();
  polygon.set(vertices, 3);

  FixtureDef triangleShapeDef = new FixtureDef();
  triangleShapeDef.shape = polygon;
  triangleShapeDef.density = 1.0f;

  BodyDef triangleBodyDef = new BodyDef();
  triangleBodyDef.type = BodyType.DYNAMIC;
  triangleBodyDef.position.set(MathUtils.randomFloat(xLo, xHi), MathUtils.randomFloat(yLo, yHi));

  Body body1 = getWorld().createBody(triangleBodyDef);
  body1.createFixture(triangleShapeDef);

  // Large triangle (recycle definitions)
  vertices[0].mulLocal(2.0f);
  vertices[1].mulLocal(2.0f);
  vertices[2].mulLocal(2.0f);
  polygon.set(vertices, 3);

  triangleBodyDef.position.set(MathUtils.randomFloat(xLo, xHi), MathUtils.randomFloat(yLo, yHi));

  Body body2 = getWorld().createBody(triangleBodyDef);
  body2.createFixture(triangleShapeDef);

  // Small box
  polygon.setAsBox(1.0f, 0.5f);

  FixtureDef boxShapeDef = new FixtureDef();
  boxShapeDef.shape = polygon;
  boxShapeDef.density = 1.0f;

  BodyDef boxBodyDef = new BodyDef();
  boxBodyDef.type = BodyType.DYNAMIC;
  boxBodyDef.position.set(MathUtils.randomFloat(xLo, xHi), MathUtils.randomFloat(yLo, yHi));

  Body body3 = getWorld().createBody(boxBodyDef);
  body3.createFixture(boxShapeDef);

  // Large box (recycle definitions)
  polygon.setAsBox(2.0f, 1.0f);
  boxBodyDef.position.set(MathUtils.randomFloat(xLo, xHi), MathUtils.randomFloat(yLo, yHi));

  Body body4 = getWorld().createBody(boxBodyDef);
  body4.createFixture(boxShapeDef);

  // Small circle
  CircleShape circle = new CircleShape();
  circle.m_radius = 1.0f;

  FixtureDef circleShapeDef = new FixtureDef();
  circleShapeDef.shape = circle;
  circleShapeDef.density = 1.0f;

  BodyDef circleBodyDef = new BodyDef();
  circleBodyDef.type = BodyType.DYNAMIC;
  circleBodyDef.position.set(MathUtils.randomFloat(xLo, xHi), MathUtils.randomFloat(yLo, yHi));

  Body body5 = getWorld().createBody(circleBodyDef);
  body5.createFixture(circleShapeDef);

  // Large circle
  circle.m_radius *= 2.0f;
  circleBodyDef.position.set(MathUtils.randomFloat(xLo, xHi), MathUtils.randomFloat(yLo, yHi));

  Body body6 = getWorld().createBody(circleBodyDef);
  body6.createFixture(circleShapeDef);
}
 
Example #22
Source File: EdgeAndCircleContact.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void evaluate(Manifold manifold, Transform xfA, Transform xfB) {
  pool.getCollision().collideEdgeAndCircle(manifold, (EdgeShape) m_fixtureA.getShape(), xfA,
      (CircleShape) m_fixtureB.getShape(), xfB);
}
 
Example #23
Source File: CircleContact.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void evaluate(Manifold manifold, Transform xfA, Transform xfB) {
  pool.getCollision().collideCircles(manifold, (CircleShape) m_fixtureA.getShape(), xfA,
      (CircleShape) m_fixtureB.getShape(), xfB);
}
 
Example #24
Source File: PolygonAndCircleContact.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void evaluate(Manifold manifold, Transform xfA, Transform xfB) {
  pool.getCollision().collidePolygonAndCircle(manifold, (PolygonShape) m_fixtureA.getShape(),
      xfA, (CircleShape) m_fixtureB.getShape(), xfB);
}
 
Example #25
Source File: Distance.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Initialize the proxy using the given shape. The shape must remain in scope while the proxy is
 * in use.
 */
public final void set(final Shape shape, int index) {
  switch (shape.getType()) {
    case CIRCLE:
      final CircleShape circle = (CircleShape) shape;
      m_vertices[0].set(circle.m_p);
      m_count = 1;
      m_radius = circle.m_radius;

      break;
    case POLYGON:
      final PolygonShape poly = (PolygonShape) shape;
      m_count = poly.m_count;
      m_radius = poly.m_radius;
      for (int i = 0; i < m_count; i++) {
        m_vertices[i].set(poly.m_vertices[i]);
      }
      break;
    case CHAIN:
      final ChainShape chain = (ChainShape) shape;
      assert (0 <= index && index < chain.m_count);

      m_buffer[0] = chain.m_vertices[index];
      if (index + 1 < chain.m_count) {
        m_buffer[1] = chain.m_vertices[index + 1];
      } else {
        m_buffer[1] = chain.m_vertices[0];
      }

      m_vertices[0].set(m_buffer[0]);
      m_vertices[1].set(m_buffer[1]);
      m_count = 2;
      m_radius = chain.m_radius;
      break;
    case EDGE:
      EdgeShape edge = (EdgeShape) shape;
      m_vertices[0].set(edge.m_vertex1);
      m_vertices[1].set(edge.m_vertex2);
      m_count = 2;
      m_radius = edge.m_radius;
      break;
    default:
      assert (false);
  }
}
 
Example #26
Source File: JBox2DTest.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
@Test
public void testCircleShape() {
    CircleShape axisShape = new CircleShape();
    axisShape.setRadius(0.02f);
    axisShape.m_p.set(0, 0);
}