Beispiel #1
0
 /**
  * Set the linear velocity of this body
  *
  * @param xVelocity The x component of the velocity
  * @param yVelocity The y component of the velocity
  */
 public void setVelocity(float xVelocity, float yVelocity) {
   checkBody();
   Vec2 vel = jboxBody.getLinearVelocity();
   vel.x = xVelocity;
   vel.y = yVelocity;
   jboxBody.setLinearVelocity(vel);
 }
Beispiel #2
0
  private Body initPhysicsBody(World world, float x, float y) {
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.DYNAMIC;
    bodyDef.position = new Vec2(0, 0);
    Body body = world.createBody(bodyDef);

    PolygonShape shape = new PolygonShape();
    Transform fx = new Transform();
    fx.position.set(100f, 100f);
    shape.centroid(fx);

    shape.setAsBox(
        sprite.layer().width() * GameScreen.M_PER_PIXEL / 2,
        sprite.layer().height() * GameScreen.M_PER_PIXEL / 2);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    fixtureDef.density = 0.4f;
    fixtureDef.friction = 0.1f;
    fixtureDef.restitution = 0f;
    body.createFixture(fixtureDef);
    body.setLinearDamping(0.2f);
    body.setTransform(new Vec2(x, y), 0f);

    //        MassData md = body.getMassData();
    //        massD.center.set(2f, 0); body.setMassData(massD);

    return body;
  }
Beispiel #3
0
 public void setStatic(boolean isStatic) {
   if (isStatic) {
     jboxBody.m_type = org.jbox2d.dynamics.Body.e_staticType;
   } else {
     jboxBody.m_type = org.jbox2d.dynamics.Body.e_dynamicType;
   }
 }
Beispiel #4
0
 public void rdiver() {
   //        float a= 0.6f;
   //        float i;
   //        for(i=0f;i<a;i=i+0.001f){
   //        body.setTransform(body.getPosition(),i);}
   body.applyLinearImpulse(new Vec2(0f, 5f), body.getPosition());
 }
  /**
   * Call this if you want to establish collision that was previously disabled by
   * ContactFilter::ShouldCollide.
   */
  public void refilter() {
    if (m_body == null) {
      return;
    }

    // Flag associated contacts for filtering.
    ContactEdge edge = m_body.getContactList();
    while (edge != null) {
      Contact contact = edge.contact;
      Fixture fixtureA = contact.getFixtureA();
      Fixture fixtureB = contact.getFixtureB();
      if (fixtureA == this || fixtureB == this) {
        contact.flagForFiltering();
      }
      edge = edge.next;
    }

    World world = m_body.getWorld();

    if (world == null) {
      return;
    }

    // Touch each proxy so that new pairs may be created
    BroadPhase broadPhase = world.m_contactManager.m_broadPhase;
    for (int i = 0; i < m_proxyCount; ++i) {
      broadPhase.touchProxy(m_proxies[i].proxyId);
    }
  }
  @Override
  public void initTest(boolean deserialized) {
    if (deserialized) {
      return;
    }
    Body bodies[] = new Body[e_count];
    {
      BodyDef bd = new BodyDef();
      Body 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 = 1.0f;

      for (int i = 0; i < e_count; ++i) {
        BodyDef bd = new BodyDef();
        bd.type = BodyType.DYNAMIC;
        bd.position.set(0.0f, 4.0f + 3.0f * i);

        bodies[i] = getWorld().createBody(bd);

        bodies[i].createFixture(shape, 1.0f);

        // m_bodies[i].setLinearVelocity(new Vec2(0.0f, -100.0f));
      }
    }
  }
  public void addAreaObject(String worldname, String objname, LayoutArea area, ObjectProps props) {
    Box2dData data = this.mBox2dWorlds.get(worldname);
    if (data == null) {
      return;
    }

    // Dynamic Body
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.DYNAMIC;
    bodyDef.position.set(area.mLocation[0], area.mLocation[1]);
    Body body = data.mWorld.createBody(bodyDef);

    FixtureDef fixtureDef = new FixtureDef();
    PolygonShape dynamicBox = new PolygonShape();
    dynamicBox.setAsBox(area.mBounds[0] / 2, area.mBounds[1] / 2);
    fixtureDef.shape = dynamicBox;

    fixtureDef.density = props.mDensity;
    fixtureDef.friction = props.mFriction;
    fixtureDef.restitution = props.mRestitution;
    // kinematicBody
    fixtureDef.filter.groupIndex = props.mGroupIndex;
    body.createFixture(fixtureDef);

    BodyData bodydata = new BodyData();
    bodydata.mArea = area;
    bodydata.mBody = body;
    bodydata.mProps = props;
    area.assignPsyData(bodydata);
    data.mAreaModels.add(bodydata);
  }
