org.jbox2d.collision.shapes.PolygonShape Java Examples

The following examples show how to use org.jbox2d.collision.shapes.PolygonShape. 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: MoBike.java    From kAndroid with Apache License 2.0 6 votes vote down vote up
private void updateTopAndBottomBounds() {
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.STATIC;

    PolygonShape shape = new PolygonShape();
    float hx = mappingView2Body(width);
    float hy = mappingView2Body(proportion);
    shape.setAsBox(hx, hy);

    FixtureDef def = new FixtureDef();
    def.shape = shape;
    def.density = density;
    def.friction = frictionRatio;
    def.restitution = restitutionRatio;

    bodyDef.position.set(0, -hy);
    Body topBody = world.createBody(bodyDef);
    topBody.createFixture(def);

    bodyDef.position.set(0, mappingView2Body(height) + hy);
    Body bottomBody = world.createBody(bodyDef);
    bottomBody.createFixture(def);
}
 
Example #2
Source File: Tumbler.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public synchronized void step(TestbedSettings settings) {
  super.step(settings);

  if (m_count < MAX_NUM) {
    BodyDef bd = new BodyDef();
    bd.type = BodyType.DYNAMIC;
    bd.position.set(0.0f, 10.0f);
    Body body = m_world.createBody(bd);

    PolygonShape shape = new PolygonShape();
    shape.setAsBox(0.125f, 0.125f);
    body.createFixture(shape, 1.0f);

    ++m_count;
  }
}
 
Example #3
Source File: DominoTower.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void makeDomino(float x, float y, boolean horizontal, World world) {

    PolygonShape sd = new PolygonShape();
    sd.setAsBox(.5f * dwidth, .5f * dheight);
    FixtureDef fd = new FixtureDef();
    fd.shape = sd;
    fd.density = ddensity;
    BodyDef bd = new BodyDef();
    bd.type = BodyType.DYNAMIC;
    fd.friction = dfriction;
    fd.restitution = 0.65f;
    bd.position = new Vec2(x, y);
    bd.angle = horizontal ? (float) (Math.PI / 2.0) : 0f;
    Body myBody = getWorld().createBody(bd);
    myBody.createFixture(fd);
  }
 
Example #4
Source File: ChainAndPolygonContact.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().collideEdgeAndPolygon(manifold, edge, xfA,
      (PolygonShape) m_fixtureB.getShape(), xfB);
}
 
Example #5
Source File: DistanceTest.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void initTest(boolean argDeserialized) {
	
	input.transformA = new Transform();
	input.transformB = new Transform();
	{
		m_transformA = new Transform();
		m_transformA.setIdentity();
		m_transformA.p.set(0.0f, -0.2f);
		m_polygonA = new PolygonShape();
		m_polygonA.setAsBox(10.0f, 0.2f);
	}
	
	{
		m_positionB = new Vec2();
		m_positionB.set(12.017401f, 0.13678508f);
		m_angleB = -0.0109265f;
		
		m_transformB = new Transform();
		m_transformB.set(m_positionB, m_angleB);
		
		m_polygonB = new PolygonShape();
		m_polygonB.setAsBox(2.0f, 0.1f);
	}
	for (int i = 0; i < v.length; i++) {
		v[i] = new Vec2();
	}
}
 
Example #6
Source File: Tumbler.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void initTest(boolean deserialized) {
  {
    BodyDef bd = new BodyDef();
    bd.type = BodyType.DYNAMIC;
    bd.allowSleep = false;
    bd.position.set(0.0f, 10.0f);
    Body body = m_world.createBody(bd);

    PolygonShape shape = new PolygonShape();
    shape.setAsBox(0.5f, 10.0f, new Vec2(10.0f, 0.0f), 0.0f);
    body.createFixture(shape, 5.0f);
    shape.setAsBox(0.5f, 10.0f, new Vec2(-10.0f, 0.0f), 0.0f);
    body.createFixture(shape, 5.0f);
    shape.setAsBox(10.0f, 0.5f, new Vec2(0.0f, 10.0f), 0.0f);
    body.createFixture(shape, 5.0f);
    shape.setAsBox(10.0f, 0.5f, new Vec2(0.0f, -10.0f), 0.0f);
    body.createFixture(shape, 5.0f);

    RevoluteJointDef jd = new RevoluteJointDef();
    jd.bodyA = getGroundBody();
    jd.bodyB = body;
    jd.localAnchorA.set(0.0f, 10.0f);
    jd.localAnchorB.set(0.0f, 0.0f);
    jd.referenceAngle = 0.0f;
    jd.motorSpeed = 0.05f * MathUtils.PI;
    jd.maxMotorTorque = 1e8f;
    jd.enableMotor = true;
    m_joint = (RevoluteJoint) m_world.createJoint(jd);
  }
  m_count = 0;
}
 
