Package org.jbox2d.dynamics

Examples of org.jbox2d.dynamics.Body


      final ParticleBodyContact contact = m_bodyContactBuffer[k];
      int a = contact.index;
      if ((m_flagsBuffer.data[a] & ParticleType.b2_powderParticle) != 0) {
        float w = contact.weight;
        if (w > minWeight) {
          Body b = contact.body;
          float m = contact.mass;
          Vec2 p = m_positionBuffer.data[a];
          Vec2 n = contact.normal;
          final Vec2 f = tempVec;
          final Vec2 va = m_velocityBuffer.data[a];
          final float inter = powderStrength * m * (w - minWeight);
          final float pInvMass = getParticleInvMass();
          f.x = inter * n.x;
          f.y = inter * n.y;
          va.x -= pInvMass * f.x;
          va.y -= pInvMass * f.y;
          b.applyLinearImpulse(f, p, true);
        }
      }
    }
    for (int k = 0; k < m_contactCount; k++) {
      final ParticleContact contact = m_contactBuffer[k];
View Full Code Here


    m_enableMotor = def.enableMotor;
  }
 
  @Override
  public void initVelocityConstraints(final TimeStep step) {
    final Body b1 = m_bodyA;
    final Body b2 = m_bodyB;
   
    if (m_enableMotor || m_enableLimit) {
      // You cannot create a rotation limit between bodies that
      // both have fixed rotation.
      assert (b1.m_invI > 0.0f || b2.m_invI > 0.0f);
    }
   
    final Vec2 r1 = pool.popVec2();
    final Vec2 r2 = pool.popVec2();
   
    // Compute the effective mass matrix.
    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);
   
    // J = [-I -r1_skew I r2_skew]
    // [ 0 -1 0 1]
    // r_skew = [-ry; rx]
   
View Full Code Here

    pool.pushVec2(2);
  }
 
  @Override
  public void solveVelocityConstraints(final TimeStep step) {
    final Body b1 = m_bodyA;
    final 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;
   
    float m1 = b1.m_invMass, m2 = b2.m_invMass;
    float i1 = b1.m_invI, i2 = b2.m_invI;
   
    // Solve motor constraint.
    if (m_enableMotor && m_limitState != LimitState.EQUAL) {
      float Cdot = w2 - w1 - m_motorSpeed;
      float impulse = m_motorMass * (-Cdot);
      float oldImpulse = m_motorImpulse;
      float maxImpulse = step.dt * m_maxMotorTorque;
      m_motorImpulse = MathUtils.clamp(m_motorImpulse + impulse, -maxImpulse, maxImpulse);
      impulse = m_motorImpulse - oldImpulse;
     
      w1 -= i1 * impulse;
      w2 += i2 * impulse;
    }
    final Vec2 temp = pool.popVec2();
    final Vec2 r1 = pool.popVec2();
    final Vec2 r2 = pool.popVec2();
   
    // Solve limit constraint.
    if (m_enableLimit && m_limitState != LimitState.INACTIVE) {
     
      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);
      // Vec2 r1 = b2Mul(b1.getTransform().R, m_localAnchor1 - b1.getLocalCenter());
      // Vec2 r2 = b2Mul(b2.getTransform().R, m_localAnchor2 - b2.getLocalCenter());
     
      final Vec2 Cdot1 = pool.popVec2();
      final Vec3 Cdot = pool.popVec3();
     
      // Solve point-to-point constraint
      Vec2.crossToOut(w1, r1, temp);
      Vec2.crossToOut(w2, r2, Cdot1);
      Cdot1.addLocal(v2).subLocal(v1).subLocal(temp);
      float Cdot2 = w2 - w1;
      Cdot.set(Cdot1.x, Cdot1.y, Cdot2);
      // Vec2 Cdot1 = v2 + b2Cross(w2, r2) - v1 - b2Cross(w1, r1);
      // float Cdot2 = w2 - w1;
      // b2Vec3 Cdot(Cdot1.x, Cdot1.y, Cdot2);
     
      Vec3 impulse = pool.popVec3();
      m_mass.solve33ToOut(Cdot.negateLocal(), impulse);
      // Cdot.negateLocal(); just leave negated, we don't use later
     
      if (m_limitState == LimitState.EQUAL) {
        m_impulse.addLocal(impulse);
      }
      else if (m_limitState == LimitState.AT_LOWER) {
        float newImpulse = m_impulse.z + impulse.z;
        if (newImpulse < 0.0f) {
          m_mass.solve22ToOut(Cdot1.negateLocal(), temp);
          //Cdot1.negateLocal(); just leave negated, we don't use it again
          impulse.x = temp.x;
          impulse.y = temp.y;
          impulse.z = -m_impulse.z;
          m_impulse.x += temp.x;
          m_impulse.y += temp.y;
          m_impulse.z = 0.0f;
        }
      }
      else if (m_limitState == LimitState.AT_UPPER) {
        float newImpulse = m_impulse.z + impulse.z;
        if (newImpulse > 0.0f) {
          m_mass.solve22ToOut(Cdot1.negateLocal(), temp);
          //Cdot1.negateLocal(); just leave negated, we don't use it again
          impulse.x = temp.x;
          impulse.y = temp.y;
          impulse.z = -m_impulse.z;
          m_impulse.x += temp.x;
          m_impulse.y += temp.y;
          m_impulse.z = 0.0f;
        }
      }
      final Vec2 P = pool.popVec2();
     
      P.set(impulse.x, impulse.y);
     
      temp.set(P).mulLocal(m1);
      v1.subLocal(temp);
      w1 -= i1 * (Vec2.cross(r1, P) + impulse.z);
     
      temp.set(P).mulLocal(m2);
      v2.addLocal(temp);
      w2 += i2 * (Vec2.cross(r2, P) + impulse.z);
     
      pool.pushVec2(2);
      pool.pushVec3(2);
    }
    else {
      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);
      // Vec2 r1 = b2Mul(b1.getTransform().R, m_localAnchor1 - b1.getLocalCenter());
      // Vec2 r2 = b2Mul(b2.getTransform().R, m_localAnchor2 - b2.getLocalCenter());
     
      // Solve point-to-point constraint
      Vec2 Cdot = pool.popVec2();