Beispiel #8
0
  @Override
  public void create() {
    {
      PolygonDef sd = new PolygonDef();
      sd.setAsBox(50.0f, 10.0f);

      BodyDef bd = new BodyDef();
      bd.position = new Vec2(0.0f, -10.0f);
      m_world.createStaticBody(bd).createShape(sd);
    }

    {
      PolygonDef sd = new PolygonDef();
      float w = 4.0f;
      float h = 0.25f;
      sd.setAsBox(w, h);
      sd.density = 1.0f;
      sd.friction = 0.3f;
      sd.restitution = 0.0f;

      BodyDef bd = new BodyDef();

      int numSlats = 8;
      float lastCMX = 0.0f;
      float eps = 0.14f;
      for (int i = 0; i < numSlats; ++i) {
        float newX = lastCMX + w - eps;
        lastCMX = (i * lastCMX + newX) / (i + 1);
        bd.position = new Vec2(newX, .25f + 2 * h * (numSlats - i - 1));
        Body myBody = m_world.createDynamicBody(bd);
        myBody.createShape(sd);
        myBody.setMassFromShapes();
      }
    }
  }
  public void initTest(boolean argDeserialized) {
    if (argDeserialized) {
      return;
    }

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

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

    { // Platforms
      for (int i = 0; i < 4; i++) {
        FixtureDef fd = new FixtureDef();
        PolygonShape sd = new PolygonShape();
        sd.setAsBox(25.0f, 0.125f);
        fd.shape = sd;

        BodyDef bd = new BodyDef();
        bd.position = new Vec2(0.0f, 5f + 5f * i);
        getWorld().createBody(bd).createFixture(fd);
      }
    }

    {
      FixtureDef fd = new FixtureDef();
      PolygonShape sd = new PolygonShape();
      sd.setAsBox(0.125f, 2f);
      fd.shape = sd;
      fd.density = 25.0f;

      BodyDef bd = new BodyDef();
      bd.type = BodyType.DYNAMIC;
      float friction = .5f;
      int numPerRow = 25;

      for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < numPerRow; j++) {
          fd.friction = friction;
          bd.position = new Vec2(-14.75f + j * (29.5f / (numPerRow - 1)), 7.3f + 5f * i);
          if (i == 2 && j == 0) {
            bd.angle = -0.1f;
            bd.position.x += .1f;
          } else if (i == 3 && j == numPerRow - 1) {
            bd.angle = .1f;
            bd.position.x -= .1f;
          } else bd.angle = 0f;
          Body myBody = getWorld().createBody(bd);
          myBody.createFixture(fd);
        }
      }
    }
  }
