Package net.phys2d.math

Examples of net.phys2d.math.Vector2f


  public int collide(Contact[] contacts, Body bodyA, Body bodyB) {
    int count = collider.collide(contacts, bodyB, bodyA);
   
    // reverse the collision results by inverting normals
    for ( int i = 0; i < count; i++ ) {
      Vector2f vec = MathUtil.scale(contacts[i].getNormal(),-1);
      contacts[i].setNormal(vec);
    }
   
    return count;
  }
View Full Code Here


   */
  public void intersect(int a, int b) {
    if ( noIntersections >= MAX_INTERSECTIONS )
      return;
   
    Vector2f startA = vertsA[a];
    Vector2f endA = vertsA[(a+1) % vertsA.length ];
    Vector2f startB = vertsB[b];
    Vector2f endB = vertsB[(b+1) % vertsB.length ];
    //TODO: reuse mathutil.intersect
    float d = (endB.y - startB.y) * (endA.x - startA.x) - (endB.x - startB.x) * (endA.y - startA.y);
   
    if ( d == 0 ) // parallel lines
      return;
   
    float uA = (endB.x - startB.x) * (startA.y - startB.y) - (endB.y - startB.y) * (startA.x - startB.x);
    uA /= d;
    float uB = (endA.x - startA.x) * (startA.y - startB.y) - (endA.y - startA.y) * (startA.x - startB.x);
    uB /= d;
   
    if ( uA < 0 || uA > 1 || uB < 0 || uB > 1 )
      return; // intersection point isn't between the start and endpoints
   
    Vector2f position = new Vector2f(
        startA.x + uA * (endA.x - startA.x),
        startA.y + uA * (endA.y - startA.y));
   
    Vector2f dist = new Vector2f(position);
    dist.sub(startA);
    float distFromVertA = dist.lengthSquared();
    dist = new Vector2f(position);
    dist.sub(startB);
    float distFromVertB = dist.lengthSquared();
   
    // z axis of 3d cross product
    float sA = (startA.x - startB.x) * (endB.y - startB.y) - (endB.x - startB.x) * (startA.y - startB.y);
   
    if ( sA > 0 ) {
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.getShape()).getRadius());
      pt.add(circleBody.getPosition());
      contacts[i].setPosition(pt);
    }
   
    return count;
  }
View Full Code Here

    if (c == 's') {   
      Vector2f[] circleVerts = new Vector2f[30];
      float[] radius = {20,10};
      for( int i = 0; i < 30; i++ ) {
        float angle = (float) (3*4 * i * Math.PI/180);
        circleVerts[i] = new Vector2f(
            (float) (Math.cos(angle) * radius[i%2]),
            (float) (Math.sin(angle) * radius[i%2]));
      }
      Polygon circlePolygon = new Polygon(circleVerts);
      newBody = new Body(circlePolygon, 4);
    } else if ( c == 'w' ) {
      newBody = new Body(new Circle(15), 2);
    } else if ( c == 'l' ) {
      newBody = new Body(new Line(0,-50,0,50), 1f);
    } else if ( c == 't' ) {
      Vector2f[] triangleVerts = {new Vector2f(-20, -20), new Vector2f(20,-20), new Vector2f(20,20)};
      ConvexPolygon trianglePolygon = new ConvexPolygon(triangleVerts);
      newBody = new Body(trianglePolygon, 3);
    } else if ( c == 'b' ) {
      newBody = new Body(new Box(20,30), 3);
    } else {
View Full Code Here

      world.add(b2);
     
      bodies[i]=b2;
    }
   
    final AngleJoint aj1 = new AngleJoint(b1,bodies[0],new Vector2f(),new Vector2f(),-(float)Math.PI/9.0f,-0.0f);
    //AngleJoint aj1 = new AngleJoint(b1,bodies[0],new Vector2f(),new Vector2f(),0.0f,-0.0f);
    final DistanceJoint dj1 = new DistanceJoint(b1,bodies[0],new Vector2f(),new Vector2f(),30);
    final AngleJoint aja2 = new AngleJoint(bodies[0],b1,new Vector2f(),new Vector2f(),-(float)Math.PI/9.0f+(float)Math.PI,(float)Math.PI);
    world.add(aja2);
    world.add(dj1);
    world.add(aj1);
    for(int i=1;i<N;i++){
      final AngleJoint aj = new AngleJoint(bodies[i-1],bodies[i],new Vector2f(),new Vector2f(),-(float)Math.PI/9.0f,-0.0f);
      world.add(aj);
     
      final AngleJoint aj2 = new AngleJoint(bodies[i],bodies[i-1],new Vector2f(),new Vector2f(),-(float)Math.PI/9.0f+(float)(Math.PI+Math.sin(i/10.0f)),(float)(Math.PI+Math.sin(i/10.0f)));
      world.add(aj2);
     
      final DistanceJoint dj = new DistanceJoint(bodies[i-1],bodies[i],new Vector2f(),new Vector2f(),30);
      world.add(dj);
    }
  }
View Full Code Here

    world.add(body1);
   
    Body body2 = new Body("Cue", new Circle(20.0f),1);
    //Body body2 = new Body("Cue", new Box(40.0f,40.0f),1);
    body2.setPosition(250,0);
    body2.adjustVelocity(new Vector2f(0,100));
    body2.setRestitution(1.0f);
    body2.setFriction(0);
    world.add(body2);
  }
View Full Code Here

   
    Body body4 = new Body("Mover3", new Box(20.0f, 20.0f), 50.0f);
    body4.setPosition(200f, 200f);
    world.add(body4);
   
    SpringJoint joint1 = new SpringJoint(body1, body2, new Vector2f(150,60), new Vector2f(100, 90));
    joint1.setBrokenSpringConst(10);
    joint1.setCompressedSpringConst(0);
    joint1.setStretchedSpringConst(0);
    joint1.setMinSpringSize(0);
    joint1.setMaxSpringSize(joint1.getSpringSize());
    world.add(joint1);
   
    SpringJoint joint2 = new SpringJoint(body2, body3, new Vector2f(100,110), new Vector2f(160, 150));
    joint2.setBrokenSpringConst(10);
    joint2.setCompressedSpringConst(0);
    joint2.setStretchedSpringConst(0);
    //joint2.setSpringSize(100);
    joint2.setMinSpringSize(0);
    joint2.setMaxSpringSize(joint2.getSpringSize());
    world.add(joint2);
//   
    SpringJoint joint3 = new SpringJoint(body3, body4, new Vector2f(150,160), new Vector2f(200, 210));
    joint3.setBrokenSpringConst(100);
    joint3.setCompressedSpringConst(0);
    joint3.setStretchedSpringConst(0);
    joint3.setMinSpringSize(0);
    joint3.setMaxSpringSize(joint3.getSpringSize());
    world.add(joint3);
   
    SpringJoint joint5 = new SpringJoint(body4, body1, new Vector2f(190, 200), new Vector2f(400, 60));
    joint5.setBrokenSpringConst(1);
    joint5.setCompressedSpringConst(0.5f);
    joint5.setStretchedSpringConst(0.5f);
    joint5.setSpringSize(100);
    joint5.setMinSpringSize(50);
View Full Code Here

    int numContacts = 0;
   
    Line line = (Line) bodyA.getShape();
    Box box = (Box) bodyB.getShape();
   
    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, Body bodyA, Body 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

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.