Esempio n. 1
0
 public boolean processGestureEvent(MTGestureEvent ge) {
   DragEvent de = (DragEvent) ge;
   try {
     Body body = (Body) comp.getUserData("box2d");
     MouseJoint mouseJoint;
     Vector3D to = new Vector3D(de.getTo());
     // Un-scale position from mt4j to box2d
     PhysicsHelper.scaleDown(to, scale);
     switch (de.getId()) {
       case DragEvent.GESTURE_STARTED:
         comp.sendToFront();
         body.wakeUp();
         body.setXForm(new Vec2(to.x, to.y), body.getAngle());
         mouseJoint = PhysicsHelper.createDragJoint(world, body, to.x, to.y);
         comp.setUserData(comp.getID(), mouseJoint);
         break;
       case DragEvent.GESTURE_UPDATED:
         mouseJoint = (MouseJoint) comp.getUserData(comp.getID());
         if (mouseJoint != null) {
           boolean onCorrectGameSide =
               ((MTComponent) de.getTarget()).containsPointGlobal(de.getTo());
           // System.out.println(((MTComponent)de.getTargetComponent()).getName()  + " Contains
           // " + to + " -> " + contains);
           if (onCorrectGameSide) {
             mouseJoint.setTarget(new Vec2(to.x, to.y));
           }
         }
         break;
       case DragEvent.GESTURE_ENDED:
         mouseJoint = (MouseJoint) comp.getUserData(comp.getID());
         if (mouseJoint != null) {
           comp.setUserData(comp.getID(), null);
           // Only destroy the joint if it isnt already (go through joint list and check)
           for (Joint joint = world.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())) {
                   if (mj.equals(mouseJoint)) {
                     world.destroyJoint(mj);
                   }
                 }
                 break;
               default:
                 break;
             }
           }
         }
         mouseJoint = null;
         break;
       default:
         break;
     }
   } catch (Exception e) {
     System.err.println(e.getMessage());
   }
   return false;
 }
Esempio n. 2
0
  public void update(int delta) {
    if (!hasLoaded) return;
    sprite.layer().setRotation(body.getAngle());
    //        float a= 0.6f;
    //        float i;
    //        for(i=0f;i<a;i=i+0.1f){
    //            body.setTransform(body.getPosition(),i);}

  }
Esempio n. 3
0
  // Drawing the box
  void display() {
    // We look at each body and get its screen position
    Vec2 pos = box2d.getBodyPixelCoord(body);
    // Get its angle of rotation
    float a = body.getAngle();

    parent.rectMode(PConstants.CENTER);
    parent.pushMatrix();
    parent.translate(pos.x, pos.y);
    parent.rotate(-a);
    parent.fill(127);
    parent.stroke(0);
    parent.strokeWeight(2);
    parent.rect(0, 0, w, h);
    parent.popMatrix();
  }
Esempio n. 4
0
File: Body.java Progetto: rdo/speleo
 /**
  * Set the position of the body. This can only be called after the body has been added to the
  * world.
  *
  * @param x The new x coordinate of the body
  * @param y The new y coordinate of the body
  */
 public void setPosition(float x, float y) {
   checkBody();
   jboxBody.setXForm(new Vec2(x, y), jboxBody.getAngle());
 }
Esempio n. 5
0
File: Body.java Progetto: rdo/speleo
 /**
  * Get the rotation of the body
  *
  * @return The rotation of the body
  */
 public float getRotation() {
   checkBody();
   return jboxBody.getAngle();
 }
Esempio n. 6
0
 public float getAngle() {
   return body.getAngle();
 }
