Package eas.simulation.spatial.sim2D.physicalSimulation.physicsEngine.net.phys2d.math

Examples of eas.simulation.spatial.sim2D.physicalSimulation.physicsEngine.net.phys2d.math.Vector2f


    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.
   */
  @Override
    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

    int count = super.collide(contacts, boxBody, circleBody);
   
    // reverse the collision results by inverting normals
    // and projecting the results onto the circle
    for (int i=0;i<count;i++) {
      Vector2f vec = MathUtil.scale(contacts[i].getNormal(),-1);
      contacts[i].setNormal(vec);
     
      Vector2f pt = MathUtil.sub(contacts[i].getPosition(), circleBody.getPosition());
      pt.normalise();
      pt.scale(((Circle) circleBody.getBodyShape()).getRadius());
      pt.add(circleBody.getPosition());
      contacts[i].setPosition(pt);
    }
   
    return count;
  }
View Full Code Here

  /**
   * @see eas.simulation.spatial.sim2D.physicalSimulation.physicsEngine.net.phys2d.raw.Joint#applyImpulse()
   */
  @Override
    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

    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

    int numContacts = 0;
   
    Line line = (Line) bodyA.getBodyShape();
    Box box = (Box) bodyB.getBodyShape();
   
    Vector2f lineVec = new Vector2f(line.getDX(), line.getDY());
    lineVec.normalise()
    Vector2f axis = new Vector2f(-line.getDY(), line.getDX());
    axis.normalise();
   
    Vector2f res = new Vector2f();
    line.getStart().projectOntoUnit(axis, res);
    float linePos = getProp(res,axis);
   
    Vector2f c = MathUtil.sub(bodyB.getPosition(),bodyA.getPosition());
    c.projectOntoUnit(axis,res);
    float centre = getProp(res, axis);
   
    Vector2f[] pts = box.getPoints(bodyB.getPosition(), bodyB.getRotation());
    float[] tangent = new float[4];
    float[] proj = new float[4];
   
    int outOfRange = 0;
   
    for (int i=0;i<4;i++) {
      pts[i].sub(bodyA.getPosition());
      pts[i].projectOntoUnit(axis, res);
      tangent[i] = getProp(res, axis);
      pts[i].projectOntoUnit(lineVec, res);
      proj[i] = getProp(res, new Vector2f(line.getDX(), line.getDY()));
     
      if ((proj[i] >= 1) || (proj[i] <= 0)) {
        outOfRange++;
      }
    }
    if (outOfRange == 4) {
      return 0;
    }
   
    Vector2f normal = new Vector2f(axis);
   
    if (centre < linePos) {
      if (!line.blocksInnerEdge()) {
        return 0;
      }
     
      normal.scale(-1);
      for (int i=0;i<4;i++) {
        if (tangent[i] > linePos) {
          if (proj[i] < 0) {
            Vector2f onAxis = new Vector2f();
            Line leftLine = new Line(getPt(pts,i-1),pts[i]);
            Line rightLine = new Line(getPt(pts,i+1),pts[i]);
            leftLine.getClosestPoint(line.getStart(),res);
            res.projectOntoUnit(axis, onAxis);
            float left = getProp(onAxis, axis);
            rightLine.getClosestPoint(line.getStart(),res);
            res.projectOntoUnit(axis, onAxis);
            float right = getProp(onAxis, axis);
           
            if ((left > 0) && (right > 0)) {
              Vector2f pos = new Vector2f(bodyA.getPosition());
              pos.add(line.getStart());
             
              resolveEndPointCollision(pos,bodyA,bodyB,normal,leftLine,rightLine,contacts[numContacts],i);
              numContacts++;
            }
          } else if (proj[i] > 1) {
            Vector2f onAxis = new Vector2f();
            Line leftLine = new Line(getPt(pts,i-1),pts[i]);
            Line rightLine = new Line(getPt(pts,i+1),pts[i]);
            leftLine.getClosestPoint(line.getEnd(),res);
            res.projectOntoUnit(axis, onAxis);
            float left = getProp(onAxis, axis);
            rightLine.getClosestPoint(line.getEnd(),res);
            res.projectOntoUnit(axis, onAxis);
            float right = getProp(onAxis, axis);
           
            if ((left > 0) && (right > 0)) {
              Vector2f pos = new Vector2f(bodyA.getPosition());
              pos.add(line.getEnd());

              resolveEndPointCollision(pos,bodyA,bodyB,normal,leftLine,rightLine,contacts[numContacts],i);
              numContacts++;
            }
          } else {
            pts[i].projectOntoUnit(lineVec, res);
            res.add(bodyA.getPosition());
            contacts[numContacts].setSeparation(-(tangent[i]-linePos));
            contacts[numContacts].setPosition(new Vector2f(res));
            contacts[numContacts].setNormal(normal);
            contacts[numContacts].setFeature(new FeaturePair(i))
            numContacts++;
          }
        }
      }
    } else {
      if (!line.blocksOuterEdge()) {
        return 0;
      }
     
      for (int i=0;i<4;i++) {
        if (tangent[i] < linePos) {
          if (proj[i] < 0) {
            Vector2f onAxis = new Vector2f();
            Line leftLine = new Line(getPt(pts,i-1),pts[i]);
            Line rightLine = new Line(getPt(pts,i+1),pts[i]);
            leftLine.getClosestPoint(line.getStart(),res);
            res.projectOntoUnit(axis, onAxis);
            float left = getProp(onAxis, axis);
            rightLine.getClosestPoint(line.getStart(),res);
            res.projectOntoUnit(axis, onAxis);
            float right = getProp(onAxis, axis);
           
            if ((left < 0) && (right < 0)) {
              Vector2f pos = new Vector2f(bodyA.getPosition());
              pos.add(line.getStart());

              resolveEndPointCollision(pos,bodyA,bodyB,normal,leftLine,rightLine,contacts[numContacts],i);
              numContacts++;
            }
          } else if (proj[i] > 1) {
            Vector2f onAxis = new Vector2f();
            Line leftLine = new Line(getPt(pts,i-1),pts[i]);
            Line rightLine = new Line(getPt(pts,i+1),pts[i]);
            leftLine.getClosestPoint(line.getEnd(),res);
            res.projectOntoUnit(axis, onAxis);
            float left = getProp(onAxis, axis);
            rightLine.getClosestPoint(line.getEnd(),res);
            res.projectOntoUnit(axis, onAxis);
            float right = getProp(onAxis, axis);
           
            if ((left < 0) && (right < 0)) {
              Vector2f pos = new Vector2f(bodyA.getPosition());
              pos.add(line.getEnd());

              resolveEndPointCollision(pos,bodyA,bodyB,normal,leftLine,rightLine,contacts[numContacts],i);
              numContacts++;
            }
          } else {
            pts[i].projectOntoUnit(lineVec, res);
            res.add(bodyA.getPosition());
            contacts[numContacts].setSeparation(-(linePos - tangent[i]));
            contacts[numContacts].setPosition(new Vector2f(res));
            contacts[numContacts].setNormal(normal);
            contacts[numContacts].setFeature(new FeaturePair());       
            numContacts++;
          }
        }
View Full Code Here

   * @param contact The contact to populate
   * @param norm The normal determined for the line
   * @param i The index of teh face we're resolving for feature ID
   */
  private void resolveEndPointCollision(Vector2f pos, PhysicsAgent2D<?> bodyA, @SuppressWarnings("unused") PhysicsAgent2D<?> bodyB, Vector2f norm, Line leftLine, Line rightLine, Contact contact, int i) {
    Vector2f start = new Vector2f(pos);
    Vector2f end = new Vector2f(start);
    end.add(norm);
   
    rightLine.move(bodyA.getPosition());
    leftLine.move(bodyA.getPosition());
    Line normLine = new Line(start,end);
    Vector2f rightPoint = normLine.intersect(rightLine);
    Vector2f leftPoint = normLine.intersect(leftLine);
   
    float dis1 = Float.MAX_VALUE;
    if (rightPoint != null) {
      dis1 = rightPoint.distance(start) - norm.length();
    }
    float dis2 = Float.MAX_VALUE;
    if (leftPoint != null) {
      dis2 = leftPoint.distance(start) - norm.length();
    }
   
    norm.normalise();
    float dis = Math.min(dis1,dis2);
   
View Full Code Here

      float dis = (float) Math.sqrt(closestDistance);
      contacts[0].setSeparation(dis - circle.getRadius());
     
      // this should really be where the edge and the line
      // between the two elements cross?
      Vector2f contactPoint = new Vector2f();
      lines[closest].getClosestPoint(circleBody.getPosition(), contactPoint);
     
      Vector2f normal = MathUtil.sub(circleBody.getPosition(), contactPoint);
      normal.normalise();
      contacts[0].setNormal(normal);
      contacts[0].setPosition(contactPoint);
      contacts[0].setFeature(new FeaturePair());
     
      return 1;
View Full Code Here

    Vector2f[] vertsA = poly.getVertices(bodyA.getPosition(), bodyA.getRotation());
    Vector2f[] vertsB = box.getPoints(bodyB.getPosition(), bodyB.getRotation());
   
    // TODO: use a sweepline that has the smallest projection of the box
    // now we use just an arbitrary one
    Vector2f sweepline = new Vector2f(vertsB[1]);
    sweepline.sub(vertsB[2]);
   
    EdgeSweep sweep = new EdgeSweep(sweepline);
   
    sweep.addVerticesToSweep(true, vertsA);
    sweep.addVerticesToSweep(false, vertsB);
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(body2.getPosition());
    a1.sub(body1.getPosition());
    localAnchor1 = MathUtil.mul(rot1T,a1);
    Vector2f a2 = new Vector2f(body1.getPosition());
    a2.sub(body2.getPosition());
    localAnchor2 = MathUtil.mul(rot2T,a2);

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

TOP

Related Classes of eas.simulation.spatial.sim2D.physicalSimulation.physicsEngine.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.