Package org.jbox2d.collision.shapes

Examples of org.jbox2d.collision.shapes.PolygonShape


  final float dfriction = 0.1f;
  int baseCount = 25;

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


    if(argDeserialized){
      return;
    }

    { // Floor
      PolygonShape sd = new PolygonShape();
      sd.setAsBox(50.0f, 10.0f);

      BodyDef bd = new BodyDef();
      bd.position = new Vec2(0.0f, -10.0f);
      getWorld().createBody(bd).createFixture(sd, 0f);
    }

    {
      ddensity = 10f;
      // Make bullet
      PolygonShape sd = new PolygonShape();
      sd.setAsBox(.7f, .7f);
      FixtureDef fd = new FixtureDef();
      fd.density = 35f;
      BodyDef bd = new BodyDef();
      bd.type = BodyType.DYNAMIC;
      fd.shape = sd;
View Full Code Here

      body.createFixture(shape, 1.0f);
    }

    // Chassis
    {
      PolygonShape shape = new PolygonShape();
      shape.setAsBox(2.5f, 1.0f);

      FixtureDef sd = new FixtureDef();
      sd.density = 1.0f;
      sd.shape = shape;
      sd.filter.groupIndex = -1;
View Full Code Here

    fd1.filter.groupIndex = -1;
    fd2.filter.groupIndex = -1;
    fd1.density = 1.0f;
    fd2.density = 1.0f;

    PolygonShape poly1 = new PolygonShape();
    PolygonShape poly2 = new PolygonShape();

    if (s > 0.0f) {
      Vec2[] vertices = new Vec2[3];

      vertices[0] = p1;
      vertices[1] = p2;
      vertices[2] = p3;
      poly1.set(vertices, 3);

      vertices[0] = new Vec2();
      vertices[1] = p5.sub(p4);
      vertices[2] = p6.sub(p4);
      poly2.set(vertices, 3);
    } else {
      Vec2[] vertices = new Vec2[3];

      vertices[0] = p1;
      vertices[1] = p3;
      vertices[2] = p2;
      poly1.set(vertices, 3);

      vertices[0] = new Vec2();
      vertices[1] = p6.sub(p4);
      vertices[2] = p5.sub(p4);
      poly2.set(vertices, 3);
    }

    fd1.shape = poly1;
    fd2.shape = poly2;
View Full Code Here

      pendulum = getWorld().createBody(bodyDef);
      pendulum.createFixture(shape, 1);
    }

    {
      PolygonShape shape = new PolygonShape();
      shape.setAsBox(1, 1);

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

    {
      PolygonShape shape = new PolygonShape();
      shape.setAsBox(3, 1);

      BodyDef bodyDef = new BodyDef();
      bodyDef.type = BodyType.STATIC;
      ground = getWorld().createBody(bodyDef);
      ground.createFixture(shape, 0);
View Full Code Here

      BodyDef bd = new BodyDef();
      bd.type = BodyType.DYNAMIC;
      bd.position.set(0.0f, 3.0f);
      m_attachment = getWorld().createBody(bd);

      PolygonShape shape = new PolygonShape();
      shape.setAsBox(0.5f, 2.0f);
      m_attachment.createFixture(shape, 2.0f);
    }

    // Define platform
    {
      BodyDef bd = new BodyDef();
      bd.type = BodyType.DYNAMIC;
      bd.position.set(-4.0f, 5.0f);
      m_platform = getWorld().createBody(bd);

      PolygonShape shape = new PolygonShape();
      shape.setAsBox(0.5f, 4.0f, new Vec2(4.0f, 0.0f), 0.5f * MathUtils.PI);

      FixtureDef fd = new FixtureDef();
      fd.shape = shape;
      fd.friction = 0.6f;
      fd.density = 2.0f;
      m_platform.createFixture(fd);

      RevoluteJointDef rjd = new RevoluteJointDef();
      rjd.initialize(m_attachment, m_platform, new Vec2(0.0f, 5.0f));
      rjd.maxMotorTorque = 50.0f;
      rjd.enableMotor = true;
      getWorld().createJoint(rjd);

      PrismaticJointDef pjd = new PrismaticJointDef();
      pjd.initialize(ground, m_platform, new Vec2(0.0f, 5.0f), new Vec2(1.0f, 0.0f));

      pjd.maxMotorForce = 1000.0f;
      pjd.enableMotor = true;
      pjd.lowerTranslation = -10.0f;
      pjd.upperTranslation = 10.0f;
      pjd.enableLimit = true;

      getWorld().createJoint(pjd);

    }

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

      PolygonShape shape = new PolygonShape();
      shape.setAsBox(0.75f, 0.75f);

      FixtureDef fd = new FixtureDef();
      fd.shape = shape;
      fd.friction = 0.6f;
      fd.density = 2.0f;
View Full Code Here

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

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

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

      WeldJointDef jd = new WeldJointDef();

      Body prevBody = ground;
      for (int i = 0; i < e_count; ++i) {
        BodyDef bd = new BodyDef();
        bd.type = BodyType.DYNAMIC;
        bd.position.set(-14.5f + 1.0f * i, 5.0f);
        Body body = getWorld().createBody(bd);
        body.createFixture(fd);

        Vec2 anchor = new Vec2(-15.0f + 1.0f * i, 5.0f);
        jd.initialize(prevBody, body, anchor);
        getWorld().createJoint(jd);

        prevBody = body;
      }
    }

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

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

      WeldJointDef jd = new WeldJointDef();
      jd.frequencyHz = 5f;
      jd.dampingRatio = .7f;

      Body prevBody = ground;
      for (int i = 0; i < 3; ++i) {
        BodyDef bd = new BodyDef();
        bd.type = BodyType.DYNAMIC;
        bd.position.set(-14.0f + 2.0f * i, 15.0f);
        Body body = getWorld().createBody(bd);
        body.createFixture(fd);

        Vec2 anchor = new Vec2(-15.0f + 2.0f * i, 15.0f);
        jd.initialize(prevBody, body, anchor);
        getWorld().createJoint(jd);

        prevBody = body;
      }
    }

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

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

      WeldJointDef jd = new WeldJointDef();

      Body prevBody = ground;
      for (int i = 0; i < e_count; ++i) {
        BodyDef bd = new BodyDef();
        bd.type = BodyType.DYNAMIC;
        bd.position.set(-4.5f + 1.0f * i, 5.0f);
        Body body = getWorld().createBody(bd);
        body.createFixture(fd);

        if (i > 0) {
          Vec2 anchor = new Vec2(-5.0f + 1.0f * i, 5.0f);
          jd.initialize(prevBody, body, anchor);
          getWorld().createJoint(jd);
        }

        prevBody = body;
      }
    }

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

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

      WeldJointDef jd = new WeldJointDef();
      jd.frequencyHz = 8f;
      jd.dampingRatio = .7f;

      Body prevBody = ground;
      for (int i = 0; i < e_count; ++i) {
        BodyDef bd = new BodyDef();
        bd.type = BodyType.DYNAMIC;
        bd.position.set(5.5f + 1.0f * i, 10.0f);
        Body body = getWorld().createBody(bd);
        body.createFixture(fd);

        if (i > 0) {
          Vec2 anchor = new Vec2(5.0f + 1.0f * i, 10.0f);
          jd.initialize(prevBody, body, anchor);
          getWorld().createJoint(jd);
        }

        prevBody = body;
      }
    }

    for (int i = 0; i < 2; ++i) {
      Vec2 vertices[] = new Vec2[3];
      vertices[0] = new Vec2(-0.5f, 0.0f);
      vertices[1] = new Vec2(0.5f, 0.0f);
      vertices[2] = new Vec2(0.0f, 1.5f);

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

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

View Full Code Here

    }

    m_world.setParticleRadius(0.15f);
    m_world.setParticleDamping(0.2f);
    {
      PolygonShape shape = new PolygonShape();
      shape.setAsBox(8, 10, new Vec2(-12, 10.1f), 0);
      ParticleGroupDef pd = new ParticleGroupDef();
      pd.shape = shape;
      m_world.createParticleGroup(pd);
    }
  }
