Collision Detection In an ArrayList of Objects
in
Programming Questions
•
2 years ago
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:
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:
- //testing for collisions
- void collide(ArrayList sats){
- for(int i=0; i<sats.size();i++){
- Satellite sat1 = (Satellite)sats.get(i);
- for(int j=i+1;j<sats.size();j++){
- if(j!=i){
- Satellite sat2 = (Satellite)sats.get(j);
- //calculating distance
- float dx = sat1.loc.x - sat2.loc.x;
- float dy = sat1.loc.y - sat2.loc.y;
- float distance = (sqrt(dx*dx+dy*dy));
- //set the minimum distance
- float minDist = sat1.sz/2+sz/2;
- //if there is a collision: fill the ellipse; send osc value; print collision!
- if(distance<minDist){
- fill(c);
- ellipse(sat1.loc.x, sat1.loc.y, sz, sz);
- sendOSC(radius, freq);
- println("collision!");
- }
- //if the ellipses are close together, draw a line connecting them
- if(distance<(minDist*5)){
- stroke(c);
- strokeWeight(0.5);
- line(sat1.loc.x, sat1.loc.y, sat2.loc.x, sat2.loc.y);
- }
- }
- }
- }
- }
1