Example #7
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 #8
Source File: MoBike.java    From kAndroid with Apache License 2.0 5 votes vote down vote up
private void updateLeftAndRightBounds() {
    BodyDef bodyDef = new BodyDef();
    //生成静态刚体
    bodyDef.type = BodyType.STATIC;

    //设置形状 - 多边形
    PolygonShape shape = new PolygonShape();

    //hx 1  hy 屏幕高度
    float hx = mappingView2Body(proportion);
    float hy = mappingView2Body(height);
    shape.setAsBox(hx, hy);

    //设置 系数
    FixtureDef def = new FixtureDef();
    def.shape = shape;
    def.density = density;
    def.friction = frictionRatio;
    def.restitution = restitutionRatio;

    //-1 高度
    bodyDef.position.set(-hx, hy);
    Body leftBody = world.createBody(bodyDef);
    //设置位置
    leftBody.createFixture(def);

    //这里便于理解 应该是 w + 1  h
    //而不是 w + 1 0 验证结果一样
    bodyDef.position.set(mappingView2Body(width) + hx, 0);
    Body rightBody = world.createBody(bodyDef);
    rightBody.createFixture(def);
}
 
Example #9
Source File: Collision.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Find the max separation between poly1 and poly2 using edge normals from poly1.
 * 
 * @param edgeIndex
 * @param poly1
 * @param xf1
 * @param poly2
 * @param xf2
 * @return
 */
public final void findMaxSeparation(EdgeResults results, final PolygonShape poly1,
    final Transform xf1, final PolygonShape poly2, final Transform xf2) {
  int count1 = poly1.m_count;
  int count2 = poly2.m_count;
  Vec2[] n1s = poly1.m_normals;
  Vec2[] v1s = poly1.m_vertices;
  Vec2[] v2s = poly2.m_vertices;
  
  Transform.mulTransToOutUnsafe(xf2, xf1, xf);
  final Rot xfq = xf.q;

  int bestIndex = 0;
  float maxSeparation = -Float.MAX_VALUE;
  for (int i = 0; i < count1; i++) {
    // Get poly1 normal in frame2.
    Rot.mulToOutUnsafe(xfq, n1s[i], n);
    Transform.mulToOutUnsafe(xf, v1s[i], v1);

    // Find deepest point for normal i.
    float si = Float.MAX_VALUE;
    for (int j = 0; j < count2; ++j) {
      Vec2 v2sj = v2s[j];
      float sij = n.x * (v2sj.x - v1.x) + n.y * (v2sj.y - v1.y);
      if (sij < si) {
        si = sij;
      }
    }
    
    if (si > maxSeparation) {
      maxSeparation = si;
      bestIndex = i;
    }
  }

  results.edgeIndex = bestIndex;
  results.separation = maxSeparation;
}
 
