Java Code Examples for org.jbox2d.collision.shapes.PolygonShape#setAsBox()

The following examples show how to use org.jbox2d.collision.shapes.PolygonShape#setAsBox() . 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: 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 3
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 4
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 5
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 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: 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 8
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 9
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);
}