Loading...
Logo
Processing Forum
Hello!

I'm working on a musical interface with pure data and processing, but have come across a problem. Basically I want the user to be able to add a new Satellite that travels in a circle at a specified radius and at a random speed. If the user adds more than one Satellite object to a given radius, then when the two collide I want to send an osc value to pure data - triggering a sound. I've been approaching it a bit like a particle system so far - having my Satellite class, then having a Satellite System class to manage the adding/subtracting of objects from the arraylist, and a draw method to run the sketch. My problem lies with detecting if the satellites have collided though - I'm using two for loops to compare the x and y values between the satellites, but when a collision is detected, it sends the osc value 11 times! I'm sure that it is because of the for loops, but what is the best way around this problem? If anybody has any ideas I would be very thankful!

Hope I'm making sense - I haven't included all the code as there is quite a bit of it already. The problem bit is included below:

Copy code
  1.   //testing for collisions
  2.   void collide(ArrayList sats){
  3.     for(int i=0; i<sats.size();i++){
  4.       Satellite sat1 = (Satellite)sats.get(i);
  5.       for(int j=i+1;j<sats.size();j++){
  6.         if(j!=i){
  7.           Satellite sat2 = (Satellite)sats.get(j);
  8.          
  9.           //calculating distance
  10.           float dx = sat1.loc.x - sat2.loc.x;
  11.           float dy = sat1.loc.y - sat2.loc.y;
  12.           float distance = (sqrt(dx*dx+dy*dy));
  13.          
  14.           //set the minimum distance
  15.           float minDist = sat1.sz/2+sz/2;
  16.           //if there is a collision: fill the ellipse; send osc value; print collision!
  17.           if(distance<minDist){
  18.             fill(c);
  19.             ellipse(sat1.loc.x, sat1.loc.y, sz, sz);
  20.             sendOSC(radius, freq);
  21.             println("collision!");
  22.           }
  23.           //if the ellipses are close together, draw a line connecting them
  24.           if(distance<(minDist*5)){
  25.             stroke(c);
  26.             strokeWeight(0.5);
  27.             line(sat1.loc.x, sat1.loc.y, sat2.loc.x, sat2.loc.y);
  28.           }
  29.         }
  30.       }
  31.     }
  32.   }

Replies(2)

Is it consistently 11 repetitions or just a random number of repeated OSC calls?  I'd expect the latter; though it could be consistent if they always travel at a consistent speed:  it doesn't look like you do anything to separate ellipses that have collided; meaning that the collision detection will prove true in following frames.

Now with standard bouncing ball 'physics' when two balls collide you'd invert their velocities and place them so they were no longer touching - thereby avoiding repeated collisions in the next frame as they'd fly off in opposite directions.  Perhaps that's not what you want to happen with your satellites, in which case you're going to have to debounce it somehow...  That would probably involve adding a boolean variable to your Satellite object, only performing the OSC call if it's false and setting it to true after you make the OSC call.  That will avoid repeated calls on the same collision.  You'd then need to set it back to false when the collision is no longer occurring - i.e. when there is no collision and the boolean variable is true set it to false.
Thanks muchly Blindfish, you were right about the debouncing - I didn't consider that might be the problem. It's fixed now! :)