Package org.jbox2d.dynamics

Examples of org.jbox2d.dynamics.World


    }
    TestbedModel model = new TestbedModel();
    model.setWorldCreator(new WorldCreator() {
      @Override
      public World createWorld(Vec2 gravity) {
        return new World(gravity, new DefaultWorldPool(100, 40), new BroadPhaseJNI());
      }
    });
    final TestbedController controller =
        new TestbedController(model, UpdateBehavior.UPDATE_CALLED, MouseBehavior.NORMAL,
            new TestbedErrorHandler() {
View Full Code Here


    }
    TestbedModel model = new TestbedModel();
    model.setWorldCreator(new WorldCreator() {
      @Override
      public World createWorld(Vec2 gravity) {
        return new World(gravity, new DefaultWorldPool(100, 40), new BroadPhaseJNI());
      }
    });
    final TestbedController controller =
        new TestbedController(model, UpdateBehavior.UPDATE_CALLED, MouseBehavior.NORMAL,
            new TestbedErrorHandler() {
View Full Code Here

    }
    log.debug("Serialed world to " + currTest.getFilename());
  }

  private void _load() {
    World w;
    try {
      FileInputStream fis = new FileInputStream(currTest.getFilename());
      w = currTest.getDeserializer().deserializeWorld(fis);
      fis.close();
    } catch (FileNotFoundException e) {
View Full Code Here

import org.jbox2d.dynamics.World;

public class DefaultWorldCreator implements WorldCreator {
  @Override
  public World createWorld(Vec2 gravity) {
    return new World(gravity);
  }
View Full Code Here

    PbWorld world = PbWorld.parseFrom(argInput);
    return deserializeWorld(world);
  }

  public World deserializeWorld(PbWorld pbWorld) {
    World world = new World(pbToVec(pbWorld.getGravity()));

    world.setAutoClearForces(pbWorld.getAutoClearForces());
    world.setContinuousPhysics(pbWorld.getContinuousPhysics());
    world.setWarmStarting(pbWorld.getWarmStarting());
    world.setSubStepping(pbWorld.getSubStepping());

    HashMap<Integer, Body> bodyMap = new HashMap<Integer, Body>();
    HashMap<Integer, Joint> jointMap = new HashMap<Integer, Joint>();

    for (int i = 0; i < pbWorld.getBodiesCount(); i++) {
View Full Code Here

  @Override
  public void initTest(boolean deserialized) {
    if (deserialized) {
      return;
    }
    World world = getWorld();
    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;
          bd.bullet = bullet;
          fixture.shape = shape;
          fixture.density = 1f;
          fixture.filter.categoryBits = 2;
          fixture.filter.maskBits = 1 | 4 | 2;
          body = world.createBody(bd);
          body.createFixture(fixture);
        }
      }
    }

View Full Code Here

    AABB worldAABB = new AABB(new Vec2(-worldOffset, -worldOffset), new Vec2((app.width)/scale + worldOffset, (app.height)/scale + worldOffset));
    Vec2 gravity = new Vec2(0, 10);
//    Vec2 gravity = new Vec2(0, 0);
    boolean sleep = true;
    //Create the pyhsics world
    this.world = new World(worldAABB, gravity, sleep);
   
    this.registerGlobalInputProcessor(new CursorTracer(app, this));
   
    //Update the positions of the components according the the physics simulation each frame
    this.registerPreDrawAction(new UpdatePhysicsAction(world, timeStep, constraintIterations, scale));
View Full Code Here

    //Physics world dimensions
    AABB worldAABB = new AABB(new Vec2(-worldOffset, -worldOffset), new Vec2((app.width)/scale + worldOffset, (app.height)/scale + worldOffset));
    Vec2 gravity = new Vec2(0, 0);
    boolean sleep = true;
    //Create the pyhsics world
    this.world = new World(worldAABB, gravity, sleep);
   
    //Update the positions of the components according the the physics simulation each frame
    this.registerPreDrawAction(new UpdatePhysicsAction(world, timeStep, constraintIterations, scale));
   
    physicsContainer = new MTComponent(app);
View Full Code Here

  }
//  */
 
  public static void addDragJoint(World world, MTComponent comp, boolean isDynamic, float scale){
    final float worldScale = scale;
    final World theWorld = world;
   
    if (isDynamic){
      //DYNAMIC BODIES MAKE MOUSEJOINTS
      comp.removeAllGestureEventListeners(DragProcessor.class);

      comp.registerInputProcessor(new MultipleDragProcessor(comp.getRenderer()));
      comp.addGestureListener(MultipleDragProcessor.class, new IGestureEventListener() {

//        comp.addGestureListener(DragProcessor.class, new IGestureEventListener() {
        //@Override
        public boolean processGestureEvent(MTGestureEvent ge) {
          DragEvent de = (DragEvent)ge;
          try{
            MTComponent comp = (MTComponent)de.getTargetComponent();
            Body body = (Body)comp.getUserData("box2d");
            MouseJoint mouseJoint;
            Vector3D to = new Vector3D(de.getTo());
            //Un-scale position from mt4j to box2d
            PhysicsHelper.scaleDown(to, worldScale);
            //System.out.println("MouseJoint To: " + to);
            long cursorID =  de.getDragCursor().getId();

            switch (de.getId()) {
            case DragEvent.GESTURE_DETECTED:
              comp.sendToFront();
              body.wakeUp();
              mouseJoint = createDragJoint(theWorld, body, to.x, to.y);
              comp.setUserData("mouseJoint" + cursorID, mouseJoint);
              break;
            case DragEvent.GESTURE_UPDATED:
              mouseJoint = (MouseJoint) comp.getUserData("mouseJoint" + cursorID);
              if (mouseJoint != null){
                mouseJoint.setTarget(new Vec2(to.x, to.y));
              }
              break;
            case DragEvent.GESTURE_ENDED:
              mouseJoint = (MouseJoint) comp.getUserData("mouseJoint" + cursorID);
              if (mouseJoint != null){
                comp.setUserData("mouseJoint" + cursorID, null);
//                theWorld.destroyJoint(mouseJoint); 
                //Only destroy the joint if it isnt already (go through joint list and check)
                for (Joint joint = theWorld.getJointList(); joint != null; joint = joint.getNext()) {
                  JointType type = joint.getType();
                  switch (type) {
                  case MOUSE_JOINT:
                    MouseJoint mj = (MouseJoint)joint;
                    if (body.equals(mj.getBody1()) || body.equals(mj.getBody2())){
//                      theWorld.destroyJoint(mj);
                      if (mj.equals(mouseJoint)) {
                        theWorld.destroyJoint(mj);
                      }
                    }
                    break;
                  default:
                    break;
View Full Code Here

TOP

Related Classes of org.jbox2d.dynamics.World

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.