Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Collision between polygon with Toxiclib

$
0
0

Hello !!

I hope to make a video puzzle with pieces that look like a physical piece, but I don’t understand how to make to generate collisions between them. I use the Toxiclibs and this is my actual code.

Thank you for your help.

import toxi.geom.*; import toxi.processing.*;

boolean draging; Surface surface; ToxiclibsSupport gfx;

void settings() { size(900, 900); }

void setup() { gfx = new ToxiclibsSupport(this); surface = new Surface(width, height, 3, 3); }

void draw() { background(255);

for (Piece piece : surface.pieces) { piece.display(gfx); }

display_framerate(0); }

void mousePressed() { draging = surface.selectPiece(); }

void mouseDragged() { if (draging) { surface.collision(gfx); } }

void mouseReleased() { if (draging) { surface.deselectPiece(); } }

void display_framerate(color c) { pushStyle(); fill(c); textSize(14); textAlign(LEFT, TOP); text("framerate: " + floor(frameRate), 6, 6); popStyle(); }

class Piece { float
w, h; color cFill, cStroke; Vec2D location, offset; Vec2D[] vertices; Polygon2D shape; ConvexPolygonClipper convexClipper;

Piece(Vec2D location, float w, float h) { this.w = w; this.h = h; this.location = new Vec2D(); this.offset = new Vec2D(); this.shape = new Polygon2D();

this.cStroke = 255;
this.cFill = 128;

this.calcVertices();
this.think();
this.moveTo(location.addSelf(w*0.5, h*0.5));

}

void moveTo(Vec2D newLocation) { newLocation.subSelf(this.location).subSelf(offset); shape.translate(newLocation); this.location.addSelf(newLocation); this.convexClipper = new ConvexPolygonClipper(this.shape); }

void calcVertices() { this.vertices = new Vec2D[4]; vertices[0] = (new Vec2D(0, 0)); vertices[1] = (new Vec2D(w-30, 0)); vertices[2] = (new Vec2D(w-20, h+10)); vertices[3] = (new Vec2D(0, h-10)); }

void calcOffset(Vec2D location) { offset = location.sub(shape.getCentroid()); }

void changeColor(color cStroke, color cFill) { this.cFill = cFill; this.cStroke = cStroke; }

void think() { for (Vec2D vertice : vertices) { shape.add(vertice); } shape.center(); }

void display(ToxiclibsSupport gfx) { pushStyle(); stroke(0); noFill(); gfx.polygon2D(shape); popStyle(); }

}

class Surface { int totalPiece; float w, h; Piece selectedPiece; Piece[] pieces;

Surface(float w, float h, int nW, int nH) { this.w = w; this.h = h;

float pieceW = w/nW;
float pieceH = h/nH;

this.totalPiece = nW * nH;

this.pieces = new Piece[2];

this.pieces[0] = new Piece(new Vec2D(0, 0), pieceW, pieceH);
this.pieces[1] = new Piece(new Vec2D(pieceW, 0), pieceW, pieceH);

for (int y = 0; y < nW; y++)
{
  for (int x = 0; x < nH; x++)
  {
    this.pieces[y * nW + x] = (new Piece(new Vec2D(x * pieceW, y * pieceH), pieceW, pieceH));
  }
}

}

boolean selectPiece() { for (int i = this.pieces.length - 1; i >= 0; i--) { if (pieces[i].shape.containsPoint(new Vec2D(mouseX, mouseY))) { pieces[i].calcOffset(new Vec2D(mouseX, mouseY)); selectedPiece = pieces[i];

    this.pieces = placeItemToEnd(pieces, i);

    return true;
  }
}

return false;

}

void deselectPiece() { selectedPiece = null; }

void collision(ToxiclibsSupport gfx) { for (Piece piece : pieces) { if (piece == selectedPiece) continue;

  for (Line2D a : piece.shape.getEdges())
  {
    int i = 0;
    for (Line2D b : selectedPiece.shape.getEdges())
    {
      Line2D.LineIntersection c = b.intersectLine(a);

      if (c.getPos() != null && c.getType() == Line2D.LineIntersection.Type.INTERSECTING)
      {
        ellipse(c.getPos().x, c.getPos().y, 10, 10);
        fill(0);
        text(i, c.getPos().x+10, c.getPos().y+10);
        println(i, c.getPos(), c.getType(), c.getCoefficients()[0], c.getCoefficients()[1]);
        selectPiece();
      }
      i++;
    }
  }
}

surface.selectedPiece.moveTo(new Vec2D(mouseX, mouseY));

}

Piece[] placeItemToEnd(Piece[] array, int itemPosition) { if (itemPosition == array.length - 1) return array;

for (int i = itemPosition; i < array.length - 1; i++)
{
  Piece temp = array[i];
  array[i] = array[i+1];
  array[i+1] = temp;
}

return array;

} }


Viewing all articles
Browse latest Browse all 2896

Trending Articles