Esempio n. 7
0
  public void start() {

    Vec2 gravity = new Vec2(0, 0);
    World world = new World(gravity);
    BodyDef groundBodyDef = new BodyDef();
    groundBodyDef.position.set(0, -25);
    Body groundBody = world.createBody(groundBodyDef);
    PolygonShape groundBox = new PolygonShape();
    groundBox.setAsBox(900, 10);
    groundBody.createFixture(groundBox, 0);

    // Dynamic Body
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.DYNAMIC;
    bodyDef.position.set(0, 0);
    Body body = world.createBody(bodyDef);
    PolygonShape dynamicBox = new PolygonShape();
    dynamicBox.setAsBox(12, 12);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = dynamicBox;
    fixtureDef.density = 1f;
    fixtureDef.friction = 0.3f;
    body.createFixture(fixtureDef);

    // Setup world
    float timeStep = 1.0f / 60.0f;
    int velocityIterations = 6;
    int positionIterations = 2;
    body.setLinearVelocity(new Vec2(15.0f, -15.0f));
    body.setLinearDamping(2f);
    Vec2 f = body.getWorldVector(new Vec2(0.0f, -30.0f));
    Vec2 p = body.getWorldPoint(body.getLocalCenter().add(new Vec2(-.2f, 0f)));
    // body.applyForce(new Vec2(-200,-200),new Vec2(-200,200));

    try {
      Display.setDisplayMode(new DisplayMode(screen_width, screen_height));
      Display.create();
    } catch (LWJGLException e) {
      e.printStackTrace();
      System.exit(0);
    }

    lastFPS = getTime();

    // standardBall[0] = new Ball(400,200,0,12);
    // standardBall[1] = new Ball(100,210,2,12);
    // standardBall[0].addImpulse(-10,0);

    standardBall[0] = new Ball(200, 300, 0, 12);
    standardBall[1] = new Ball(415, 100, 2, 12);
    standardBall[0].addImpulse(10, -9.99f);

    standardBall[2] = new Ball(620, 270, 2, 12);
    standardBall[3] = new Ball(640, 290, 2, 12);
    standardBall[4] = new Ball(660, 310, 2, 12);
    standardBall[5] = new Ball(680, 330, 2, 12);

    standardBall[6] = new Ball(620, 230, 2, 12);
    standardBall[7] = new Ball(640, 210, 2, 12);
    standardBall[8] = new Ball(660, 190, 2, 12);
    standardBall[9] = new Ball(680, 170, 2, 12);

    standardBall[10] = new Ball(640, 250, 1, 12);
    standardBall[11] = new Ball(660, 230, 2, 12);
    standardBall[12] = new Ball(660, 270, 2, 12);
    standardBall[13] = new Ball(680, 290, 2, 12);
    standardBall[14] = new Ball(680, 210, 2, 12);
    standardBall[15] = new Ball(680, 250, 2, 12);

    standardTable = new Table(0, 0, 800, 400, 25, 15);

    // init OpenGL
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GL11.glOrtho(0, 900, 0, 500, 1, -1);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);

    while (!Display.isCloseRequested()) {
      int delta = getDelta();
      world.step(timeStep, velocityIterations, positionIterations);
      Vec2 position = body.getPosition();
      float angle = body.getAngle();
      // System.out.printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);

      // Clear the screen and depth buffer
      GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);

      standardTable.draw();

      for (int i = 0; i < NUMBER_OF_BALLS; i++) {
        standardBall[i].draw();
        standardBall[i].update(delta);
      }
      standardBall[0].setX((position.x * 20) + offset_x);
      standardBall[0].setY((position.y * 20) + offset_y);

      update(delta);

      Display.update();

      Display.sync(60);
    }
    Display.destroy();
  }