Example #10
Source File: JBox2DTest.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private void initReel() {
    BodyDef reelDef = new BodyDef();
    reelDef.type = BodyType.DYNAMIC;
    reelDef.position = new Vec2(3, 3);
    reel = world.createBody(reelDef);

    FixtureDef fixture = new FixtureDef();
    fixture.friction = 0.5f;
    fixture.restitution = 0.4f;
    fixture.density = 1;

    int parts = 30;
    for (int i = 0; i < parts; ++i) {
        System.out.println("New reel part");
        PolygonShape shape = new PolygonShape();
        double angle1 = i / (double) parts * 2 * Math.PI;
        double x1 = 2.7 * Math.cos(angle1);
        double y1 = 2.7 * Math.sin(angle1);
        double angle2 = (i + 1) / (double) parts * 2 * Math.PI;
        double x2 = 2.7 * Math.cos(angle2);
        double y2 = 2.7 * Math.sin(angle2);
        double angle = (angle1 + angle2) / 2;
        double x = 0.01 * Math.cos(angle);
        double y = 0.01 * Math.sin(angle);

        shape.set(new Vec2[] { new Vec2((float) x1, (float) y1), new Vec2((float) x2, (float) y2),
                new Vec2((float) (x2 - x), (float) (y2 - y)), new Vec2((float) (x1 - x), (float) (y1 - y)) }, 4);
        fixture.shape = shape;
        reel.createFixture(fixture);
    }
}
 
Example #11
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 #12
Source File: JBox2DSimulation.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private void initReel() {
    final BodyDef reelDef = new BodyDef();
    reelDef.type = BodyType.DYNAMIC;
    reelDef.position = new Vec2(3, 3);
    reel = world.createBody(reelDef);

    final FixtureDef fixture = new FixtureDef();
    fixture.friction = 0.5f;
    fixture.restitution = 0.4f;
    fixture.density = 1;

    final int parts = 30;
    for (int i = 0; i < parts; ++i) {
        final PolygonShape shape = new PolygonShape();
        final double angle1 = i / (double) parts * 2 * Math.PI;
        final double x1 = 2.7 * Math.cos(angle1);
        final double y1 = 2.7 * Math.sin(angle1);
        final double angle2 = (i + 1) / (double) parts * 2 * Math.PI;
        final double x2 = 2.7 * Math.cos(angle2);
        final double y2 = 2.7 * Math.sin(angle2);
        final double angle = (angle1 + angle2) / 2;
        final double x = 0.01 * Math.cos(angle);
        final double y = 0.01 * Math.sin(angle);

        shape.set(new Vec2[] { new Vec2((float) x1, (float) y1), new Vec2((float) x2, (float) y2),
                new Vec2((float) (x2 - x), (float) (y2 - y)), new Vec2((float) (x1 - x), (float) (y1 - y)) }, 4);
        fixture.shape = shape;
        reel.createFixture(fixture);
    }
}
 
Example #13
Source File: Box2d.java    From j2cl with Apache License 2.0 5 votes vote down vote up
private Box2d() {
  // Define the gravity vector.
  Vec2 gravity = new Vec2(0f, -9.8f);

  // Initialise the World.
  world = new World(gravity);

  // Create the ground (Something for dynamic bodies to collide with).
  {
    BodyDef groundBodyDef = new BodyDef();
    groundBodyDef.position.set(10, -40);
    groundBodyDef.type = BodyType.STATIC;

    // Create the Body in the World.
    Body ground = world.createBody(groundBodyDef);

    // Create the fixtures (physical aspects) of the ground body.
    FixtureDef groundEdgeFixtureDef = new FixtureDef();
    groundEdgeFixtureDef.density = 1.0f;
    groundEdgeFixtureDef.friction = 1.0f;
    groundEdgeFixtureDef.restitution = 0.4f;

    PolygonShape groundEdge = new PolygonShape();
    groundEdgeFixtureDef.shape = groundEdge;

    // Bottom Edge.
    groundEdge.setAsBox(100, 1);
    ground.createFixture(groundEdgeFixtureDef);
  }

  for (int i = 0; i < NUM_BALLS; i++) {
    spawnBall();
  }
}
 