Beispiel #10
0
  /**
   * create a joint to constrain bodies together. No reference to the definition is retained. This
   * may cause the connected bodies to cease colliding.
   *
   * @warning This function is locked during callbacks.
   * @param def
   * @return
   */
  public Joint createJoint(JointDef def) {
    assert (isLocked() == false);
    if (isLocked()) {
      return null;
    }

    Joint j = Joint.create(this, def);

    // Connect to the world list.
    j.m_prev = null;
    j.m_next = m_jointList;
    if (m_jointList != null) {
      m_jointList.m_prev = j;
    }
    m_jointList = j;
    ++m_jointCount;

    // Connect to the bodies' doubly linked lists.
    j.m_edgeA.joint = j;
    j.m_edgeA.other = j.m_bodyB;
    j.m_edgeA.prev = null;
    j.m_edgeA.next = j.m_bodyA.m_jointList;
    if (j.m_bodyA.m_jointList != null) {
      j.m_bodyA.m_jointList.prev = j.m_edgeA;
    }
    j.m_bodyA.m_jointList = j.m_edgeA;

    j.m_edgeB.joint = j;
    j.m_edgeB.other = j.m_bodyA;
    j.m_edgeB.prev = null;
    j.m_edgeB.next = j.m_bodyB.m_jointList;
    if (j.m_bodyB.m_jointList != null) {
      j.m_bodyB.m_jointList.prev = j.m_edgeB;
    }
    j.m_bodyB.m_jointList = j.m_edgeB;

    Body bodyA = def.bodyA;
    Body bodyB = def.bodyB;

    // If the joint prevents collisions, then flag any contacts for filtering.
    if (def.collideConnected == false) {
      ContactEdge edge = bodyB.getContactList();
      while (edge != null) {
        if (edge.other == bodyA) {
          // Flag the contact for filtering at the next time step (where either
          // body is awake).
          edge.contact.flagForFiltering();
        }

        edge = edge.next;
      }
    }

    // Note: creating a joint doesn't wake the bodies.

    return j;
  }
Beispiel #11
0
 public boolean reportFixture(Fixture fixture) {
   Body body = fixture.getBody();
   if (body.getType() == BodyType.DYNAMIC) {
     boolean inside = fixture.testPoint(point);
     if (inside) {
       this.fixture = fixture;
       return false;
     }
   }
   return true;
 }
 public void setCockpit(Body newCockpit) {
   if (cockpit != null) {
     if (cockpit.getUserData() instanceof ControlledBodyData) {
       ((ControlledBodyData) cockpit.getUserData()).removeSubPart(body);
     }
   }
   if (newCockpit != null) {
     ((ControlledBodyData) cockpit.getUserData()).addSubPart(body);
   }
   cockpit = newCockpit;
   calcDistFromCockpit();
 }
Beispiel #13
0
  public void setAllowSleep(boolean flag) {
    if (flag == m_allowSleep) {
      return;
    }

    m_allowSleep = flag;
    if (m_allowSleep == false) {
      for (Body b = m_bodyList; b != null; b = b.m_next) {
        b.setAwake(true);
      }
    }
  }
Beispiel #14
0
  /**
   * Notification that this body is being added to the world
   *
   * @param world The world this body is being added to
   */
  void addToWorld(World world) {
    org.jbox2d.dynamics.World jboxWorld = world.getJBoxWorld();

    jboxBody = jboxWorld.createBody(jboxBodyDef);
    shape.createInBody(this);

    if (!staticBody) {
      jboxBody.setMassFromShapes();
    } else {
      jboxBody.m_type = org.jbox2d.dynamics.Body.e_staticType;
    }
  }
 @Override
 public void destroy() {
   Body b = getBody();
   if (b != null) {
     b.getWorld().destroyBody(getBody());
   }
   if (getLayer() != null) {
     GroupLayer parent = getLayer().parent();
     if (parent != null) {
       parent.remove(getLayer());
     }
   }
 }
 public double depositResource(String name, double amount, boolean acceptPartial) {
   if (cockpit != null && cockpit != body) {
     if (cockpit.getUserData() instanceof ControlledBodyData) {
       ControlledBodyData data = (ControlledBodyData) cockpit.getUserData();
       return data.depositResource(name, amount, acceptPartial);
     }
   }
   if (acceptPartial) {
     double collected = addResource(name, amount, true);
     int size = subparts.size();
     for (int a = 0; a < size && collected < amount; a++) {
       Body subpart = subparts.get(a);
       if (subpart.getUserData() instanceof ControlledBodyData) {
         ControlledBodyData data = (ControlledBodyData) subpart.getUserData();
         collected += data.addResource(name, amount - collected, true);
       }
     }
     return collected;
   } else {
     double deposited = addResource(name, amount, false);
     if (deposited != 0) return amount;
     int size = subparts.size();
     for (int a = 0; a < size; a++) {
       Body subpart = subparts.get(a);
       if (subpart.getUserData() instanceof ControlledBodyData) {
         ControlledBodyData data = (ControlledBodyData) subpart.getUserData();
         if (data.allowedResource(name)) {
           deposited = data.addResource(name, amount, false);
           if (deposited != 0) return amount;
         }
       }
     }
     return 0;
   }
 }
 public double requestResource(String name, double amount, boolean acceptPartial) {
   if (cockpit != null && cockpit != body) {
     if (cockpit.getUserData() instanceof ControlledBodyData) {
       ControlledBodyData data = (ControlledBodyData) cockpit.getUserData();
       return data.requestResource(name, amount, acceptPartial);
     }
   }
   if (acceptPartial) {
     double collected = removeResource(name, amount);
     int size = subparts.size();
     for (int a = 0; a < size && collected < amount; a++) {
       Body subpart = subparts.get(a);
       if (subpart.getUserData() instanceof ControlledBodyData) {
         ControlledBodyData data = (ControlledBodyData) subpart.getUserData();
         collected += data.removeResource(name, amount);
       }
     }
     return collected;
   } else {
     if (getResource(name).amount >= amount) return removeResource(name, amount);
     int size = subparts.size();
     for (int a = 0; a < size; a++) {
       Body subpart = subparts.get(a);
       if (subpart.getUserData() instanceof ControlledBodyData) {
         ControlledBodyData data = (ControlledBodyData) subpart.getUserData();
         if (data.getResource(name).amount >= amount) return data.removeResource(name, amount);
       }
     }
     return 0;
   }
 }
