Package org.jbox2d.dynamics

Examples of org.jbox2d.dynamics.Body


    public boolean reportFixture(Fixture fixture) {
      if (fixture.isSensor()) {
        return true;
      }
      final Shape shape = fixture.getShape();
      Body body = fixture.getBody();
      int childCount = shape.getChildCount();
      for (int childIndex = 0; childIndex < childCount; childIndex++) {
        AABB aabb = fixture.getAABB(childIndex);
        final float aabblowerBoundx = aabb.lowerBound.x - system.m_particleDiameter;
        final float aabblowerBoundy = aabb.lowerBound.y - system.m_particleDiameter;
        final float aabbupperBoundx = aabb.upperBound.x + system.m_particleDiameter;
        final float aabbupperBoundy = aabb.upperBound.y + system.m_particleDiameter;
        int firstProxy =
            lowerBound(
                system.m_proxyBuffer,
                system.m_proxyCount,
                computeTag(system.m_inverseDiameter * aabblowerBoundx, system.m_inverseDiameter
                    * aabblowerBoundy));
        int lastProxy =
            upperBound(
                system.m_proxyBuffer,
                system.m_proxyCount,
                computeTag(system.m_inverseDiameter * aabbupperBoundx, system.m_inverseDiameter
                    * aabbupperBoundy));

        for (int proxy = firstProxy; proxy != lastProxy; ++proxy) {
          int a = system.m_proxyBuffer[proxy].index;
          Vec2 ap = system.m_positionBuffer.data[a];
          if (aabblowerBoundx <= ap.x && ap.x <= aabbupperBoundx && aabblowerBoundy <= ap.y
              && ap.y <= aabbupperBoundy) {
            Vec2 av = system.m_velocityBuffer.data[a];
            final Vec2 temp = tempVec;
            Transform.mulTransToOutUnsafe(body.m_xf0, ap, temp);
            Transform.mulToOutUnsafe(body.m_xf, temp, input.p1);
            input.p2.x = ap.x + step.dt * av.x;
            input.p2.y = ap.y + step.dt * av.y;
            input.maxFraction = 1;
            if (fixture.raycast(output, input, childIndex)) {
              final Vec2 p = tempVec;
              p.x =
                  (1 - output.fraction) * input.p1.x + output.fraction * input.p2.x
                      + Settings.linearSlop * output.normal.x;
              p.y =
                  (1 - output.fraction) * input.p1.y + output.fraction * input.p2.y
                      + Settings.linearSlop * output.normal.y;

              final float vx = step.inv_dt * (p.x - ap.x);
              final float vy = step.inv_dt * (p.y - ap.y);
              av.x = vx;
              av.y = vy;
              final float particleMass = system.getParticleMass();
              final float ax = particleMass * (av.x - vx);
              final float ay = particleMass * (av.y - vy);
              Vec2 b = output.normal;
              final float fdn = ax * b.x + ay * b.y;
              final Vec2 f = tempVec2;
              f.x = fdn * b.x;
              f.y = fdn * b.y;
              body.applyLinearImpulse(f, p, true);
            }
          }
        }
      }
      return true;
View Full Code Here


    return m_dampingRatio;
  }

  @Override
  public void initVelocityConstraints(TimeStep step) {
    Body b = m_bodyB;

    float mass = b.getMass();

    // Frequency
    float omega = 2.0f * MathUtils.PI * m_frequencyHz;

    // Damping coefficient
    float d = 2.0f * mass * m_dampingRatio * omega;

    // Spring stiffness
    float k = mass * (omega * omega);

    // magic formulas
    // gamma has units of inverse mass.
    // beta has units of inverse time.
    assert(d + step.dt * k > Settings.EPSILON);
    m_gamma = step.dt * (d + step.dt * k);
    if (m_gamma != 0.0f){
      m_gamma = 1.0f / m_gamma;
    }
    m_beta = step.dt * k * m_gamma;

    Vec2 r = pool.popVec2();
   
    // Compute the effective mass matrix.
    //Vec2 r = Mul(b.getTransform().R, m_localAnchor - b.getLocalCenter());
    r.set(m_localAnchor).subLocal(b.getLocalCenter());
    Mat22.mulToOut(b.getTransform().R, r, r);
   
    // K    = [(1/m1 + 1/m2) * eye(2) - skew(r1) * invI1 * skew(r1) - skew(r2) * invI2 * skew(r2)]
    //      = [1/m1+1/m2     0    ] + invI1 * [r1.y*r1.y -r1.x*r1.y] + invI2 * [r1.y*r1.y -r1.x*r1.y]
    //        [    0     1/m1+1/m2]           [-r1.x*r1.y r1.x*r1.x]           [-r1.x*r1.y r1.x*r1.x]
    float invMass = b.m_invMass;
View Full Code Here

    return true;
  }

  @Override
  public void solveVelocityConstraints(TimeStep step) {
    Body b = m_bodyB;

    Vec2 r = pool.popVec2();

    r.set(m_localAnchor).subLocal(b.getLocalCenter());
    Mat22.mulToOut(b.getTransform().R, r, r);
   
    // Cdot = v + cross(w, r)
    Vec2 Cdot = pool.popVec2();
    Vec2.crossToOut(b.m_angularVelocity, r, Cdot);
    Cdot.addLocal(b.m_linearVelocity);
View Full Code Here

  public float getReactionTorque(float inv_dt) {
    return 0.0f;
  }
 
  public float getJointTranslation() {
    Body b1 = m_bodyA;
    Body b2 = m_bodyB;
   
    Vec2 p1 = pool.popVec2();
    Vec2 p2 = pool.popVec2();
    Vec2 axis = pool.popVec2();
    b1.getWorldPointToOut(m_localAnchor1, p1);
    b2.getWorldPointToOut(m_localAnchor1, p2);
    p2.subLocal(p1);
    b1.getWorldVectorToOut(m_localXAxis1, axis);
   
    float translation = Vec2.dot(p2, axis);
    pool.pushVec2(3);
View Full Code Here

    pool.pushVec2(3);
    return translation;
  }
 
  public float getJointSpeed() {
    Body b1 = m_bodyA;
    Body b2 = m_bodyB;
   
    final Vec2 r1 = pool.popVec2();
    final Vec2 r2 = pool.popVec2();
    final Vec2 p1 = pool.popVec2();
    final Vec2 p2 = pool.popVec2();
   
    r1.set(m_localAnchor1).subLocal(b1.getLocalCenter());
    r2.set(m_localAnchor2).subLocal(b2.getLocalCenter());
    Mat22.mulToOut(b1.getTransform().R, r1, r1);
    Mat22.mulToOut(b2.getTransform().R, r2, r2);
   
    p1.set(b1.m_sweep.c).addLocal(r1);
    p2.set(b2.m_sweep.c).addLocal(r2);
    p2.subLocal(p1);
   
View Full Code Here

  /**
   * @see org.jbox2d.dynamics.joints.Joint#initVelocityConstraints(org.jbox2d.dynamics.TimeStep)
   */
  @Override
  public void initVelocityConstraints(TimeStep step) {
    Body b1 = m_bodyA;
    Body b2 = m_bodyB;
   
    m_localCenterA.set(b1.getLocalCenter());
    m_localCenterB.set(b2.getLocalCenter());
   
    Transform xf1 = b1.getTransform();
    Transform xf2 = b2.getTransform();
   
    // Compute the effective masses.
    final Vec2 r1 = pool.popVec2();
    final Vec2 r2 = pool.popVec2();
    final Vec2 temp = pool.popVec2();
View Full Code Here

  /**
   * @see org.jbox2d.dynamics.joints.Joint#solveVelocityConstraints(org.jbox2d.dynamics.TimeStep)
   */
  @Override
  public void solveVelocityConstraints(TimeStep step) {
    Body b1 = m_bodyA;
    Body b2 = m_bodyB;
   
    final Vec2 v1 = b1.m_linearVelocity;
    float w1 = b1.m_angularVelocity;
    final Vec2 v2 = b2.m_linearVelocity;
    float w2 = b2.m_angularVelocity;
View Full Code Here

  /**
   * @see org.jbox2d.dynamics.joints.Joint#solvePositionConstraints(float)
   */
  @Override
  public boolean solvePositionConstraints(float baumgarte) {
    Body b1 = m_bodyA;
    Body b2 = m_bodyB;
   
    final Vec2 c1 = b1.m_sweep.c;
    float a1 = b1.m_sweep.a;
   
    final Vec2 c2 = b2.m_sweep.c;
    float a2 = b2.m_sweep.a;
   
    // Solve linear limit constraint.
    float linearError = 0.0f, angularError = 0.0f;
    boolean active = false;
    float C2 = 0.0f;
   
    Mat22 R1 = pool.popMat22();
    Mat22 R2 = pool.popMat22();
    R1.set(a1);
    R2.set(a2);
   
    final Vec2 r1 = pool.popVec2();
    final Vec2 r2 = pool.popVec2();
    final Vec2 temp = pool.popVec2();
    final Vec2 d = pool.popVec2();
   
    r1.set(m_localAnchor1).subLocal(m_localCenterA);
    r2.set(m_localAnchor2).subLocal(m_localCenterB);
    Mat22.mulToOut(R1, r1, r1);
    Mat22.mulToOut(R2, r2, r2);
    d.set(c2).addLocal(r2).subLocal(c1).subLocal(r1);
   
    if (m_enableLimit) {
      Mat22.mulToOut(R1, m_localXAxis1, m_axis);
     
      temp.set(d).addLocal(r1);
      m_a1 = Vec2.cross(temp, m_axis);
      m_a2 = Vec2.cross(r2, m_axis);
     
      float translation = Vec2.dot(m_axis, d);
      if (MathUtils.abs(m_upperTranslation - m_lowerTranslation) < 2.0f * Settings.linearSlop) {
        // Prevent large angular corrections
        C2 = MathUtils.clamp(translation, -Settings.maxLinearCorrection, Settings.maxLinearCorrection);
        linearError = MathUtils.abs(translation);
        active = true;
      }
      else if (translation <= m_lowerTranslation) {
        // Prevent large linear corrections and allow some slop.
        C2 = MathUtils.clamp(translation - m_lowerTranslation + Settings.linearSlop,
            -Settings.maxLinearCorrection, 0.0f);
        linearError = m_lowerTranslation - translation;
        active = true;
      }
      else if (translation >= m_upperTranslation) {
        // Prevent large linear corrections and allow some slop.
        C2 = MathUtils.clamp(translation - m_upperTranslation - Settings.linearSlop, 0.0f,
            Settings.maxLinearCorrection);
        linearError = translation - m_upperTranslation;
        active = true;
      }
    }
   
    Mat22.mulToOut(R1, m_localYAxis1, m_perp);
   
    temp.set(d).addLocal(r1);
    m_s1 = Vec2.cross(temp, m_perp);
    m_s2 = Vec2.cross(r2, m_perp);
   
    final Vec2 impulse = pool.popVec2();
    float C1;
    C1 = Vec2.dot(m_perp, d);
   
    linearError = MathUtils.max(linearError, MathUtils.abs(C1));
    angularError = 0.0f;
   
    if (active) {
      float m1 = m_invMassA, m2 = m_invMassB;
      float i1 = m_invIA, i2 = m_invIB;
     
      float k11 = m1 + m2 + i1 * m_s1 * m_s1 + i2 * m_s2 * m_s2;
      float k12 = i1 * m_s1 * m_a1 + i2 * m_s2 * m_a2;
      float k22 = m1 + m2 + i1 * m_a1 * m_a1 + i2 * m_a2 * m_a2;
     
      m_K.m11 = k11;
      m_K.m12 = k12;
      m_K.m21 = k12;
      m_K.m22 = k22;
     
      final Vec2 C = pool.popVec2();
      C.x = C1;
      C.y = C2;
     
      m_K.solveToOut(C.negateLocal(), impulse);
      pool.pushVec2(1);
    }
    else {
      float m1 = m_invMassA, m2 = m_invMassB;
      float i1 = m_invIA, i2 = m_invIB;
     
      float k11 = m1 + m2 + i1 * m_s1 * m_s1 + i2 * m_s2 * m_s2;
     
      float impulse1;
      if (k11 != 0.0f) {
        impulse1 = -C1 / k11;
      }
      else {
        impulse1 = 0.0f;
      }
     
      impulse.x = impulse1;
      impulse.y = 0.0f;
    }
   
    final Vec2 P = pool.popVec2();
    temp.set(m_axis).mulLocal(impulse.y);
    P.set(m_perp).mulLocal(impulse.x).add(temp);
   
    float L1 = impulse.x * m_s1 + impulse.y * m_a1;
    float L2 = impulse.x * m_s2 + impulse.y * m_a2;
   
    temp.set(P).mulLocal(m_invMassA);
    c1.subLocal(temp);
    a1 -= m_invIA * L1;
   
    temp.set(P).mulLocal(m_invMassB);
    c2.addLocal(temp);
    a2 += m_invIB * L2;
   
    // TODO_ERIN remove need for this.
    b1.m_sweep.a = a1;
    b2.m_sweep.a = a2;
    b1.synchronizeTransform();
    b2.synchronizeTransform();
   
    pool.pushVec2(6);
    pool.pushMat22(2);
   
    return linearError <= Settings.linearSlop && angularError <= Settings.angularSlop;
View Full Code Here

    Vec2 gravity = new Vec2(0, -10f);
    world = new World(gravity);

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

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


    {
      float a = .5f;
      PolygonShape shape = new PolygonShape();
      shape.setAsBox(a, a);

      Vec2 x = new Vec2(-7.0f, 0.75f);
      Vec2 y = new Vec2();
      Vec2 deltaX = new Vec2(0.5625f, 1f);
      Vec2 deltaY = new Vec2(1.125f, 0.0f);

      for (int i = 0; i < PYRAMID_SIZE; ++i){
        y.set(x);

        for (int j = i; j < PYRAMID_SIZE; ++j){
          BodyDef bd = new BodyDef();
          bd.type = BodyType.DYNAMIC;
          bd.position.set(y);
          Body body = world.createBody(bd);
          body.createFixture(shape, 5.0f);
                                        topBody = body;
          y.addLocal(deltaY);
        }

        x.addLocal(deltaX);
View Full Code Here

  /**
   * Get the world manifold.
   */
  public void getWorldManifold(WorldManifold worldManifold) {
    final Body bodyA = m_fixtureA.getBody();
    final Body bodyB = m_fixtureB.getBody();
    final Shape shapeA = m_fixtureA.getShape();
    final Shape shapeB = m_fixtureB.getShape();

    worldManifold.initialize(m_manifold, bodyA.getTransform(),
        shapeA.m_radius, bodyB.getTransform(), shapeB.m_radius);
  }
View Full Code Here

TOP

Related Classes of org.jbox2d.dynamics.Body

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.