Esempio n. 8
0
  @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(50.0f, 0.0f), new Vec2(-50.0f, 0.0f));
      ground.createFixture(shape, 0.0f);
    }

    {
      CircleShape circle1 = new CircleShape();
      circle1.m_radius = 1.0f;

      PolygonShape box = new PolygonShape();
      box.setAsBox(0.5f, 5.0f);

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

      BodyDef bd1 = new BodyDef();
      bd1.type = BodyType.STATIC;
      bd1.position.set(10.0f, 9.0f);
      Body body1 = m_world.createBody(bd1);
      body1.createFixture(circle1, 5.0f);

      BodyDef bd2 = new BodyDef();
      bd2.type = BodyType.DYNAMIC;
      bd2.position.set(10.0f, 8.0f);
      Body body2 = m_world.createBody(bd2);
      body2.createFixture(box, 5.0f);

      BodyDef bd3 = new BodyDef();
      bd3.type = BodyType.DYNAMIC;
      bd3.position.set(10.0f, 6.0f);
      Body body3 = m_world.createBody(bd3);
      body3.createFixture(circle2, 5.0f);

      RevoluteJointDef jd1 = new RevoluteJointDef();
      jd1.initialize(body2, body1, bd1.position);
      Joint joint1 = m_world.createJoint(jd1);

      RevoluteJointDef jd2 = new RevoluteJointDef();
      jd2.initialize(body2, body3, bd3.position);
      Joint joint2 = m_world.createJoint(jd2);

      GearJointDef jd4 = new GearJointDef();
      jd4.bodyA = body1;
      jd4.bodyB = body3;
      jd4.joint1 = joint1;
      jd4.joint2 = joint2;
      jd4.ratio = circle2.m_radius / circle1.m_radius;
      m_world.createJoint(jd4);
    }

    {
      CircleShape circle1 = new CircleShape();
      circle1.m_radius = 1.0f;

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

      PolygonShape box = new PolygonShape();
      box.setAsBox(0.5f, 5.0f);

      BodyDef bd1 = new BodyDef();
      bd1.type = BodyType.DYNAMIC;
      bd1.position.set(-3.0f, 12.0f);
      Body body1 = m_world.createBody(bd1);
      body1.createFixture(circle1, 5.0f);

      RevoluteJointDef jd1 = new RevoluteJointDef();
      jd1.bodyA = ground;
      jd1.bodyB = body1;
      ground.getLocalPointToOut(bd1.position, jd1.localAnchorA);
      body1.getLocalPointToOut(bd1.position, jd1.localAnchorB);
      jd1.referenceAngle = body1.getAngle() - ground.getAngle();
      m_joint1 = (RevoluteJoint) m_world.createJoint(jd1);

      BodyDef bd2 = new BodyDef();
      bd2.type = BodyType.DYNAMIC;
      bd2.position.set(0.0f, 12.0f);
      Body body2 = m_world.createBody(bd2);
      body2.createFixture(circle2, 5.0f);

      RevoluteJointDef jd2 = new RevoluteJointDef();
      jd2.initialize(ground, body2, bd2.position);
      m_joint2 = (RevoluteJoint) m_world.createJoint(jd2);

      BodyDef bd3 = new BodyDef();
      bd3.type = BodyType.DYNAMIC;
      bd3.position.set(2.5f, 12.0f);
      Body body3 = m_world.createBody(bd3);
      body3.createFixture(box, 5.0f);

      PrismaticJointDef jd3 = new PrismaticJointDef();
      jd3.initialize(ground, body3, bd3.position, new Vec2(0.0f, 1.0f));
      jd3.lowerTranslation = -5.0f;
      jd3.upperTranslation = 5.0f;
      jd3.enableLimit = true;

      m_joint3 = (PrismaticJoint) m_world.createJoint(jd3);

      GearJointDef jd4 = new GearJointDef();
      jd4.bodyA = body1;
      jd4.bodyB = body2;
      jd4.joint1 = m_joint1;
      jd4.joint2 = m_joint2;
      jd4.ratio = circle2.m_radius / circle1.m_radius;
      m_joint4 = (GearJoint) m_world.createJoint(jd4);

      GearJointDef jd5 = new GearJointDef();
      jd5.bodyA = body2;
      jd5.bodyB = body3;
      jd5.joint1 = m_joint2;
      jd5.joint2 = m_joint3;
      jd5.ratio = 1f / circle2.m_radius;
      m_joint5 = (GearJoint) m_world.createJoint(jd5);
    }
  }