Beispiel #18
0
  public void paint(Clock clock) {
    if (!hasLoaded) return;

    // body.setLinearVelocity(new Vec2(30 * MathUtils.cos(GameScreen.angle), 30 *
    // MathUtils.sin(GameScreen.angle)));

    sprite
        .layer()
        .setTranslation(
            (body.getPosition().x / GameScreen.M_PER_PIXEL),
            (body.getPosition().y / GameScreen.M_PER_PIXEL));
    Vec2 delta = new Vec2(80f - body.getPosition().x, 400f - body.getPosition().y);
    float angle = MathUtils.atan2(delta.x, delta.y);
    body.setLinearVelocity(new Vec2(8 * -MathUtils.cos(angle), 8 * MathUtils.sin(angle)));
  }
 public void startTick() {
   if (parent != null) {
     if (parent.getUserData() instanceof LivingBodyData) {
       if (((LivingBodyData) parent.getUserData()).health <= 0) health = 0;
     } else health = 0;
   }
   if (health <= 0) return;
   if (scripts == null) return;
   try {
     Game.e.put("scriptid", scripts.data.UUID);
     Game.e.eval(scripts.tickScript);
   } catch (ScriptException e) {
     e.printStackTrace();
   }
 }
Beispiel #20
0
  /** 创建多边形物体 */
  private void createBody() {
    // 定义物体的形状是多边形形状
    PolygonShape shape = new PolygonShape();
    shape.set(vecs, edge);

    // 设置多边形物体的一些固定的物理属性
    FixtureDef fd = new FixtureDef();
    fd.shape = shape;
    // 固定设置参数
    fd.density = 5.0f; // 密度
    fd.friction = 0.3f; // 摩擦系数
    fd.restitution = restitution; // 恢复系数

    BodyDef bd = new BodyDef();
    bd.position.set(x, y);
    bd.type = BodyType.DYNAMIC;
    bd.angle = angle;
    bd.allowSleep = true;
    bd.setAwake(true);

    // 根据bodyDef创建物体到世界中
    body = world.createBody(bd);
    // 设置好物体的固定属性
    body.createFixture(fd);
  }
 public void calcDistFromCockpit() {
   if (cockpit == null) return;
   distFromCockpit = -1;
   if (cockpit == body) distFromCockpit = 0;
   for (JointEdge j = body.getJointList(); j != null; j = j.next) {
     Body otherBody = j.other;
     if (otherBody.getUserData() instanceof ControlledBodyData) {
       ControlledBodyData data = (ControlledBodyData) otherBody.getUserData();
       if (data.cockpit == cockpit && data.distFromCockpit != -1) {
         data.calcDistFromCockpit();
         if (data.distFromCockpit != -1 && data.distFromCockpit < distFromCockpit)
           distFromCockpit = data.distFromCockpit;
       }
     }
   }
   if (distFromCockpit == -1) setCockpit(null);
 }
