Package net.phys2d.raw.shapes

Examples of net.phys2d.raw.shapes.Circle


    world.add(body1);
    Body body3 = new StaticBody("Ground2", new Box(200.0f, 20.0f));
    body3.setPosition(250.0f, 100);
    world.add(body3);
   
    Body swing = new Body("Swing", new Circle(10), 50);
    swing.setPosition(160.0f, 300);
    world.add(swing);
    Body swing2 = new Body("Swing", new Circle(10), 50);
    swing2.setPosition(340.0f, 300);
    world.add(swing2);
    Body swing3 = new Body("Swing", new Box(250.0f, 10.0f), 50);
    swing3.setPosition(250.0f, 285);
    swing3.setFriction(4.0f);
View Full Code Here


   * @param g The graphics context on which to draw
   * @param body The body to be drawn
   * @param fill True if we should draw it filled (indicates a collision)
   */
  protected void drawCircleBody(Graphics2D g, Body body, boolean fill) {
    Circle circle = (Circle) body.getShape();
    drawCircleBody(g,body,circle,fill);
    drawAABody(g,body,body.getShape().getBounds());
  }
View Full Code Here

    for (int i=0;i<9;i++) {
      float size = 6;
      if (i == 0) {
        size = 10;
      }
      DynamicShape shape = new Circle(size);
      Body body = new Body(shape, i == 0 ? 100.0f : 10.0f);
      body.setFriction(0.4f);
      body.setPosition(170.0f + (i*20), 171.0f);
      body.setRotation(0.0f);
      world.add(body);
View Full Code Here

    body2.setPosition(230.0f, 280.0f);
    world.add(body2);
    body2 = new Body("Mover1", new Box(40,40), 10.0f);
    body2.setPosition(280.0f, 280.0f);
    world.add(body2);
    body2 = new Body("Mover1", new Circle(20), 10.0f);
    body2.setPosition(380.0f, 280.0f);
    world.add(body2);
  }
View Full Code Here

  /**
   * @see net.phys2d.raw.collide.Collider#collide(net.phys2d.raw.Contact[], net.phys2d.raw.Body, net.phys2d.raw.Body)
   */
  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

  private void nextStep() {
    if (step == CREATE) {
      System.out.println("Generating 100");
      for (int i=0;i<100;i++) {
        float size = (float) (10 + (Math.random() * 20));
        Body body = new Body(new Circle(size),1);
        body.setPosition((float) (Math.random() * 500)+50, (float) (Math.random() * 500)+50);
        bodies.add(body);
      }
      step = COLLIDE;
    } else if (step == COLLIDE) {
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 net.phys2d.raw.collide.Collider#collide(net.phys2d.raw.Contact[], net.phys2d.raw.Body, net.phys2d.raw.Body)
   */
  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

   
    box = new Body("Faller", new Box(50,50), 1);
    box.setPosition(50,50);
    box.setRotation(-0.5f);
    world.add(box);
    Body other = new Body("Faller", new Circle(10), 1);
    other.setPosition(200,50);
    other.setRotation(-0.5f);
    world.add(other);
    other = new Body("Faller", new Circle(10), 1);
    other.setPosition(225,50);
    other.setRotation(-0.5f);
    world.add(other);
    other = new Body("Faller", new Circle(10), 1);
    other.setPosition(250,50);
    other.setRotation(-0.5f);
    world.add(other);
    other = new Body("Faller", new Circle(10), 1);
    other.setPosition(275,50);
    other.setRotation(-0.5f);
    world.add(other);
    other = new Body("Faller", new Circle(10), 1);
    other.setPosition(300,50);
    other.setRotation(-0.5f);
    world.add(other);
  }
View Full Code Here

 
  /**
   * @see net.phys2d.raw.test.AbstractDemo#init(net.phys2d.raw.World)
   */
  protected void init(World world) {
    b1= new Body(new Circle(15),1e3f);
    b1.setMoveable(false);
    b1.setPosition(250,250);
    b1.setRotDamping(10);
    b1.setRotation(0.01f);
   
    world.add(b1);
    final int N=15;
    final Body bodies[] = new Body[N];
    for(int i=0;i<N;i++){
      final Body b2 = new Body(new Circle(8),10);
      b2.setPosition(250+30*(1+i),250);
      world.add(b2);
     
      bodies[i]=b2;
    }
 
View Full Code Here

TOP

Related Classes of 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.