Package net.phys2d.math

Examples of net.phys2d.math.Vector2f


   * @param y1 The y coordinate of the start point
   * @param x2 The x coordinate of the end point
   * @param y2 The y coordinate of the end point
   */
  public Line(float x1, float y1, float x2, float y2) {
    this(new Vector2f(x1,y1), new Vector2f(x2,y2));
  }
View Full Code Here


   */
  public void set(ROVector2f start, ROVector2f end) {
    this.start = start;
    this.end = end;
   
    vec = new Vector2f(end);
    vec.sub(start);
   
    lenSquared = vec.length();
    lenSquared *= lenSquared;
  }
View Full Code Here

  public Vector2f[] getVertices(ROVector2f displacement, float rotation) {
    float cos = (float) Math.cos(rotation);
    float sin = (float) Math.sin(rotation);
   
    Vector2f[] endPoints = new Vector2f[2];
    endPoints[0] = new Vector2f(//getX1(), getY1());
        getX1() * cos - getY1() * sin,
        getY1() * cos + getX1() * sin);
    endPoints[0].add(displacement);
    endPoints[1] = new Vector2f(//getX2(), getY2());
        getX2() * cos - getY2() * sin,
        getY2() * cos + getX2() * sin);
    endPoints[1].add(displacement);
   
    return endPoints;
View Full Code Here

   * Move this line a certain amount
   *
   * @param v The amount to move the line
   */
  public void move(ROVector2f v) {
    Vector2f temp = new Vector2f(start);
    temp.add(v);
    start = temp;
    temp = new Vector2f(end);
    temp.add(v);
    end = temp;
  }
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);

    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);

    float biasFactor = 0.3f;
    bias = biasFactor * (-dp.lengthSquared() + distant);

    dp.normalise();

    sc = MathUtil.mul(K, dp).dot(dp);

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

    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

    float u = ua;
   
    float ix = start.getX() + (u * (end.getX() - start.getX()));
    float iy = start.getY() + (u * (end.getY() - start.getY()));
   
    return new Vector2f(ix,iy);
  }
View Full Code Here

   */
  public void set(Body b1, Body b2) {
    body1 = b1;
    body2 = b2;

    joint1 = new BasicJoint(b1,b2,new Vector2f(b1.getPosition()));
    joint2 = new BasicJoint(b2,b1,new Vector2f(b2.getPosition()));
  }