Beispiel #22
0
  void playerControl(LudumDare26Game.ControlsState controlsState) {
    boolean doStop = !(controlsState.leftPressed || controlsState.rightPressed);

    final Body body = getBody();
    float linearDamping = 0;
    if (doStop) {
      body.setAngularVelocity(0);
      //                linearDamping = 10f;
      Vec2 linearVelocity = body.getLinearVelocity();
      linearVelocity.x *= 0.9;
      //                body.applyForce(new Vec2(v, 0), body.getPosition());
      body.setLinearVelocity(linearVelocity);
    } else {
      Vec2 linearVelocity = body.getLinearVelocity();
      final int dir = (controlsState.leftPressed ? -1 : 0) + (controlsState.rightPressed ? 1 : 0);
      float v = linearVelocity.x;
      v += dir * MOVE_ACC;
      v = Math.max(-MOVE_SPEED, Math.min(v, MOVE_SPEED));
      linearVelocity.x = v;
      //                body.applyForce(new Vec2(v, 0), body.getPosition());
      body.setLinearVelocity(linearVelocity);
      //                linearDamping = 1f;
    }
    body.setLinearDamping(linearDamping);
  }
Beispiel #23
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);}

  }
  public Planetoid(Sprite s, BodyDef bodyDef, World world, float mass, float forceFactor) {

    this.sprite = s;
    this.pBody = world.createBody(bodyDef);

    CircleShape circle = new CircleShape();

    circle.m_radius = this.sprite.getWidth() / Globals.PHYS_RATIO / Globals.magicBoundRatio;

    FixtureDef fixDef = new FixtureDef();

    fixDef.shape = circle;

    pBody.createFixture(fixDef);
    pBody.m_mass = mass;

    this.forceFactor = forceFactor;
  }
 @Override
 public void move(Body body) {
   Objects.requireNonNull(body);
   if (!hasBeenCalled) {
     Vec2 v2 = new Vec2(0.0f, 2.0f);
     body.setLinearVelocity(v2);
     hasBeenCalled = true;
   }
 }
Beispiel #26
0
  /**
   * create a rigid body given a definition. No reference to the definition is retained.
   *
   * @warning This function is locked during callbacks.
   * @param def
   * @return
   */
  public Body createBody(BodyDef def) {
    assert (isLocked() == false);
    if (isLocked()) {
      return null;
    }
    // TODO djm pooling
    Body b = new Body(def, this);

    // add to world doubly linked list
    b.m_prev = null;
    b.m_next = m_bodyList;
    if (m_bodyList != null) {
      m_bodyList.m_prev = b;
    }
    m_bodyList = b;
    ++m_bodyCount;

    return b;
  }
Beispiel #27
0
  @Override
  Body initPhysicsBody(World world, float x, float y, float width, float height, float angle) {
    FixtureDef fixtureDef = new FixtureDef();
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.DYNAMIC;
    bodyDef.position = new Vec2(0, 0);
    Body body = world.createBody(bodyDef);

    CircleShape circleShape = new CircleShape();
    circleShape.m_radius = RADIUS;
    fixtureDef.shape = circleShape;
    fixtureDef.density = 0.4f;
    fixtureDef.friction = 1f;
    fixtureDef.restitution = 0.0f;
    circleShape.m_p.set(0, 0);
    body.createFixture(fixtureDef);
    //        body.setLinearDamping(0.2f);
    body.setTransform(new Vec2(x, y), angle);
    return body;
  }