View Full Code Here

    pool.pushVec2(3);
  }
 
  @Override
  public boolean solvePositionConstraints(float baumgarte) {
    final Body b1 = m_bodyA;
    final Body b2 = m_bodyB;
   
    // TODO_ERIN block solve with limit.
   
    float angularError = 0.0f;
    float positionError = 0.0f;
   
    // Solve angular limit constraint.
    if (m_enableLimit && m_limitState != LimitState.INACTIVE) {
      float angle = b2.m_sweep.a - b1.m_sweep.a - m_referenceAngle;
      float limitImpulse = 0.0f;
     
      if (m_limitState == LimitState.EQUAL) {
        // Prevent large angular corrections
        float C = MathUtils.clamp(angle - m_lowerAngle, -Settings.maxAngularCorrection,
            Settings.maxAngularCorrection);
        limitImpulse = -m_motorMass * C;
        angularError = MathUtils.abs(C);
      }
      else if (m_limitState == LimitState.AT_LOWER) {
        float C = angle - m_lowerAngle;
        angularError = -C;
       
        // Prevent large angular corrections and allow some slop.
        C = MathUtils.clamp(C + Settings.angularSlop, -Settings.maxAngularCorrection, 0.0f);
        limitImpulse = -m_motorMass * C;
      }
      else if (m_limitState == LimitState.AT_UPPER) {
        float C = angle - m_upperAngle;
        angularError = C;
       
        // Prevent large angular corrections and allow some slop.
        C = MathUtils.clamp(C - Settings.angularSlop, 0.0f, Settings.maxAngularCorrection);
        limitImpulse = -m_motorMass * C;
      }
     
      b1.m_sweep.a -= b1.m_invI * limitImpulse;
      b2.m_sweep.a += b2.m_invI * limitImpulse;
     
      b1.synchronizeTransform();
      b2.synchronizeTransform();
    }
   
    // Solve point-to-point constraint.
    {
      Vec2 impulse = pool.popVec2();

      Vec2 r1 = pool.popVec2();
      Vec2 r2 = pool.popVec2();
      Vec2 C = 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);
     
      C.set(b2.m_sweep.c).addLocal(r2).subLocal(b1.m_sweep.c).subLocal(r1);
      positionError = C.length();
     
      float invMass1 = b1.m_invMass, invMass2 = b2.m_invMass;
      float invI1 = b1.m_invI, invI2 = b2.m_invI;
     
      // Handle large detachment.
      final float k_allowedStretch = 10.0f * Settings.linearSlop;
      if (C.lengthSquared() > k_allowedStretch * k_allowedStretch) {
        Vec2 u = pool.popVec2();
       
        // Use a particle solution (no rotation).
        // u.set(C);
        // u.normalize(); ?? we don't even use this
        float m = invMass1 + invMass2;
        if (m > 0.0f) {
          m = 1.0f / m;
        }
        impulse.set(C).negateLocal().mulLocal(m);
        final float k_beta = 0.5f;
        // using u as temp variable
        u.set(impulse).mulLocal(k_beta * invMass1);
        b1.m_sweep.c.subLocal(u);
        u.set(impulse).mulLocal(k_beta * invMass2);
        b2.m_sweep.c.addLocal(u);
       
        C.set(b2.m_sweep.c).addLocal(r2).subLocal(b1.m_sweep.c).subLocal(r1);
       
        pool.pushVec2(1);
      }
     
      Mat22 K1 = pool.popMat22();
      K1.m11 = invMass1 + invMass2;
      K1.m21 = 0.0f;
      K1.m12 = 0.0f;
      K1.m22 = invMass1 + invMass2;
     
      Mat22 K2 = pool.popMat22();
      K2.m11 = invI1 * r1.y * r1.y;
      K2.m21 = -invI1 * r1.x * r1.y;
      K2.m12 = -invI1 * r1.x * r1.y;
      K2.m22 = invI1 * r1.x * r1.x;
     
      Mat22 K3 = pool.popMat22();
      K3.m11 = invI2 * r2.y * r2.y;
      K3.m21 = -invI2 * r2.x * r2.y;
      K3.m12 = -invI2 * r2.x * r2.y;
      K3.m22 = invI2 * r2.x * r2.x;
     
      K1.addLocal(K2).addLocal(K3);
      K1.solveToOut(C.negateLocal(), impulse); // just leave c negated
     
      // using C as temp variable
      C.set(impulse).mulLocal(b1.m_invMass);
      b1.m_sweep.c.subLocal(C);
      b1.m_sweep.a -= b1.m_invI * Vec2.cross(r1, impulse);
     
      C.set(impulse).mulLocal(b2.m_invMass);
      b2.m_sweep.c.addLocal(C);
      b2.m_sweep.a += b2.m_invI * Vec2.cross(r2, impulse);
     
      b1.synchronizeTransform();
      b2.synchronizeTransform();
     
      pool.pushMat22(3);
      pool.pushVec2(4);
     
    }
