org.jbox2d.dynamics.BodyDef Java Examples

The following examples show how to use org.jbox2d.dynamics.BodyDef. 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: TestbedTest.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void init(TestbedModel model) {
  this.model = model;

  Vec2 gravity = new Vec2(0, -10f);
  m_world = model.getWorldCreator().createWorld(gravity);
  m_world.setParticleGravityScale(0.4f);
  m_world.setParticleDensity(1.2f);
  bomb = null;
  mouseJoint = null;

  mouseTracing = false;
  mouseTracerPosition.setZero();
  mouseTracerVelocity.setZero();

  BodyDef bodyDef = new BodyDef();
  groundBody = m_world.createBody(bodyDef);

  init(m_world, false);
}
 
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: 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 #5
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 #6
Source File: Surface.java    From Form-N-Fun with MIT License 6 votes vote down vote up
public Surface(float _x0,float _y0,float _x1,float _y1 ,Createbox2d _box2d)  //used as boundary for the game so that balls won't go out of the screen
{
    x0 = _x0;
    y0 = _y0;
    x1 = _x1;
    y1 = _y1;
    box2d = _box2d;
    surface = new ArrayList<Vec2>();
    surface.add(new Vec2(x0,y0));
    surface.add(new Vec2(x1,y1));
    ChainShape chain = new ChainShape(); //create chainshape in box2d
    Vec2[] vertices = new Vec2[surface.size()];
    for(int i = 0; i < vertices.length; i++)
    {
        vertices[i] = box2d.coordPixelsToWorld(surface.get(i)); //convert surface points to box2d world coordinates
    }
    chain.createLoop(vertices, vertices.length);
    BodyDef bd = new BodyDef(); //Define body
    body = box2d.getWorld().createBody(bd);
    body.createFixture(chain,1);
    body.setUserData(this);

}
 
Example #7
Source File: Maze.java    From Form-N-Fun with MIT License 6 votes vote down vote up
public Maze(BoundaryPoints rigidpoints,Createbox2d _box2d) //used to create mazes
{
    box2d = _box2d;
    this.rigidpoints = rigidpoints; //get points to form maze
    surface = new ArrayList<Vec2>(); //to build box2d surface
    for(int i = 0; i < rigidpoints.size(); i++ )
        if(this.rigidpoints.getPoints().get(i).x > 0 && this.rigidpoints.getPoints().get(i).y > 0)
            surface.add(new Vec2(this.rigidpoints.getPoints().get(i).x, this.rigidpoints.getPoints().get(i).y)); //add points to build box2d surface

    if(surface.size() > 0)
    {
        ChainShape chain = new ChainShape(); //chainshape in box2d
        Vec2[] vertices = new Vec2[surface.size()];
        for(int i = 0; i < vertices.length; i++)
        {
            vertices[i] = box2d.coordPixelsToWorld(surface.get(i)); //convert surface points to box2d world coordinates
        }
        chain.createChain(vertices, vertices.length);//create chain
        BodyDef bd = new BodyDef(); //define body
        body = box2d.getWorld().createBody(bd);
        body.createFixture(chain,1);  //body properties
        body.setUserData(this);
    }

}
 
Example #8
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 #9
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 #10
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 #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 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 #13
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 #14
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 #15
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 #16
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 #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: 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 #19
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 #20
Source File: Breakable.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
void Break() {
  // Create two bodies from one.
  Body body1 = m_piece1.getBody();
  Vec2 center = body1.getWorldCenter();

  body1.destroyFixture(m_piece2);
  m_piece2 = null;

  BodyDef bd = new BodyDef();
  bd.type = BodyType.DYNAMIC;
  bd.position = body1.getPosition();
  bd.angle = body1.getAngle();

  Body body2 = getWorld().createBody(bd);
  m_piece2 = body2.createFixture(m_shape2, 1.0f);

  // Compute consistent velocities for new bodies based on
  // cached velocity.
  Vec2 center1 = body1.getWorldCenter();
  Vec2 center2 = body2.getWorldCenter();

  Vec2 velocity1 = m_velocity.add(Vec2.cross(m_angularVelocity, center1.sub(center)));
  Vec2 velocity2 = m_velocity.add(Vec2.cross(m_angularVelocity, center2.sub(center)));

  body1.setAngularVelocity(m_angularVelocity);
  body1.setLinearVelocity(velocity1);

  body2.setAngularVelocity(m_angularVelocity);
  body2.setLinearVelocity(velocity2);
}
 
Example #21
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 #22
Source File: MoBike.java    From kAndroid with Apache License 2.0 5 votes vote down vote up
private void createBody(View childView) {
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.DYNAMIC;

    //设置view中心点位置
    bodyDef.position.set(mappingView2Body(childView.getX() + childView.getWidth() / 2),
            mappingView2Body(childView.getY() + childView.getHeight() / 2));

    Shape shape = null;
    Boolean isCircle = (boolean) childView.getTag(R.id.wd_view_circle_tag);
    if (isCircle != null && isCircle) {
        shape = createCircleBody(childView);
    } else {
        shape = createPolygonBody(childView);
    }

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

    Body body = world.createBody(bodyDef);
    body.createFixture(def);

    childView.setTag(R.id.wd_view_body_tag, body);
    body.setLinearVelocity(new Vec2(random.nextFloat(), random.nextFloat()));
}
 
Example #23
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 #24
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 #25
Source File: PbDeserializer.java    From jbox2d with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public Body deserializeBody(World argWorld, PbBody argBody) {
  PbBody b = argBody;

  BodyDef bd = new BodyDef();
  bd.position.set(pbToVec(b.getPosition()));
  bd.angle = b.getAngle();
  bd.linearDamping = b.getLinearDamping();
  bd.angularDamping = b.getAngularDamping();
  bd.gravityScale = b.getGravityScale();
  // velocities are populated after fixture addition
  
  bd.bullet = b.getBullet();
  bd.allowSleep = b.getAllowSleep();
  bd.awake = b.getAwake();
  bd.active = b.getActive();
  bd.fixedRotation = b.getFixedRotation();

  switch (b.getType()) {
    case DYNAMIC:
      bd.type = BodyType.DYNAMIC;
      break;
    case KINEMATIC:
      bd.type = BodyType.KINEMATIC;
      break;
    case STATIC:
      bd.type = BodyType.STATIC;
      break;
    default:
      UnsupportedObjectException e =
          new UnsupportedObjectException("Unknown body type: " + argBody.getType(), Type.BODY);
      if (ulistener == null || ulistener.isUnsupported(e)) {
        throw e;
      }
      return null;
  }

  Body body = argWorld.createBody(bd);

  for (int i = 0; i < b.getFixturesCount(); i++) {
    deserializeFixture(body, b.getFixtures(i));
  }
  
  // adding fixtures can change this, so we put this here and set it directly in the body
  body.m_linearVelocity.set(pbToVec(b.getLinearVelocity()));
  body.m_angularVelocity = b.getAngularVelocity();

  if (listener != null && b.hasTag()) {
    listener.processBody(body, b.getTag());
  }
  return body;
}
 
Example #26
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 #27
Source File: Createbox2d.java    From Form-N-Fun with MIT License 2 votes vote down vote up
/**
 *  create body in box2d world
 */

public Body createBody(BodyDef bd) {

    return world.createBody(bd);
}