Example #14
Source File: Collision.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public final void findIncidentEdge(final ClipVertex[] c, final PolygonShape poly1,
    final Transform xf1, int edge1, final PolygonShape poly2, final Transform xf2) {
  int count1 = poly1.m_count;
  final Vec2[] normals1 = poly1.m_normals;

  int count2 = poly2.m_count;
  final Vec2[] vertices2 = poly2.m_vertices;
  final Vec2[] normals2 = poly2.m_normals;

  assert (0 <= edge1 && edge1 < count1);

  final ClipVertex c0 = c[0];
  final ClipVertex c1 = c[1];
  final Rot xf1q = xf1.q;
  final Rot xf2q = xf2.q;

  // Get the normal of the reference edge in poly2's frame.
  // Vec2 normal1 = MulT(xf2.R, Mul(xf1.R, normals1[edge1]));
  // before inline:
  // Rot.mulToOutUnsafe(xf1.q, normals1[edge1], normal1); // temporary
  // Rot.mulTrans(xf2.q, normal1, normal1);
  // after inline:
  final Vec2 v = normals1[edge1];
  final float tempx = xf1q.c * v.x - xf1q.s * v.y;
  final float tempy = xf1q.s * v.x + xf1q.c * v.y;
  final float normal1x = xf2q.c * tempx + xf2q.s * tempy;
  final float normal1y = -xf2q.s * tempx + xf2q.c * tempy;

  // end inline

  // Find the incident edge on poly2.
  int index = 0;
  float minDot = Float.MAX_VALUE;
  for (int i = 0; i < count2; ++i) {
    Vec2 b = normals2[i];
    float dot = normal1x * b.x + normal1y * b.y;
    if (dot < minDot) {
      minDot = dot;
      index = i;
    }
  }

  // Build the clip vertices for the incident edge.
  int i1 = index;
  int i2 = i1 + 1 < count2 ? i1 + 1 : 0;

  // c0.v = Mul(xf2, vertices2[i1]);
  Vec2 v1 = vertices2[i1];
  Vec2 out = c0.v;
  out.x = (xf2q.c * v1.x - xf2q.s * v1.y) + xf2.p.x;
  out.y = (xf2q.s * v1.x + xf2q.c * v1.y) + xf2.p.y;
  c0.id.indexA = (byte) edge1;
  c0.id.indexB = (byte) i1;
  c0.id.typeA = (byte) ContactID.Type.FACE.ordinal();
  c0.id.typeB = (byte) ContactID.Type.VERTEX.ordinal();

  // c1.v = Mul(xf2, vertices2[i2]);
  Vec2 v2 = vertices2[i2];
  Vec2 out1 = c1.v;
  out1.x = (xf2q.c * v2.x - xf2q.s * v2.y) + xf2.p.x;
  out1.y = (xf2q.s * v2.x + xf2q.c * v2.y) + xf2.p.y;
  c1.id.indexA = (byte) edge1;
  c1.id.indexB = (byte) i2;
  c1.id.typeA = (byte) ContactID.Type.FACE.ordinal();
  c1.id.typeB = (byte) ContactID.Type.VERTEX.ordinal();
}
 
Example #15
Source File: Collision.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void collideEdgeAndPolygon(Manifold manifold, final EdgeShape edgeA, final Transform xfA,
    final PolygonShape polygonB, final Transform xfB) {
  collider.collide(manifold, edgeA, xfA, polygonB, xfB);
}
 
Example #16
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 #17
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 #18
Source File: EdgeAndPolygonContact.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().collideEdgeAndPolygon(manifold, (EdgeShape) m_fixtureA.getShape(), xfA,
      (PolygonShape) m_fixtureB.getShape(), xfB);
}
 
Example #19
Source File: PolygonContact.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().collidePolygons(manifold, (PolygonShape) m_fixtureA.getShape(), xfA,
      (PolygonShape) m_fixtureB.getShape(), xfB);
}
 
Example #20
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 #21
Source File: MoBike.java    From kAndroid with Apache License 2.0 4 votes vote down vote up
private Shape createPolygonBody(View childView) {
    PolygonShape polygonShape = new PolygonShape();
    //形状的大小为 view 的一半 (还可以等比缩放)
    polygonShape.setAsBox(mappingView2Body(childView.getWidth() / 2), mappingView2Body(childView.getHeight() / 2));
    return polygonShape;
}
 
Example #22
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 #23
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;
}