Package net.phys2d.math

Examples of net.phys2d.math.Vector2f


  public void preStep(float invDT) {
    float biasFactor = 0.005f;
    float biasImpulse = 0.0f;
    float RA = body1.getRotation() + rotateA;

    Vector2f VA = new Vector2f((float) Math.cos(RA), (float) Math.sin(RA));

    Matrix2f rot1 = new Matrix2f(body1.getRotation());
    Matrix2f rot2 = new Matrix2f(body2.getRotation());
    Vector2f r1 = MathUtil.mul(rot1, anchor1);
    Vector2f r2 = MathUtil.mul(rot2, anchor2);

    Vector2f p1 = new Vector2f(body1.getPosition());
    p1.add(r1);
    Vector2f p2 = new Vector2f(body2.getPosition());
    p2.add(r2);
    dp = new Vector2f(p2);
    dp.sub(p1);
    dlength2 = dp.lengthSquared();
    ndp = new Vector2f(dp);
    ndp.normalise();

    R = new Vector2f(r1);
    R.add(dp);
    // System.out.println(accumulateImpulse);
    Vector2f relativeVelocity = new Vector2f(body2.getVelocity());
    relativeVelocity.add(MathUtil.cross(r2, body2.getAngularVelocity()));
    relativeVelocity.sub(body1.getVelocity());
    relativeVelocity.sub(MathUtil.cross(r1, body1.getAngularVelocity()));

    /*
     * Matrix2f tr1 = new Matrix2f(-body1.getRotation()); relativeVelocity =
     * MathUtil.mul(tr1, relativeVelocity);
     * relativeVelocity.add(MathUtil.mul(tr1, dp));
     */
    // relativeVelocity.add(MathUtil.cross(dp,body1.getAngularVelocity()));
    n = new Vector2f(-ndp.y, ndp.x);
    Vector2f v1 = new Vector2f(n);
    v1.scale(-body2.getInvMass() - body1.getInvMass());

    Vector2f v2 = MathUtil.cross(MathUtil.cross(r2, n), r2);
    v2.scale(-body2.getInvI());

    Vector2f v3 = MathUtil.cross(MathUtil.cross(R, n), r1);
    v3.scale(-body1.getInvI());

    Vector2f K1 = new Vector2f(v1);
    K1.add(v2);
    K1.add(v3);

    K = MathUtil.cross(dp, K1) / dlength2 - MathUtil.cross(R, n)
        * body1.getInvI();



      biasImpulse = biasFactor * MathUtil.cross(ndp, VA) * invDT;

    Vector2f impulse = new Vector2f(n);
    impulse.scale(accumulateImpulse+biasImpulse);
    if (!body1.isStatic()) {
      Vector2f accum1 = new Vector2f(impulse);
      accum1.scale(body1.getInvMass());
      body1.adjustVelocity(accum1);
      body1.adjustAngularVelocity((body1.getInvI() * MathUtil.cross(R,
          impulse)));
    }
    if (!body2.isStatic()) {
      Vector2f accum2 = new Vector2f(impulse);
      accum2.scale(-body2.getInvMass());
      body2.adjustVelocity(accum2);
      body2.adjustAngularVelocity(-(body2.getInvI() * MathUtil.cross(r2,
          impulse)));
    }
  }
 
View Full Code Here


   
    stretchedSpringConst = 100;
    compressedSpringConst = 100;
    brokenSpringConst = 100;
   
    Vector2f spring = new Vector2f(anchor1);
    spring.sub(anchor2);
    springSize = spring.length();
    minSpringSize = 0;
    maxSpringSize = 2 * springSize;
   
    set(b1,b2,anchor1,anchor2);
  }
 
View Full Code Here

    body1 = b1;
    body2 = b2; 

    Matrix2f rot1 = new Matrix2f(body1.getRotation());
    Matrix2f rot1T = rot1.transpose();
    Vector2f a1 = new Vector2f(anchor1);
    a1.sub(body1.getPosition());
    localAnchor1 = MathUtil.mul(rot1T,a1);
   
    Matrix2f rot2 = new Matrix2f(body2.getRotation());
    Matrix2f rot2T = rot2.transpose();
    Vector2f a2 = new Vector2f(anchor2);
    a2.sub(body2.getPosition());
    localAnchor2 = MathUtil.mul(rot2T,a2);
  }
