Package net.phys2d.math

Examples of net.phys2d.math.Vector2f


   * @see net.phys2d.raw.Joint#preStep(float)
   */
  public void preStep(float invDT) {
    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);
    Vector2f dp = new Vector2f(p2);
    dp.sub(p1);
    float length = dp.length();
    // dp.scale(1.0f/length);
    Vector2f V = new Vector2f((float) Math.cos(originalAngle
        + body1.getRotation()), (float) Math.sin(originalAngle
        + body1.getRotation()));
    Vector2f ndp = new Vector2f(dp);
    ndp.normalise();
    float torq = (float) Math.asin(MathUtil.cross(ndp, V))
        * compressConstant / invDT;
    float P = torq / length;
    Vector2f n = new Vector2f(ndp.y, -ndp.x);
    Vector2f impulse = new Vector2f(n);
    impulse.scale(P);
    if (!body1.isStatic()) {
      Vector2f accum1 = new Vector2f(impulse);
      accum1.scale(body1.getInvMass());
      body1.adjustVelocity(accum1);
      body1.adjustAngularVelocity((body1.getInvI() * MathUtil.cross(dp,
          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


      }
      if (b.isResting() && restingBodyDetection) {
        continue;
      }

      Vector2f temp = new Vector2f(b.getForce());
      temp.scale(b.getInvMass());
      if (b.getGravityEffected()) {
        temp.add(gravity);
      }
      temp.scale(dt);
     
      b.adjustVelocity(temp);
     
      Vector2f damping = new Vector2f(b.getVelocity());
      damping.scale(-b.getDamping() * b.getInvMass());
      b.adjustVelocity(damping);
     
      b.adjustAngularVelocity(dt * b.getInvI() * b.getTorque());
      b.adjustAngularVelocity(-b.getAngularVelocity() * b.getInvI() * b.getRotDamping());
    }
 
View Full Code Here

   */
  public void applyImpulse() {
    if(collideSide==COLLIDE_NONE)
      return;
   
    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 reln = dv.dot(ndp);
    reln = restitute-reln;
    float P = reln/K;
    float newImpulse;
    if(collideSide==COLLIDE_MIN){
      newImpulse = accumulateImpulse+P<0.0f?accumulateImpulse+P:0.0f;
    }else{
      newImpulse = accumulateImpulse+P>0.0f?accumulateImpulse+P:0.0f;
    }
    P = newImpulse-accumulateImpulse;
    accumulateImpulse = newImpulse;
   
    Vector2f impulse = new Vector2f(ndp);
    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

      throw new IllegalArgumentException("A polygon can not have fewer than 3 edges!");
   
    this.vertices = new Vector2f[vertices.length];
   
    for ( int i = 0; i < vertices.length; i++ ) {
      this.vertices[i] = new Vector2f(vertices[i]);
    }
   
   
    float r = computeBoundingCircleRadius();
    this.bounds = new AABox(r*2,r*2);
 
View Full Code Here

   * @return this polygon's computed area
   */
  protected float computeArea() {
    this.area = 0;
   
    Vector2f v1, v2;
   
    for ( int i = 0; i < vertices.length; i++ ) {
      v1 = vertices[i];
      v2 = vertices[(i+1) % vertices.length];
     
View Full Code Here

   */
  protected Vector2f computeCentroid() {
    float x = 0;
    float y = 0;
   
    Vector2f v1, v2;
   
    for ( int i = 0; i < vertices.length; i++ ) {
      v1 = vertices[i];
      v2 = vertices[(i+1) % vertices.length];
     
      x += (v1.x + v2.x) * (v1.x * v2.y - v2.x * v1.y);
      y += (v1.y + v2.y) * (v1.x * v2.y - v2.x * v1.y);
    }
   
    return new Vector2f(x / (6 * this.area), y / (6 * this.area));
  }
 
View Full Code Here

  public boolean isConvex() {
    // check if all angles are smaller or equal to 180 degrees
    int l = vertices.length;
   
    for ( int i = 0; i < vertices.length; i++ ) {
      Vector2f x = vertices[i];
      Vector2f y = vertices[(i+1)%l];
      Vector2f z = vertices[(i+2)%l];
     
      // does the 3d cross product point up or down?
      if ( (z.x-x.x)*(y.y-x.y)-(y.x-x.x)*(z.y-x.y) >= 0 )
        return false;
    }
View Full Code Here

      float x = vertices[i].x * cos - vertices[i].y * sin;
      float y = vertices[i].y * cos + vertices[i].x * sin;
      x += displacement.getX();
      y += displacement.getY();
     
      retVertices[i] = new Vector2f(x, y);
    }
   
    return retVertices;
  }
View Full Code Here

   */
  public Vector2f getCentroid(ROVector2f displacement, float rotation) {
    float cos = (float) Math.cos(rotation);
    float sin = (float) Math.sin(rotation);
   
    return new Vector2f(
        centroid.x * cos - centroid.y * sin + displacement.getX(),
        centroid.y * cos + centroid.x * sin + displacement.getY());
  }
View Full Code Here

    Matrix2f rot2 = new Matrix2f(body2.getRotation());

     r1 = MathUtil.mul(rot1,anchor1);
     r2 = MathUtil.mul(rot2,anchor2);
   
    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);
   
    Vector2f dv = new Vector2f(body2.getVelocity());
    dv.add(MathUtil.cross(body2.getAngularVelocity(),r2));
    dv.sub(body1.getVelocity());
    dv.sub(MathUtil.cross(body1.getAngularVelocity(),r1));
     
    ndp = new Vector2f(dp);
    ndp.normalise();
   
    restitute = -restitutionConstant*dv.dot(ndp);
   
    Vector2f v1 = new Vector2f(ndp);
    v1.scale(-body2.getInvMass() - body1.getInvMass());

    Vector2f v2 = MathUtil.cross(MathUtil.cross(r2, ndp), r2);
    v2.scale(-body2.getInvI());
   
    Vector2f v3 = MathUtil.cross(MathUtil.cross(r1, ndp),r1);
    v3.scale(-body1.getInvI());
   
    Vector2f K1 = new Vector2f(v1);
    K1.add(v2);
    K1.add(v3);
   
    K = K1.dot(ndp);
    float length=dp.lengthSquared();
    if(length<minDistance){
      if(collideSide!=COLLIDE_MIN)
        accumulateImpulse=0;
      collideSide=COLLIDE_MIN;     
      biasImpulse=-biasFactor*(length-minDistance);
      if(restitute<0)
        restitute=0;
    }else if(length>maxDistance){
      if(collideSide!=COLLIDE_MAX)
        accumulateImpulse=0;
      collideSide=COLLIDE_MAX;
      biasImpulse=-biasFactor*(length-maxDistance);
      if(restitute>0)
        restitute=0;
    }else{
      collideSide=COLLIDE_NONE;
      accumulateImpulse=0;
    }
    restitute+=biasImpulse;
    Vector2f impulse = new Vector2f(ndp);
    impulse.scale(accumulateImpulse);
   
    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.