View Full Code Here

        body.createFixture(circle2, 0.0f);
      }
    }

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

      PolygonShape polygon2 = new PolygonShape();
      polygon2.setAsBox(0.25f, 0.5f, new Vec2(0.0f, -0.5f), 0.5f * MathUtils.PI);

      for (int i = 0; i < 10; ++i) {
        float x = MathUtils.randomFloat(-0.1f, 0.1f);
        BodyDef bd = new BodyDef();
        bd.type = BodyType.DYNAMIC;
        bd.position.set(x - 5.0f, 1.05f + 2.5f * i);
        bd.angle = MathUtils.randomFloat(-MathUtils.PI, MathUtils.PI);
        Body body = getWorld().createBody(bd);
        body.createFixture(polygon1, 2.0f);
        body.createFixture(polygon2, 2.0f);
      }
    }

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

      Vec2[] vertices = new Vec2[3];

      PolygonShape triangle1 = new PolygonShape();
      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));
      triangle1.set(vertices, 3);

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

      PolygonShape triangle2 = new PolygonShape();
      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));
      triangle2.set(vertices, 3);

      for (int i = 0; i < 10; ++i) {
        float x = MathUtils.randomFloat(-0.1f, 0.1f);
        BodyDef bd = new BodyDef();
        bd.type = BodyType.DYNAMIC;
        bd.position.set(x, 2.05f + 2.5f * i);
        bd.angle = 0.0f;
        Body body = getWorld().createBody(bd);
        body.createFixture(triangle1, 2.0f);
        body.createFixture(triangle2, 2.0f);
      }
    }

    {
      PolygonShape bottom = new PolygonShape();
      bottom.setAsBox(1.5f, 0.15f);

      PolygonShape left = new PolygonShape();
      left.setAsBox(0.15f, 2.7f, new Vec2(-1.45f, 2.35f), 0.2f);

      PolygonShape right = new PolygonShape();
      right.setAsBox(0.15f, 2.7f, new Vec2(1.45f, 2.35f), -0.2f);

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

    Body ground = null;
    {
      BodyDef bd = new BodyDef();
      ground = getWorld().createBody(bd);

      PolygonShape shape = new PolygonShape();
      shape.setAsBox(5.0f, 100.0f);
      bd = new BodyDef();
      bd.type = BodyType.STATIC;
      FixtureDef sides = new FixtureDef();
      sides.shape = shape;
      sides.density = 0;
      sides.friction = 0;
      sides.restitution = .8f;
      sides.filter.categoryBits = 4;
      sides.filter.maskBits = 2;

      bd.position.set(-10.01f, 50.0f);
      Body bod = world.createBody(bd);
      bod.createFixture(sides);
      bd.position.set(10.01f, 50.0f);
      bod = world.createBody(bd);
      bod.createFixture(sides);
    }

    // turney
    {
      CircleShape cd;
      FixtureDef fd = new FixtureDef();
      BodyDef bd = new BodyDef();
      bd.type = BodyType.DYNAMIC;
      int numPieces = 5;
      float radius = 4f;
      bd.position = new Vec2(0.0f, 25.0f);
      Body body = getWorld().createBody(bd);
      for (int i = 0; i < numPieces; i++) {
        cd = new CircleShape();
        cd.m_radius = .5f;
        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);
      }

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


    {
      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 = 20000;
        rjd.enableMotor = true;
        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(7f, 2f);

        BodyDef bd = new BodyDef();
        bd.type = BodyType.DYNAMIC;
        bd.position.set(0.0f, 17.0f);
        Body body = getWorld().createBody(bd);
        FixtureDef piston = new FixtureDef();
        piston.shape = shape;
        piston.density = 2;
        piston.filter.categoryBits = 1;
        piston.filter.maskBits = 2;
        body.createFixture(piston);
        body.setBullet(false);
       
        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 = true;

        getWorld().createJoint(pjd);
      }

      // Create a payload
      {
        PolygonShape sd = new PolygonShape();
        BodyDef bd = new BodyDef();
        bd.type = BodyType.DYNAMIC;
        FixtureDef fixture = new FixtureDef();
        Body body;
        for (int i = 0; i < 100; ++i) {
          sd.setAsBox(0.4f, 0.3f);
          bd.position.set(-1.0f, 23.0f + i);

          bd.bullet = bullet;
          body = world.createBody(bd);
          fixture.shape = sd;
          fixture.density = .1f;
          fixture.filter.categoryBits = 2;
          fixture.filter.maskBits = 1 | 4 | 2;
          body.createFixture(fixture);
        }

        CircleShape cd = new CircleShape();
        cd.m_radius = 0.36f;
        for (int i = 0; i < 100; ++i) {
          bd.position.set(1.0f, 23.0f + i);
          bd.bullet = bullet;
          fixture.shape = cd;
          fixture.density = 2f;
          fixture.filter.categoryBits = 2;
          fixture.filter.maskBits = 1 | 4 | 2;
          body = world.createBody(bd);
          body.createFixture(fixture);
        }
       
        float angle = 0.0f;
        float delta = MathUtils.PI / 3.0f;
        Vec2 vertices[] = new Vec2[6];
        for (int i = 0; i < 6; ++i) {
          vertices[i] = new Vec2(0.3f * MathUtils.cos(angle), 0.3f * MathUtils.sin(angle));
          angle += delta;
        }

        PolygonShape shape = new PolygonShape();
        shape.set(vertices, 6);

        for (int i = 0; i < 100; ++i) {
          bd.position.set(0f, 23.0f + i);
          bd.type = BodyType.DYNAMIC;
          bd.fixedRotation = true;
View Full Code Here

TOP

Related Classes of org.jbox2d.collision.shapes.PolygonShape

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.