Loading...
Logo
Processing Forum
Hi,

I've been reading lots about collision detection on these forums, reading about box2d, fiscia, geomerative and others, but all the threads I find relate to collision detection of an unchanging vector shape.

My ultimate goal is to be able to test if a point in 2D space is within the bounds of an irregular polygon, whose size, shape and location is potentially changing 25 times a second.

Unrealistic? An approximation would do, maybe less prescision in the "bounding box" or updates at less than 25 frames a second.

The situation: I'm using the computer vision library Jmyron to do blob tracking, which is working very well. I can extract the blobs as the co-ordinates that form quads, co-ordinates that form vector shapes or as a list of "edge pixels".

The other half of my project is a really basic random particle system. Particles whizzing about with random directions and velocities. The idea is that these particles can't intersect with the blobs that Jmyron detects, so if a hand-shaped blob is detected, the particles will go anywhere around the "silhouette" of the hand, including between the fingers, but won't go inside the outline of the hand.

Crazy?

If anyone could point me in the right direction, I would be very grateful.

Here's a quick link to the fairly brief Jmyron reference:
http://webcamxtra.sourceforge.net/reference.shtml

Here's an image of the blob of a biro drawn as a vector (code below)
http://ilumos.co.uk/misc/blob_as_vector_outline.png

Copy code
  1. void drawGlobVectors() {
  2.   stroke(0,150,0);
  3.   fill(255,0,0,128);
  4.   int[][][] list = m.globEdgePoints(30);//get the outlines
  5.   for(int i=0;i<list.length;i++){
  6.     beginShape();
  7.     if(list[i]!=null){
  8.       for(int ii=0;ii<list[i].length;ii++){
  9.         vertex(list[i][ii][0],list[i][ii][1]);
  10.       }
  11.     }
  12.     endShape();
  13.   }
  14. }

Replies(2)

You could use toxiclibs and the Polygon2D class to check each particle for containment (collision/intersection).

The following assumes you have a small Particle class extending Vec2D something like that:
Copy code
  1. class Particle extends Vec2D {
  2.   // add other properties:
  3.   // velocity, color, size etc.

  4.   Particle(float x, float y) {
  5.     super(x, y);
  6.   }
  7. }
Then the changed code to make use of this and the Polygon2D class:

Copy code
  1. import toxi.geom.*;
  2. import toxi.processing.*;

  3. // list of particles
  4. List<Particle> particles=new ArrayList<Particle>();

  5. // Helper class for drawing
  6. ToxiclibsSupport gfx;

  7. void setup() {
  8.   ...
  9.   gfx=new ToxiclibsSupport(this);
  10. }

  11. void drawGlobVectors() {
  12.   int[][][] list = m.globEdgePoints(30);
  13.   for (int i=0; i<list.length; i++) {
  14.     int[][] verts=list[i];
  15.     if (verts!=null) {
  16.       // create empty polygon and populate with blob vertices
  17.       Polygon2D poly=new Polygon2D();
  18.       for (int ii=0; ii<list[i].length; ii++) {
  19.         poly.add(verts[ii][0], verts[ii][1]);
  20.       }
  21.       // check all particles for intersection
  22.       for (Particle p : particles) {
  23.         if (poly.containsPoint(p)) {
  24.           // do something to resolve collision/move particle out from poly
  25.         }
  26.       }
  27.       //draw polygon
  28.       stroke(0, 150, 0);
  29.       fill(255, 0, 0, 128);
  30.       gfx.polygon2D(poly);
  31.     }
  32.   }
  33. }

http://postspectacular.com | http://toxiclibs.org
Fantastic!

Many thanks to you indeed for your time answering my question with a big code example.