Beispiel #28
0
  private void drawJoint(Joint joint) {
    Body bodyA = joint.getBodyA();
    Body bodyB = joint.getBodyB();
    Transform xf1 = bodyA.getTransform();
    Transform xf2 = bodyB.getTransform();
    Vec2 x1 = xf1.p;
    Vec2 x2 = xf2.p;
    Vec2 p1 = pool.popVec2();
    Vec2 p2 = pool.popVec2();
    joint.getAnchorA(p1);
    joint.getAnchorB(p2);

    color.set(0.5f, 0.8f, 0.8f);

    switch (joint.getType()) {
        // TODO djm write after writing joints
      case DISTANCE:
        m_debugDraw.drawSegment(p1, p2, color);
        break;

      case PULLEY:
        {
          PulleyJoint pulley = (PulleyJoint) joint;
          Vec2 s1 = pulley.getGroundAnchorA();
          Vec2 s2 = pulley.getGroundAnchorB();
          m_debugDraw.drawSegment(s1, p1, color);
          m_debugDraw.drawSegment(s2, p2, color);
          m_debugDraw.drawSegment(s1, s2, color);
        }
        break;
      case CONSTANT_VOLUME:
      case MOUSE:
        // don't draw this
        break;
      default:
        m_debugDraw.drawSegment(x1, p1, color);
        m_debugDraw.drawSegment(p1, p2, color);
        m_debugDraw.drawSegment(x2, p2, color);
    }
    pool.pushVec2(2);
  }
Beispiel #29
0
 @Override
 public boolean onTouchEvent(MotionEvent event) {
   Vec2 p = new Vec2(event.getX(), event.getY());
   switch (event.getAction()) {
     case MotionEvent.ACTION_DOWN:
       break;
     case MotionEvent.ACTION_UP:
       if (mouseJoint != null) {
         myWorld.getWorld().destroyJoint(mouseJoint);
         mouseJoint = null;
       }
       break;
     case MotionEvent.ACTION_MOVE:
       if (mouseJoint != null) {
         mouseJoint.setTarget(p);
         break;
       }
       queryAABB.lowerBound.set(p.x - .001f, p.y - .001f);
       queryAABB.upperBound.set(p.x + .001f, p.y + .001f);
       callback.point.set(p);
       callback.fixture = null;
       myWorld.getWorld().queryAABB(callback, queryAABB);
       if (callback.fixture != null) {
         Body body = callback.fixture.getBody();
         XKPPhysicBody physicBody = (XKPPhysicBody) body.getUserData();
         if (!physicBody.getMouseJoint()) break;
         MouseJointDef def = new MouseJointDef();
         def.bodyA = groundBody;
         def.bodyB = body;
         def.target.set(p);
         def.maxForce = 3000f * body.getMass();
         def.dampingRatio = 0;
         def.frequencyHz = 1000;
         mouseJoint = (MouseJoint) myWorld.getWorld().createJoint(def);
         body.setAwake(true);
       }
       break;
   }
   return super.onTouchEvent(event);
 }
  @Override
  public void move(Body body) {
    Objects.requireNonNull(body);

    Vec2 v2 = new Vec2(2f, 0f);
    switch (nbCall) {
      case -1:
        nbCall++;
        break;
      case 0:
        if (body.getPosition().y * EscapeWorld.SCALE > (FrontApplication.WIDTH / 3 - 120)) {
          return;
        }
        v2.set(2f, 0f);
        break;
      case 1:
        if (body.getPosition().x * EscapeWorld.SCALE < (FrontApplication.WIDTH / 3)) {
          return;
        }
        v2.set(0f, 2f);
        break;
      case 2:
        if (body.getPosition().y * EscapeWorld.SCALE < (FrontApplication.WIDTH / 3)) {
          return;
        }
        v2.set(-2f, 0f);
        break;
      case 3:
        if (body.getPosition().x * EscapeWorld.SCALE > (FrontApplication.WIDTH / 3 - 120)) {
          return;
        }
        v2.set(0f, -2f);
        break;
      default:
        throw new AssertionError();
    }

    body.setLinearVelocity(v2);
    nbCall = (nbCall + 1) % 4;
  }