Package eas.simulation.spatial.sim2D.physicalSimulation.physicsEngine.net.phys2d.raw.shapes

Examples of eas.simulation.spatial.sim2D.physicalSimulation.physicsEngine.net.phys2d.raw.shapes.Circle


    boolean touches = bodyA.getBodyShape().getBounds().touches(x1,y1,bodyB.getBodyShape().getBounds(),x2,y2);
    if (!touches) {
      return 0;
    }
   
    Circle circleA = (Circle) bodyA.getBodyShape();
    Circle circleB = (Circle) bodyB.getBodyShape();
   
    touches = circleA.touches(x1,y1,circleB,x2,y2);
    if (!touches) {
      return 0;
    }
   
    Vector2f normal = MathUtil.sub(bodyB.getPosition(),bodyA.getPosition());
    float sep = (circleA.getRadius() + circleB.getRadius()) - normal.length();

    normal.normalise();
    Vector2f pt = MathUtil.scale(normal, circleA.getRadius());
    pt.add(bodyA.getPosition());
View Full Code Here


   * @see eas.simulation.spatial.sim2D.physicalSimulation.physicsEngine.net.phys2d.raw.collide.Collider#collide(eas.simulation.spatial.sim2D.physicalSimulation.physicsEngine.net.phys2d.raw.Contact[], eas.simulation.spatial.sim2D.physicalSimulation.physicsEngine.net.phys2d.raw.Body, eas.simulation.spatial.sim2D.physicalSimulation.physicsEngine.net.phys2d.raw.Body)
   */
  @Override
    public int collide(Contact[] contacts, PhysicsAgent2D<?> bodyA, PhysicsAgent2D<?> bodyB) {
    Polygon polyA = (Polygon) bodyA.getBodyShape();
    Circle circle = (Circle) bodyB.getBodyShape();
   
    // TODO: this can be optimized using matrix multiplications and moving only the circle
    Vector2f[] vertsA = polyA.getVertices(bodyA.getPosition(), bodyA.getRotation());
   
    Vector2f centroidA = new Vector2f(polyA.getCentroid());
    centroidA.add(bodyA.getPosition());

   
    int[][] collPairs = getCollisionCandidates(vertsA, centroidA, circle.getRadius(), bodyB.getPosition());

    int noContacts = 0;
    for ( int i = 0; i < collPairs.length; i++ ) {
      if ( noContacts >= contacts.length )
        return contacts.length;
     
      Vector2f lineStartA = vertsA[collPairs[i][0]];
      Vector2f lineEndA = vertsA[(collPairs[i][0]+1) % vertsA.length ];
      Line line = new Line(lineStartA, lineEndA);
           
      float dis2 = line.distanceSquared(bodyB.getPosition());
      float r2 = circle.getRadius() * circle.getRadius();

      if ( dis2 < r2 ) {
        Vector2f pt = new Vector2f();
       
        line.getClosestPoint(bodyB.getPosition(), pt);
        Vector2f normal = new Vector2f(bodyB.getPosition());
        normal.sub(pt);
        float sep = circle.getRadius() - normal.length();
        normal.normalise();
       
        contacts[noContacts].setSeparation(-sep);
        contacts[noContacts].setPosition(pt);
        contacts[noContacts].setNormal(normal);
View Full Code Here

    if (!touches) {
      return 0;
    }
   
    Box box = (Box) boxBody.getBodyShape();
    Circle circle = (Circle) circleBody.getBodyShape();
   
    Vector2f[] pts = box.getPoints(boxBody.getPosition(), boxBody.getRotation());
    Line[] lines = new Line[4];
    lines[0] = new Line(pts[0],pts[1]);
    lines[1] = new Line(pts[1],pts[2]);
    lines[2] = new Line(pts[2],pts[3]);
    lines[3] = new Line(pts[3],pts[0]);
   
    float r2 = circle.getRadius() * circle.getRadius();
    int closest = -1;
    float closestDistance = Float.MAX_VALUE;
   
    for (int i=0;i<4;i++) {
      float dis = lines[i].distanceSquared(circleBody.getPosition());
      if (dis < r2) {
        if (closestDistance > dis) {
          closestDistance = dis;
          closest = i;
        }
      }
    }
   
    if (closest > -1) {
      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);
View Full Code Here

   * @see eas.simulation.spatial.sim2D.physicalSimulation.physicsEngine.net.phys2d.raw.collide.Collider#collide(eas.simulation.spatial.sim2D.physicalSimulation.physicsEngine.net.phys2d.raw.Contact[], eas.simulation.spatial.sim2D.physicalSimulation.physicsEngine.net.phys2d.raw.Body, eas.simulation.spatial.sim2D.physicalSimulation.physicsEngine.net.phys2d.raw.Body)
   */
  @Override
    public int collide(Contact[] contacts, PhysicsAgent2D<?> bodyA, PhysicsAgent2D<?> bodyB) {
    Line line = (Line) bodyA.getBodyShape();
    Circle circle = (Circle) bodyB.getBodyShape();
   
    Vector2f[] vertsA = line.getVertices(bodyA.getPosition(), bodyA.getRotation());
   
    // compute intersection of the line A and a line parallel to
    // the line A's normal passing through the origin of B
    Vector2f startA = vertsA[0];
    Vector2f endA = vertsA[1];
    ROVector2f startB = bodyB.getPosition();
    Vector2f endB = new Vector2f(endA);
    endB.sub(startA);
    endB.set(endB.y, -endB.x);
//    endB.add(startB);// TODO: inline endB into equations below, this last operation will be useless..
   
    //TODO: reuse mathutil.intersect
//    float d = (endB.y - startB.getY()) * (endA.x - startA.x);
//    d -= (endB.x - startB.getX()) * (endA.y - startA.y);
//   
//    float uA = (endB.x - startB.getX()) * (startA.y - startB.getY());
//    uA -= (endB.y - startB.getY()) * (startA.x - startB.getX());
//    uA /= d;
    float d = endB.y * (endA.x - startA.x);
    d -= endB.x * (endA.y - startA.y);
   
    float uA = endB.x * (startA.y - startB.getY());
    uA -= endB.y * (startA.x - startB.getX());
    uA /= d;
   
    Vector2f position = null;
   
    if ( uA < 0 ) { // the intersection is somewhere before startA
      position = startA;
    } else if ( uA > 1 ) { // the intersection is somewhere after endA
      position = endA;
    } else {
      position = new Vector2f(
          startA.x + uA * (endA.x - startA.x),
          startA.y + uA * (endA.y - startA.y));
    }
   
    Vector2f normal = endB; // reuse of vector object
    normal.set(startB);
    normal.sub(position);
    float distSquared = normal.lengthSquared();
    float radiusSquared = circle.getRadius() * circle.getRadius();
   
    if ( distSquared < radiusSquared ) {
      contacts[0].setPosition(position);
      contacts[0].setFeature(new FeaturePair());
     
      normal.normalise();
      contacts[0].setNormal(normal);
     
      float separation = (float) Math.sqrt(distSquared) - circle.getRadius();
      contacts[0].setSeparation(separation);
     
      return 1;
    }
   
View Full Code Here

   * @see eas.simulation.spatial.sim2D.physicalSimulation.physicsEngine.net.phys2d.raw.collide.Collider#collide(eas.simulation.spatial.sim2D.physicalSimulation.physicsEngine.net.phys2d.raw.Contact[], eas.simulation.spatial.sim2D.physicalSimulation.physicsEngine.net.phys2d.raw.Body, eas.simulation.spatial.sim2D.physicalSimulation.physicsEngine.net.phys2d.raw.Body)
   */
  @Override
    public int collide(Contact[] contacts, Body bodyA, Body bodyB) {
    Line line = (Line) bodyA.getShape();
    Circle circle = (Circle) bodyB.getShape();
   
    Vector2f[] vertsA = line.getVertices(bodyA.getPosition(), bodyA.getRotation());
   
    // compute intersection of the line A and a line parallel to
    // the line A's normal passing through the origin of B
    Vector2f startA = vertsA[0];
    Vector2f endA = vertsA[1];
    ROVector2f startB = bodyB.getPosition();
    Vector2f endB = new Vector2f(endA);
    endB.sub(startA);
    endB.set(endB.y, -endB.x);
//    endB.add(startB);// TODO: inline endB into equations below, this last operation will be useless..
   
    //TODO: reuse mathutil.intersect
//    float d = (endB.y - startB.getY()) * (endA.x - startA.x);
//    d -= (endB.x - startB.getX()) * (endA.y - startA.y);
//   
//    float uA = (endB.x - startB.getX()) * (startA.y - startB.getY());
//    uA -= (endB.y - startB.getY()) * (startA.x - startB.getX());
//    uA /= d;
    float d = endB.y * (endA.x - startA.x);
    d -= endB.x * (endA.y - startA.y);
   
    float uA = endB.x * (startA.y - startB.getY());
    uA -= endB.y * (startA.x - startB.getX());
    uA /= d;
   
    Vector2f position = null;
   
    if ( uA < 0 ) { // the intersection is somewhere before startA
      position = startA;
    } else if ( uA > 1 ) { // the intersection is somewhere after endA
      position = endA;
    } else {
      position = new Vector2f(
          startA.x + uA * (endA.x - startA.x),
          startA.y + uA * (endA.y - startA.y));
    }
   
    Vector2f normal = endB; // reuse of vector object
    normal.set(startB);
    normal.sub(position);
    float distSquared = normal.lengthSquared();
    float radiusSquared = circle.getRadius() * circle.getRadius();
   
    if ( distSquared < radiusSquared ) {
      contacts[0].setPosition(position);
      contacts[0].setFeature(new FeaturePair());
     
      normal.normalise();
      contacts[0].setNormal(normal);
     
      float separation = (float) Math.sqrt(distSquared) - circle.getRadius();
      contacts[0].setSeparation(separation);
     
      return 1;
    }
   
View Full Code Here

    boolean touches = bodyA.getShape().getBounds().touches(x1,y1,bodyB.getShape().getBounds(),x2,y2);
    if (!touches) {
      return 0;
    }
   
    Circle circleA = (Circle) bodyA.getShape();
    Circle circleB = (Circle) bodyB.getShape();
   
    touches = circleA.touches(x1,y1,circleB,x2,y2);
    if (!touches) {
      return 0;
    }
   
    Vector2f normal = MathUtil.sub(bodyB.getPosition(),bodyA.getPosition());
    float sep = (circleA.getRadius() + circleB.getRadius()) - normal.length();

    normal.normalise();
    Vector2f pt = MathUtil.scale(normal, circleA.getRadius());
    pt.add(bodyA.getPosition());
View Full Code Here

   * @see eas.simulation.spatial.sim2D.physicalSimulation.physicsEngine.net.phys2d.raw.collide.Collider#collide(eas.simulation.spatial.sim2D.physicalSimulation.physicsEngine.net.phys2d.raw.Contact[], eas.simulation.spatial.sim2D.physicalSimulation.physicsEngine.net.phys2d.raw.Body, eas.simulation.spatial.sim2D.physicalSimulation.physicsEngine.net.phys2d.raw.Body)
   */
  @Override
    public int collide(Contact[] contacts, Body bodyA, Body bodyB) {
    Polygon polyA = (Polygon) bodyA.getShape();
    Circle circle = (Circle) bodyB.getShape();
   
    // TODO: this can be optimized using matrix multiplications and moving only the circle
    Vector2f[] vertsA = polyA.getVertices(bodyA.getPosition(), bodyA.getRotation());
   
    Vector2f centroidA = new Vector2f(polyA.getCentroid());
    centroidA.add(bodyA.getPosition());

   
    int[][] collPairs = getCollisionCandidates(vertsA, centroidA, circle.getRadius(), bodyB.getPosition());

    int noContacts = 0;
    for ( int i = 0; i < collPairs.length; i++ ) {
      if ( noContacts >= contacts.length )
        return contacts.length;
     
      Vector2f lineStartA = vertsA[collPairs[i][0]];
      Vector2f lineEndA = vertsA[(collPairs[i][0]+1) % vertsA.length ];
      Line line = new Line(lineStartA, lineEndA);
           
      float dis2 = line.distanceSquared(bodyB.getPosition());
      float r2 = circle.getRadius() * circle.getRadius();

      if ( dis2 < r2 ) {
        Vector2f pt = new Vector2f();
       
        line.getClosestPoint(bodyB.getPosition(), pt);
        Vector2f normal = new Vector2f(bodyB.getPosition());
        normal.sub(pt);
        float sep = circle.getRadius() - normal.length();
        normal.normalise();
       
        contacts[noContacts].setSeparation(-sep);
        contacts[noContacts].setPosition(pt);
        contacts[noContacts].setNormal(normal);
View Full Code Here

    if (!touches) {
      return 0;
    }
   
    Box box = (Box) boxBody.getShape();
    Circle circle = (Circle) circleBody.getShape();
   
    Vector2f[] pts = box.getPoints(boxBody.getPosition(), boxBody.getRotation());
    Line[] lines = new Line[4];
    lines[0] = new Line(pts[0],pts[1]);
    lines[1] = new Line(pts[1],pts[2]);
    lines[2] = new Line(pts[2],pts[3]);
    lines[3] = new Line(pts[3],pts[0]);
   
    float r2 = circle.getRadius() * circle.getRadius();
    int closest = -1;
    float closestDistance = Float.MAX_VALUE;
   
    for (int i=0;i<4;i++) {
      float dis = lines[i].distanceSquared(circleBody.getPosition());
      if (dis < r2) {
        if (closestDistance > dis) {
          closestDistance = dis;
          closest = i;
        }
      }
    }
   
    if (closest > -1) {
      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);
View Full Code Here

TOP

Related Classes of eas.simulation.spatial.sim2D.physicalSimulation.physicsEngine.net.phys2d.raw.shapes.Circle

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.