View Full Code Here

    for (int i = 0; i < numContacts; ++i)
    {
      Contact c = contacts[i];
      c.normal.normalise();
     
      Vector2f r1 = new Vector2f(c.position);
      r1.sub(body1.getPosition());
      Vector2f r2 = new Vector2f(c.position);
      r2.sub(body2.getPosition());

      // Precompute normal mass, tangent mass, and bias.
      float rn1 = r1.dot(c.normal);
      float rn2 = r2.dot(c.normal);
      float kNormal = body1.getInvMass() + body2.getInvMass();
      kNormal += body1.getInvI() * (r1.dot(r1) - rn1 * rn1) + body2.getInvI() * (r2.dot(r2) - rn2 * rn2);
      c.massNormal = damping / kNormal;
     
      Vector2f tangent = MathUtil.cross(c.normal, 1.0f);
      float rt1 = r1.dot(tangent);
      float rt2 = r2.dot(tangent);
      float kTangent = body1.getInvMass() + body2.getInvMass();
      kTangent += body1.getInvI() * (r1.dot(r1) - rt1 * rt1) + body2.getInvI() * (r2.dot(r2) - rt2 * rt2);
      c.massTangent = damping / kTangent;

      // Compute restitution
      // Relative velocity at contact
      Vector2f relativeVelocity =  new Vector2f(body2.getVelocity());
      relativeVelocity.add(MathUtil.cross(r2, body2.getAngularVelocity()));
      relativeVelocity.sub(body1.getVelocity());
      relativeVelocity.sub(MathUtil.cross(r1, body1.getAngularVelocity()));
     
      float combinedRestitution = (body1.getRestitution() * body2.getRestitution());
      float relVel = c.normal.dot(relativeVelocity);
      c.restitution = combinedRestitution * -relVel;
      c.restitution = Math.max(c.restitution, 0);
     
      float penVel = -c.separation / dt;
      if (c.restitution >= penVel) {
        c.bias = 0;
      } else {
        c.bias = -biasFactor * invDT * Math.min(0.0f, c.separation + allowedPenetration);
      }
     
      // apply damping
      c.accumulatedNormalImpulse *= damping;
     
      // Apply normal + friction impulse
      Vector2f impulse = MathUtil.scale(c.normal, c.accumulatedNormalImpulse);
      impulse.add(MathUtil.scale(tangent, c.accumulatedTangentImpulse));
     
      body1.adjustVelocity(MathUtil.scale(impulse, -body1.getInvMass()));
      body1.adjustAngularVelocity(-body1.getInvI() * MathUtil.cross(r1, impulse));

      body2.adjustVelocity(MathUtil.scale(impulse, body2.getInvMass()));
View Full Code Here

   
    for (int i = 0; i < numContacts; ++i)
    {
      Contact c = contacts[i];
     
      Vector2f r1 = new Vector2f(c.position);
      r1.sub(b1.getPosition());
      Vector2f r2 = new Vector2f(c.position);
      r2.sub(b2.getPosition());

      // Relative velocity at contact
      Vector2f relativeVelocity =  new Vector2f(b2.getVelocity());
      relativeVelocity.add(MathUtil.cross(b2.getAngularVelocity(), r2));
      relativeVelocity.sub(b1.getVelocity());
      relativeVelocity.sub(MathUtil.cross(b1.getAngularVelocity(), r1));
     
      // Compute normal impulse with bias.
      float vn = relativeVelocity.dot(c.normal);
     
      // bias caculations are now handled seperately hence we only
      // handle the real impulse caculations here
      //float normalImpulse = c.massNormal * ((c.restitution - vn) + c.bias);
      float normalImpulse = c.massNormal * (c.restitution - vn);
     
      // Clamp the accumulated impulse
      float oldNormalImpulse = c.accumulatedNormalImpulse;
      c.accumulatedNormalImpulse = Math.max(oldNormalImpulse + normalImpulse, 0.0f);
      normalImpulse = c.accumulatedNormalImpulse - oldNormalImpulse;
     
      // Apply contact impulse
      Vector2f impulse = MathUtil.scale(c.normal, normalImpulse);
     
      b1.adjustVelocity(MathUtil.scale(impulse, -b1.getInvMass()));
      b1.adjustAngularVelocity(-(b1.getInvI() * MathUtil.cross(r1, impulse)));

      b2.adjustVelocity(MathUtil.scale(impulse, b2.getInvMass()));
      b2.adjustAngularVelocity(b2.getInvI() * MathUtil.cross(r2, impulse));

      // Compute bias impulse
      // NEW STUFF FOR SEPERATING BIAS
      relativeVelocity.set(b2.getBiasedVelocity());
      relativeVelocity.add(MathUtil.cross(b2.getBiasedAngularVelocity(), r2));
      relativeVelocity.sub(b1.getBiasedVelocity());
      relativeVelocity.sub(MathUtil.cross(b1.getBiasedAngularVelocity(), r1));
      float vnb = relativeVelocity.dot(c.normal);

      float biasImpulse = c.massNormal * (-vnb + c.bias);
      float oldBiasImpulse = c.biasImpulse;
      c.biasImpulse = Math.max(oldBiasImpulse + biasImpulse, 0.0f);
      biasImpulse = c.biasImpulse - oldBiasImpulse;

      Vector2f Pb = MathUtil.scale(c.normal, biasImpulse);
     
      b1.adjustBiasedVelocity(MathUtil.scale(Pb, -b1.getInvMass()));
      b1.adjustBiasedAngularVelocity(-(b1.getInvI() * MathUtil.cross(r1, Pb)));

      b2.adjustBiasedVelocity(MathUtil.scale(Pb, b2.getInvMass()));
      b2.adjustBiasedAngularVelocity((b2.getInvI() * MathUtil.cross(r2, Pb)));

      // END NEW STUFF
     
      //
      // Compute friction (tangent) impulse
      //
      float maxTangentImpulse = friction * c.accumulatedNormalImpulse;

      // Relative velocity at contact
      relativeVelocity.set(b2.getVelocity());
      relativeVelocity.add(MathUtil.cross(b2.getAngularVelocity(), r2));
      relativeVelocity.sub(b1.getVelocity());
      relativeVelocity.sub(MathUtil.cross(b1.getAngularVelocity(), r1));
     
      Vector2f tangent = MathUtil.cross(c.normal, 1.0f);
      float vt = relativeVelocity.dot(tangent);
      float tangentImpulse = c.massTangent * (-vt);

      // Clamp friction
      float oldTangentImpulse = c.accumulatedTangentImpulse;
View Full Code Here

   * @param body1 The first body
   * @param body2 The second body
   * @return The energy contained
   */
  protected float getEnergy(Body body1, Body body2) {
    Vector2f combinedVel = MathUtil.scale(body1.getVelocity(), body1.getMass());
    combinedVel.add(MathUtil.scale(body2.getVelocity(), body2.getMass()));
   
    float combinedInertia = body1.getI() * body1.getAngularVelocity();
    combinedInertia += body2.getI() * body2.getAngularVelocity();
   
    float vel1Energy = body1.getMass() * body1.getVelocity().dot(body1.getVelocity());
 
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.