Package org.jbox2d.dynamics

Examples of org.jbox2d.dynamics.BodyDef


    if (deserialized) {
      return;
    }
    Body ground = null;
    {
      BodyDef bd = new BodyDef();
      ground = getWorld().createBody(bd);

      EdgeShape shape = new EdgeShape();
      shape.set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));
      ground.createFixture(shape, 0.0f);
    }

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

      BodyDef bd = new BodyDef();
      bd.type = BodyType.DYNAMIC;

      RevoluteJointDef rjd = new RevoluteJointDef();

      bd.position.set(-10f, 20.0f);
      Body body = getWorld().createBody(bd);
      body.createFixture(shape, 5.0f);

      float w = 100.0f;
      body.setAngularVelocity(w);
      body.setLinearVelocity(new Vec2(-8.0f * w, 0.0f));

      rjd.initialize(ground, body, new Vec2(-10.0f, 12.0f));
      rjd.motorSpeed = -1.0f * MathUtils.PI;
      rjd.maxMotorTorque = 10000.0f;
      rjd.enableMotor = false;
      rjd.lowerAngle = -0.25f * MathUtils.PI;
      rjd.upperAngle = 0.5f * MathUtils.PI;
      rjd.enableLimit = true;
      rjd.collideConnected = true;

      m_joint = (RevoluteJoint) getWorld().createJoint(rjd);
    }

    {
      CircleShape circle_shape = new CircleShape();
      circle_shape.m_radius = 3.0f;

      BodyDef circle_bd = new BodyDef();
      circle_bd.type = BodyType.DYNAMIC;
      circle_bd.position.set(5.0f, 30.0f);

      FixtureDef fd = new FixtureDef();
      fd.density = 5.0f;
      fd.filter.maskBits = 1;
      fd.shape = circle_shape;

      Body ball = m_world.createBody(circle_bd);
      ball.createFixture(fd);

      PolygonShape polygon_shape = new PolygonShape();
      polygon_shape.setAsBox(10.0f, 0.2f, new Vec2(-10.0f, 0.0f), 0.0f);

      BodyDef polygon_bd = new BodyDef();
      polygon_bd.position.set(20.0f, 10.0f);
      polygon_bd.type = BodyType.DYNAMIC;
      polygon_bd.bullet = true;
      Body polygon_body = m_world.createBody(polygon_bd);
      polygon_body.createFixture(polygon_shape, 2.0f);

      RevoluteJointDef rjd = new RevoluteJointDef();
      rjd.initialize(ground, polygon_body, new Vec2(20.0f, 10.0f));
      rjd.lowerAngle = -0.25f * MathUtils.PI;
      rjd.upperAngle = 0.0f * MathUtils.PI;
      rjd.enableLimit = true;
      m_world.createJoint(rjd);
    }

    // Tests mass computation of a small object far from the origin
    {
      BodyDef bodyDef = new BodyDef();
      bodyDef.type = BodyType.DYNAMIC;
      Body body = m_world.createBody(bodyDef);

      PolygonShape polyShape = new PolygonShape();
      Vec2 verts[] = new Vec2[3];
View Full Code Here


    Body rightWall = null;
    {
      // Ground
      PolygonShape sd = new PolygonShape();
      sd.setAsBox(50.0f, 10.0f);
      BodyDef bd = new BodyDef();
      bd.type = BodyType.STATIC;
      bd.position = new Vec2(0.0f, -10.0f);
      Body b = getWorld().createBody(bd);
      FixtureDef fd = new FixtureDef();
      fd.shape = sd;
      fd.friction = 1.0f;
      b.createFixture(fd);

      // Walls
      sd.setAsBox(3.0f, 50.0f);
      bd = new BodyDef();
      bd.position = new Vec2(45.0f, 25.0f);
      rightWall = getWorld().createBody(bd);
      rightWall.createFixture(sd, 0);
      bd.position = new Vec2(-45.0f, 25.0f);
      leftWall = getWorld().createBody(bd);
      leftWall.createFixture(sd, 0);

      // Corners
      bd = new BodyDef();
      sd.setAsBox(20.0f, 3.0f);
      bd.angle = (float) (-Math.PI / 4.0);
      bd.position = new Vec2(-35f, 8.0f);
      Body myBod = getWorld().createBody(bd);
      myBod.createFixture(sd, 0);
      bd.angle = (float) (Math.PI / 4.0);
      bd.position = new Vec2(35f, 8.0f);
      myBod = getWorld().createBody(bd);
      myBod.createFixture(sd, 0);

      // top
      sd.setAsBox(50.0f, 10.0f);
      bd.type = BodyType.STATIC;
      bd.angle = 0;
      bd.position = new Vec2(0.0f, 75.0f);
      b = getWorld().createBody(bd);
      fd.shape = sd;
      fd.friction = 1.0f;
      b.createFixture(fd);

    }

    CircleShape cd;
    FixtureDef fd = new FixtureDef();

    BodyDef bd = new BodyDef();
    bd.type = BodyType.DYNAMIC;
    int numPieces = 5;
    float radius = 6f;
    bd.position = new Vec2(0.0f, 10.0f);
    Body body = getWorld().createBody(bd);
    for (int i = 0; i < numPieces; i++) {
      cd = new CircleShape();
      cd.m_radius = 1.2f;
      fd.shape = cd;
      fd.density = 25;
      fd.friction = .1f;
      fd.restitution = .9f;
      float xPos = radius * (float) Math.cos(2f * Math.PI * (i / (float) (numPieces)));
      float yPos = radius * (float) Math.sin(2f * Math.PI * (i / (float) (numPieces)));
      cd.m_p.set(xPos, yPos);

      body.createFixture(fd);
    }

    body.setBullet(false);

    RevoluteJointDef rjd = new RevoluteJointDef();
    rjd.initialize(body, getGroundBody(), body.getPosition());
    rjd.motorSpeed = MathUtils.PI;
    rjd.maxMotorTorque = 1000000.0f;
    rjd.enableMotor = true;
    joint = (RevoluteJoint) getWorld().createJoint(rjd);

    {
      int loadSize = 41;

      for (int j = 0; j < 15; j++) {
        for (int i = 0; i < loadSize; i++) {
          CircleShape circ = new CircleShape();
          BodyDef bod = new BodyDef();
          bod.type = BodyType.DYNAMIC;
          circ.m_radius = 1.0f + (i % 2 == 0 ? 1.0f : -1.0f) * .5f * MathUtils.randomFloat(.5f, 1f);
          FixtureDef fd2 = new FixtureDef();
          fd2.shape = circ;
          fd2.density = circ.m_radius * 1.5f;
 
View Full Code Here

      FixtureDef sd = new FixtureDef();
      sd.shape = shape;
      sd.friction = 0.3f;

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

    // 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;

    triangleShapeDef.filter.groupIndex = k_smallGroup;
    triangleShapeDef.filter.categoryBits = k_triangleCategory;
    triangleShapeDef.filter.maskBits = k_triangleMask;

    BodyDef triangleBodyDef = new BodyDef();
    triangleBodyDef.type = BodyType.DYNAMIC;
    triangleBodyDef.position.set(-5.0f, 2.0f);

    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);
    triangleShapeDef.filter.groupIndex = k_largeGroup;
    triangleBodyDef.position.set(-5.0f, 6.0f);
    triangleBodyDef.fixedRotation = true; // look at me!

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

    {
      BodyDef bd = new BodyDef();
      bd.type = BodyType.DYNAMIC;
      bd.position.set(-5.0f, 10.0f);
      Body body = getWorld().createBody(bd);

      PolygonShape p = new PolygonShape();
      p.setAsBox(0.5f, 1.0f);
      body.createFixture(p, 1.0f);

      PrismaticJointDef jd = new PrismaticJointDef();
      jd.bodyA = body2;
      jd.bodyB = body;
      jd.enableLimit = true;
      jd.localAnchorA.set(0.0f, 4.0f);
      jd.localAnchorB.setZero();
      jd.localAxisA.set(0.0f, 1.0f);
      jd.lowerTranslation = -1.0f;
      jd.upperTranslation = 1.0f;

      getWorld().createJoint(jd);
    }

    // Small box
    polygon.setAsBox(1.0f, 0.5f);
    FixtureDef boxShapeDef = new FixtureDef();
    boxShapeDef.shape = polygon;
    boxShapeDef.density = 1.0f;
    boxShapeDef.restitution = 0.1f;

    boxShapeDef.filter.groupIndex = k_smallGroup;
    boxShapeDef.filter.categoryBits = k_boxCategory;
    boxShapeDef.filter.maskBits = k_boxMask;

    BodyDef boxBodyDef = new BodyDef();
    boxBodyDef.type = BodyType.DYNAMIC;
    boxBodyDef.position.set(0.0f, 2.0f);

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

    // Large box (recycle definitions)
    polygon.setAsBox(2.0f, 1.0f);
    boxShapeDef.filter.groupIndex = k_largeGroup;
    boxBodyDef.position.set(0.0f, 6.0f);

    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;

    circleShapeDef.filter.groupIndex = k_smallGroup;
    circleShapeDef.filter.categoryBits = k_circleCategory;
    circleShapeDef.filter.maskBits = k_circleMask;

    BodyDef circleBodyDef = new BodyDef();
    circleBodyDef.type = BodyType.DYNAMIC;
    circleBodyDef.position.set(5.0f, 2.0f);
   
    Body body5 = getWorld().createBody(circleBodyDef);
    body5.createFixture(circleShapeDef);
View Full Code Here

    {
      CircleShape circleShape = new CircleShape();
      circleShape.m_radius = 1;
      Shape shape = circleShape;

      BodyDef bodyDef = new BodyDef();
      bodyDef.type = BodyType.DYNAMIC;
      bodyDef.position.set(-5, 0);
      bodyDef.allowSleep = false;
      pendulum = getWorld().createBody(bodyDef);
      pendulum.createFixture(shape, 1);
    }

    {
      BodyDef bodyDef = new BodyDef();
      bodyDef.type = BodyType.STATIC;
      ground = getWorld().createBody(bodyDef);
    }

    RevoluteJointDef jointDef = new RevoluteJointDef();
View Full Code Here

    Body ground = null;
    {
      PolygonShape sd = new PolygonShape();
      sd.setAsBox(50.0f, 0.4f);

      BodyDef bd = new BodyDef();
      bd.position.set(0.0f, 0.0f);
      ground = getWorld().createBody(bd);
      ground.createFixture(sd, 0f);

      sd.setAsBox(0.4f, 50.0f, new Vec2(-10.0f, 0.0f), 0.0f);
      ground.createFixture(sd, 0f);
      sd.setAsBox(0.4f, 50.0f, new Vec2(10.0f, 0.0f), 0.0f);
      ground.createFixture(sd, 0f);
    }

    ConstantVolumeJointDef cvjd = new ConstantVolumeJointDef();

    float cx = 0.0f;
    float cy = 10.0f;
    float rx = 5.0f;
    float ry = 5.0f;
    int nBodies = 20;
    float bodyRadius = 0.5f;
    for (int i = 0; i < nBodies; ++i) {
      float angle = MathUtils.map(i, 0, nBodies, 0, 2 * 3.1415f);
      BodyDef bd = new BodyDef();
      // bd.isBullet = true;
      bd.fixedRotation = true;

      float x = cx + rx * (float) Math.sin(angle);
      float y = cy + ry * (float) Math.cos(angle);
      bd.position.set(new Vec2(x, y));
      bd.type = BodyType.DYNAMIC;
      Body body = getWorld().createBody(bd);

      FixtureDef fd = new FixtureDef();
      CircleShape cd = new CircleShape();
      cd.m_radius = bodyRadius;
      fd.shape = cd;
      fd.density = 1.0f;
      body.createFixture(fd);
      cvjd.addBody(body);
    }

    cvjd.frequencyHz = 10.0f;
    cvjd.dampingRatio = 1.0f;
    cvjd.collideConnected = false;
    getWorld().createJoint(cvjd);

    BodyDef bd2 = new BodyDef();
    bd2.type = BodyType.DYNAMIC;
    PolygonShape psd = new PolygonShape();
    psd.setAsBox(3.0f, 1.5f, new Vec2(cx, cy + 15.0f), 0.0f);
    bd2.position = new Vec2(cx, cy + 15.0f);
    Body fallingBox = getWorld().createBody(bd2);
View Full Code Here

      getGroundBody().createFixture(fd);
    }

    // Define motorized body
    {
      BodyDef bd = new BodyDef();
      bd.type = BodyType.DYNAMIC;
      bd.position.set(0.0f, 8.0f);
      Body body = getWorld().createBody(bd);

      PolygonShape shape = new PolygonShape();
View Full Code Here

    final float k_restitution = 0.4f;

    Body ground;
    {
      BodyDef bd = new BodyDef();
      bd.position.set(0.0f, 20.0f);
      ground = getWorld().createBody(bd);

      EdgeShape shape = new EdgeShape();

      FixtureDef sd = new FixtureDef();
      sd.shape = shape;
      sd.density = 0.0f;
      sd.restitution = k_restitution;

      // Left vertical
      shape.set(new Vec2(-20.0f, -20.0f), new Vec2(-20.0f, 20.0f));
      ground.createFixture(sd);

      // Right vertical
      shape.set(new Vec2(20.0f, -20.0f), new Vec2(20.0f, 20.0f));
      ground.createFixture(sd);

      // Top horizontal
      shape.set(new Vec2(-20.0f, 20.0f), new Vec2(20.0f, 20.0f));
      ground.createFixture(sd);

      // Bottom horizontal
      shape.set(new Vec2(-20.0f, -20.0f), new Vec2(20.0f, -20.0f));
      ground.createFixture(sd);
    }

    {
      Transform xf1 = new Transform();
      xf1.q.set(0.3524f * MathUtils.PI);
      Rot.mulToOutUnsafe(xf1.q, new Vec2(1.0f, 0.0f), xf1.p);

      Vec2 vertices[] = new Vec2[3];
      vertices[0] = Transform.mul(xf1, new Vec2(-1.0f, 0.0f));
      vertices[1] = Transform.mul(xf1, new Vec2(1.0f, 0.0f));
      vertices[2] = Transform.mul(xf1, new Vec2(0.0f, 0.5f));

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

      FixtureDef sd1 = new FixtureDef();
      sd1.shape = poly1;
      sd1.density = 4.0f;

      Transform xf2 = new Transform();
      xf2.q.set(-0.3524f * MathUtils.PI);
      Rot.mulToOut(xf2.q, new Vec2(-1.0f, 0.0f), xf2.p);

      vertices[0] = Transform.mul(xf2, new Vec2(-1.0f, 0.0f));
      vertices[1] = Transform.mul(xf2, new Vec2(1.0f, 0.0f));
      vertices[2] = Transform.mul(xf2, new Vec2(0.0f, 0.5f));

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

      FixtureDef sd2 = new FixtureDef();
      sd2.shape = poly2;
      sd2.density = 2.0f;

      BodyDef bd = new BodyDef();
      bd.type = BodyType.DYNAMIC;
      bd.angularDamping = 2.0f;
      bd.linearDamping = 0.5f;

      bd.position.set(0.0f, 2.0f);
      bd.angle = MathUtils.PI;
      bd.allowSleep = false;
      m_body = getWorld().createBody(bd);
      m_body.createFixture(sd1);
      m_body.createFixture(sd2);
    }

    {
      PolygonShape shape = new PolygonShape();
      shape.setAsBox(0.5f, 0.5f);

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

      for (int i = 0; i < 10; ++i) {
        BodyDef bd = new BodyDef();
        bd.type = BodyType.DYNAMIC;

        bd.position.set(0.0f, 5.0f + 1.54f * i);
        Body body = getWorld().createBody(bd);

View Full Code Here

  @Override
  public void initTest(boolean argDeserialized) {
    Body ground = null;
    {
      BodyDef bd = new BodyDef();
      ground = getWorld().createBody(bd);

      EdgeShape shape = new EdgeShape();
      shape.set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));
      ground.createFixture(shape, 0.0f);
    }

    {
      Body prevBody = ground;

      // Define crank.
      {
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(0.5f, 2.0f);

        BodyDef bd = new BodyDef();
        bd.type = BodyType.DYNAMIC;
        bd.position.set(0.0f, 7.0f);
        Body body = getWorld().createBody(bd);
        body.createFixture(shape, 2.0f);

        RevoluteJointDef rjd = new RevoluteJointDef();
        rjd.initialize(prevBody, body, new Vec2(0.0f, 5.0f));
        rjd.motorSpeed = 1.0f * MathUtils.PI;
        rjd.maxMotorTorque = 10000.0f;
        rjd.enableMotor = true;
        m_joint1 = (RevoluteJoint) getWorld().createJoint(rjd);

        prevBody = body;
      }

      // Define follower.
      {
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(0.5f, 4.0f);

        BodyDef bd = new BodyDef();
        bd.type = BodyType.DYNAMIC;
        bd.position.set(0.0f, 13.0f);
        Body body = getWorld().createBody(bd);
        body.createFixture(shape, 2.0f);

        RevoluteJointDef rjd = new RevoluteJointDef();
        rjd.initialize(prevBody, body, new Vec2(0.0f, 9.0f));
        rjd.enableMotor = false;
        getWorld().createJoint(rjd);

        prevBody = body;
      }

      // Define piston
      {
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(1.5f, 1.5f);

        BodyDef bd = new BodyDef();
        bd.type = BodyType.DYNAMIC;
        bd.fixedRotation = true;
        bd.position.set(0.0f, 17.0f);
        Body body = getWorld().createBody(bd);
        body.createFixture(shape, 2.0f);

        RevoluteJointDef rjd = new RevoluteJointDef();
        rjd.initialize(prevBody, body, new Vec2(0.0f, 17.0f));
        getWorld().createJoint(rjd);

        PrismaticJointDef pjd = new PrismaticJointDef();
        pjd.initialize(ground, body, new Vec2(0.0f, 17.0f), new Vec2(0.0f, 1.0f));

        pjd.maxMotorForce = 1000.0f;
        pjd.enableMotor = false;

        m_joint2 = (PrismaticJoint) getWorld().createJoint(pjd);
      }

      // Create a payload
      {
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(1.5f, 1.5f);

        BodyDef bd = new BodyDef();
        bd.type = BodyType.DYNAMIC;
        bd.position.set(0.0f, 23.0f);
        Body body = getWorld().createBody(bd);
        body.createFixture(shape, 2.0f);
      }
View Full Code Here

    float L = 12.0f;
    float a = 1.0f;
    float b = 2.0f;
    Body ground = null;
    {
      BodyDef bd = new BodyDef();
      ground = getWorld().createBody(bd);

      EdgeShape shape = new EdgeShape();
      shape.set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));
      ground.createFixture(shape, 0.0f);

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

      circle.m_p.set(-10.0f, y + b + L);
      ground.createFixture(circle, 0.0f);

      circle.m_p.set(10.0f, y + b + L);
      ground.createFixture(circle, 0.0f);
    }

    {

      PolygonShape shape = new PolygonShape();
      shape.setAsBox(a, b);

      BodyDef bd = new BodyDef();
      bd.type = BodyType.DYNAMIC;

      bd.position.set(-10.0f, y);
      Body body1 = getWorld().createBody(bd);
      body1.createFixture(shape, 5.0f);
View Full Code Here

  }

  @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);
View Full Code Here

TOP

Related Classes of org.jbox2d.dynamics.BodyDef

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.