View Full Code Here

  public float getReactionTorque(float inv_dt) {
    return inv_dt * m_impulse.z;
  }
 
  public float getJointAngle() {
    final Body b1 = m_bodyA;
    final Body b2 = m_bodyB;
    return b2.m_sweep.a - b1.m_sweep.a - m_referenceAngle;
  }
View Full Code Here

    final Body b2 = m_bodyB;
    return b2.m_sweep.a - b1.m_sweep.a - m_referenceAngle;
  }
 
  public float getJointSpeed() {
    final Body b1 = m_bodyA;
    final Body b2 = m_bodyB;
    return b2.m_angularVelocity - b1.m_angularVelocity;
  }
View Full Code Here

  /**
   * @see org.jbox2d.dynamics.joints.Joint#initVelocityConstraints(org.jbox2d.dynamics.TimeStep)
   */
  @Override
  public void initVelocityConstraints(TimeStep step) {
    Body bA = m_bodyA;
    Body bB = m_bodyB;

    // Compute the effective mass matrix.
    final Vec2 rA = pool.popVec2();
    final Vec2 rB = pool.popVec2();
    rA.set(m_localAnchorA).subLocal(bA.getLocalCenter());
    rB.set(m_localAnchorB).subLocal(bB.getLocalCenter());
   
    Mat22.mulToOut(bA.getTransform().R, rA, rA);
    Mat22.mulToOut(bB.getTransform().R, rB, rB);

    // J = [-I -r1_skew I r2_skew]
    //     [ 0       -1 0       1]
    // r_skew = [-ry; rx]

View Full Code Here

  /**
   * @see org.jbox2d.dynamics.joints.Joint#solveVelocityConstraints(org.jbox2d.dynamics.TimeStep)
   */
  @Override
  public void solveVelocityConstraints(TimeStep step) {
    Body bA = m_bodyA;
    Body bB = m_bodyB;

    Vec2 vA = bA.m_linearVelocity;
    float wA = bA.m_angularVelocity;
    Vec2 vB = bB.m_linearVelocity;
    float wB = bB.m_angularVelocity;

    float mA = bA.m_invMass, mB = bB.m_invMass;
    float iA = bA.m_invI, iB = bB.m_invI;
   
    final Vec2 rA = pool.popVec2();
    final Vec2 rB = pool.popVec2();
    rA.set(m_localAnchorA).subLocal(bA.getLocalCenter());
    rB.set(m_localAnchorB).subLocal(bB.getLocalCenter());
    Mat22.mulToOut(bA.getTransform().R, rA, rA);
    Mat22.mulToOut(bB.getTransform().R, rB, rB);
   
   
    final Vec2 Cdot1 = pool.popVec2();
    final Vec2 temp = pool.popVec2();
    // Solve point-to-point finalraint
View Full Code Here

  /**
   * @see org.jbox2d.dynamics.joints.Joint#solvePositionConstraints(float)
   */
  @Override
  public boolean solvePositionConstraints(float baumgarte) {
    Body bA = m_bodyA;
    Body bB = m_bodyB;

    float mA = bA.m_invMass, mB = bB.m_invMass;
    float iA = bA.m_invI, iB = bB.m_invI;

    final Vec2 rA = pool.popVec2();
    final Vec2 rB = pool.popVec2();
    rA.set(m_localAnchorA).subLocal(bA.getLocalCenter());
    rB.set(m_localAnchorB).subLocal(bB.getLocalCenter());
    Mat22.mulToOut(bA.getTransform().R, rA, rA);
    Mat22.mulToOut(bB.getTransform().R, rB, rB);
   
    final Vec2 C1 = pool.popVec2();
    C1.set(bB.m_sweep.c).addLocal(rB).subLocal(bA.m_sweep.c).subLocal(rA);
    float C2 = bB.m_sweep.a - bA.m_sweep.a - m_referenceAngle;

    // Handle large detachment.
    final float k_allowedStretch = 10.0f * Settings.linearSlop;
    float positionError = C1.length();
    float angularError = MathUtils.abs(C2);
    if (positionError > k_allowedStretch){
      iA *= 1.0f;
      iB *= 1.0f;
    }

    m_mass.col1.x = mA + mB + rA.y * rA.y * iA + rB.y * rB.y * iB;
    m_mass.col2.x = -rA.y * rA.x * iA - rB.y * rB.x * iB;
    m_mass.col3.x = -rA.y * iA - rB.y * iB;
    m_mass.col1.y = m_mass.col2.x;
    m_mass.col2.y = mA + mB + rA.x * rA.x * iA + rB.x * rB.x * iB;
    m_mass.col3.y = rA.x * iA + rB.x * iB;
    m_mass.col1.z = m_mass.col3.x;
    m_mass.col2.z = m_mass.col3.y;
    m_mass.col3.z = iA + iB;

    final Vec3 C = pool.popVec3();
    final Vec3 impulse = pool.popVec3();
    C.set(C1.x, C1.y, C2);

    m_mass.solve33ToOut(C.negateLocal(), impulse); // just leave c negated..

    final Vec2 P = pool.popVec2();
    final Vec2 temp = pool.popVec2();
    P.set(impulse.x, impulse.y);

    temp.set(P).mulLocal(mA);
    bA.m_sweep.c.subLocal(temp);
    bA.m_sweep.a -= iA * (Vec2.cross(rA, P) + impulse.z);

    temp.set(P).mulLocal(mB);
    bB.m_sweep.c.addLocal(temp);
    bB.m_sweep.a += iB * (Vec2.cross(rB, P) + impulse.z);

    bA.synchronizeTransform();
    bB.synchronizeTransform();
   
    pool.pushVec2(5);
    pool.pushVec3(2);

    return positionError <= Settings.linearSlop && angularError <= Settings.angularSlop;
View Full Code Here

    public boolean reportFixture(Fixture fixture) {
      if (fixture.isSensor()) {
        return true;
      }
      final Shape shape = fixture.getShape();
      Body b = fixture.getBody();
      Vec2 bp = b.getWorldCenter();
      float bm = b.getMass();
      float bI = b.getInertia() - bm * b.getLocalCenter().lengthSquared();
      float invBm = bm > 0 ? 1 / bm : 0;
      float invBI = bI > 0 ? 1 / bI : 0;
      int childCount = shape.getChildCount();
      for (int childIndex = 0; childIndex < childCount; childIndex++) {
        AABB aabb = fixture.getAABB(childIndex);
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.