View Full Code Here

   * @param invDT The amount of time the simulation is being stepped by
   */
  public void preStep(float invDT) {

    // calculate the spring's vector (pointing from body1 to body2) and length
    spring = new Vector2f(body2.getPosition());
    spring.add(r2);
    spring.sub(body1.getPosition());
    spring.sub(r1);
    springLength = spring.length();
   
    // the spring vector needs to be normalized for applyImpulse as well!
    spring.normalise();
   
    // calculate the spring's forces
    // note that although theoretically invDT could never be 0
    // but here it can
    float springConst;
   
    if ( springLength < minSpringSize || springLength > maxSpringSize ) {
      // Pre-compute anchors, mass matrix, and bias.
      Matrix2f rot1 = new Matrix2f(body1.getRotation());
      Matrix2f rot2 = new Matrix2f(body2.getRotation());
 
      r1 = MathUtil.mul(rot1,localAnchor1);
      r2 = MathUtil.mul(rot2,localAnchor2);
     
      // the mass normal or 'k'
      float rn1 = r1.dot(spring);
      float rn2 = r2.dot(spring);
      float kNormal = body1.getInvMass() + body2.getInvMass();
      kNormal += body1.getInvI() * (r1.dot(r1) - rn1 * rn1) + body2.getInvI() * (r2.dot(r2) - rn2 * rn2);
      massNormal = 1 / kNormal;
     
     
      // The spring is broken so apply force to correct it
      // note that we use biased velocities for this
      float springImpulse =
        invDT != 0 ? brokenSpringConst * (springLength - springSize) / invDT : 0;
     
      Vector2f impulse = MathUtil.scale(spring, springImpulse);
      body1.adjustBiasedVelocity(MathUtil.scale(impulse, body1.getInvMass()));
      body1.adjustBiasedAngularVelocity((body1.getInvI() * MathUtil.cross(r1, impulse)));

      body2.adjustBiasedVelocity(MathUtil.scale(impulse, -body2.getInvMass()));
      body2.adjustBiasedAngularVelocity(-(body2.getInvI() * MathUtil.cross(r2, impulse)));
     
      isBroken = true;
      return;
     
    } else if ( springLength < springSize ) {
      springConst = compressedSpringConst;
      isBroken = false;
    } else { // if ( springLength >= springSize )
      springConst = stretchedSpringConst;
      isBroken = false;
    }
   
    float springImpulse =
      invDT != 0 ? springConst * (springLength - springSize) / invDT : 0;

    // apply the spring's forces
    Vector2f impulse = MathUtil.scale(spring, springImpulse);
    body1.adjustVelocity(MathUtil.scale(impulse, body1.getInvMass()));
    body1.adjustAngularVelocity((body1.getInvI() * MathUtil.cross(r1, impulse)));

    body2.adjustVelocity(MathUtil.scale(impulse, -body2.getInvMass()));
    body2.adjustAngularVelocity(-(body2.getInvI() * MathUtil.cross(r2, impulse)));
View Full Code Here

   */
  public void applyImpulse() {
    if ( isBroken ) {
      // calculate difference in velocity
      // TODO: share this code with BasicJoint and Arbiter
      Vector2f relativeVelocity =  new Vector2f(body2.getVelocity());
      relativeVelocity.add(MathUtil.cross(body2.getAngularVelocity(), r2));
      relativeVelocity.sub(body1.getVelocity());
      relativeVelocity.sub(MathUtil.cross(body1.getAngularVelocity(), r1));
     
      // project the relative velocity onto the spring vector and apply the mass normal
      float normalImpulse = massNormal * relativeVelocity.dot(spring);
     
//      // TODO: Clamp the accumulated impulse?
//      float oldNormalImpulse = accumulatedNormalImpulse;
//      accumulatedNormalImpulse = Math.max(oldNormalImpulse + normalImpulse, 0.0f);
//      normalImpulse = accumulatedNormalImpulse - oldNormalImpulse;
 
      // only apply the impulse if we are pushing or pulling in the right way
      // i.e. pulling if the string is overstretched and pushing if it is too compressed
      if ( springLength < minSpringSize && normalImpulse < 0
          || springLength > maxSpringSize && normalImpulse > 0 ) {
        // now apply the impulses to the bodies
        Vector2f impulse = MathUtil.scale(spring, normalImpulse);
        body1.adjustVelocity(MathUtil.scale(impulse, body1.getInvMass()));
        body1.adjustAngularVelocity((body1.getInvI() * MathUtil.cross(r1, impulse)));
   
        body2.adjustVelocity(MathUtil.scale(impulse, -body2.getInvMass()));
        body2.adjustAngularVelocity(-(body2.getInvI() * MathUtil.cross(r2, impulse)));
View Full Code Here

   *
   * @return True if the joint is holding the bodies together
   */
  public boolean isActive() {
    if (body1.getPosition().distanceSquared(body2.getPosition()) < distance) {
      Vector2f to2 = new Vector2f(body2.getPosition());
      to2.sub(body1.getPosition());
      to2.normalise();
      Vector2f vel = new Vector2f(body1.getVelocity());
      vel.normalise();
      if (body1.getVelocity().dot(to2) < 0) {
        return true;
      }
    }
   
View Full Code Here

    Matrix2f rot1 = new Matrix2f(body1.getRotation());
    Matrix2f rot2 = new Matrix2f(body2.getRotation());
    Matrix2f rot1T = rot1.transpose();
    Matrix2f rot2T = rot2.transpose();

    Vector2f a1 = new Vector2f(anchor);
    a1.sub(body1.getPosition());
    localAnchor1 = MathUtil.mul(rot1T,a1);
    Vector2f a2 = new Vector2f(anchor);
    a2.sub(body2.getPosition());
    localAnchor2 = MathUtil.mul(rot2T,a2);

    accumulatedImpulse.set(0.0f, 0.0f);
    relaxation = 1.0f;
  }
View Full Code Here

    K3.col1.y = -body2.getInvI() * r2.x * r2.y;    K3.col2.y =  body2.getInvI() * r2.x * r2.x;

    Matrix2f K = MathUtil.add(MathUtil.add(K1,K2),K3);
    M = K.invert();

    Vector2f p1 = new Vector2f(body1.getPosition());
    p1.add(r1);
    Vector2f p2 = new Vector2f(body2.getPosition());
    p2.add(r2);
    Vector2f dp = new Vector2f(p2);
    dp.sub(p1);
   
    bias = new Vector2f(dp);
    bias.scale(-0.1f);
    bias.scale(invDT);

    // Apply accumulated impulse.
    accumulatedImpulse.scale(relaxation);
   
    if (!body1.isStatic()) {
      Vector2f accum1 = new Vector2f(accumulatedImpulse);
      accum1.scale(-body1.getInvMass());
      body1.adjustVelocity(accum1);
      body1.adjustAngularVelocity(-(body1.getInvI() * MathUtil.cross(r1, accumulatedImpulse)));
    }

    if (!body2.isStatic()) {
      Vector2f accum2 = new Vector2f(accumulatedImpulse);
      accum2.scale(body2.getInvMass());
      body2.adjustVelocity(accum2);
      body2.adjustAngularVelocity(body2.getInvI() * MathUtil.cross(r2, accumulatedImpulse));
    }
  }
View Full Code Here

 
  /**
   * Apply the impulse caused by the joint to the bodies attached.
   */
  public void applyImpulse() {
    Vector2f dv = new Vector2f(body2.getVelocity());
    dv.add(MathUtil.cross(body2.getAngularVelocity(),r2));
    dv.sub(body1.getVelocity());
    dv.sub(MathUtil.cross(body1.getAngularVelocity(),r1));
      dv.scale(-1);
      dv.add(bias); // TODO: is this baumgarte stabilization?
     
      if (dv.lengthSquared() == 0) {
        return;
      }
     
    Vector2f impulse = MathUtil.mul(M, dv);

    if (!body1.isStatic()) {
      Vector2f delta1 = new Vector2f(impulse);
      delta1.scale(-body1.getInvMass());
      body1.adjustVelocity(delta1);
      body1.adjustAngularVelocity(-body1.getInvI() * MathUtil.cross(r1,impulse));
    }

    if (!body2.isStatic()) {
      Vector2f delta2 = new Vector2f(impulse);
      delta2.scale(body2.getInvMass());
      body2.adjustVelocity(delta2);
      body2.adjustAngularVelocity(body2.getInvI() * MathUtil.cross(r2,impulse));
    }
   
    accumulatedImpulse.add(impulse);
 
View Full Code Here

 
  /**
   * @see net.phys2d.raw.Joint#applyImpulse()
   */
  public void applyImpulse() {
    Vector2f dv = new Vector2f(body2.getVelocity());
    dv.add(MathUtil.cross(body2.getAngularVelocity(), r2));
    dv.sub(body1.getVelocity());
    dv.sub(MathUtil.cross(body1.getAngularVelocity(), r1));

    float ju = -dv.dot(dp) + bias;
    float p = ju / sc;

    Vector2f impulse = new Vector2f(dp);
    impulse.scale(p);

    if (!body1.isStatic()) {
      Vector2f accum1 = new Vector2f(impulse);
      accum1.scale(-body1.getInvMass());
      body1.adjustVelocity(accum1);
      body1.adjustAngularVelocity(-(body1.getInvI() * MathUtil.cross(r1,
          impulse)));
    }

    if (!body2.isStatic()) {
      Vector2f accum2 = new Vector2f(impulse);
      accum2.scale(body2.getInvMass());
      body2.adjustVelocity(accum2);
      body2.adjustAngularVelocity(body2.getInvI()
          * MathUtil.cross(r2, impulse));
    }

View Full Code Here

TOP

Related Classes of net.phys2d.math.Vector2f

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.