We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › problem with collision detection
Page Index Toggle Pages: 1
problem with collision detection (Read 600 times)
problem with collision detection
Oct 20th, 2009, 5:54pm
 
so i am attempting to detect the collisions between a number of orbs on the screen. each orb can be selected with the mouse and dragged around the screen. eventually moving other orbs when they collide. i am currently using the equation of a circle to map the x,y coords for each orb on screen into two arrays(one for x values and one for y). currently it is detecting collisions when there are none happening and i can't figure out if i have simply miss written something or my syntax is off or if something far greater is causing problems. if you guys and girls out there can see anything that i can't please let me know. thanks

Code:
import ddf.minim.*;

int orbsize = 50;
int orbcount = 10;

AudioSample gong;
Minim minim;

Orb[] orbs = new Orb[orbcount];
float[] orbsx = new float[orbcount];
float[] orbsy = new float[orbcount];

boolean once = true;

int[] xtrail = new int[orbsize];
int[] ytrail = new int[orbsize];

float[][] detcolx = new float[orbcount][360];
float[][] detcoly = new float[orbcount][360];

void setup(){
 size(960,600);
 smooth();

 for(int i = 0; i < orbs.length; i++){
   orbsx[i] = random(25,935);
   orbsy[i] = random(25,575);
   orbs[i] = new Orb(orbsx[i],orbsy[i],orbsize);
 }

 for (int i = 0; i < xtrail.length; i ++ ) {
   xtrail[i] = 0;
   ytrail[i] = 0;
 }



 minim = new Minim(this);
 gong = minim.loadSample("gong_d4.wav");


 //checkoverlap();
}
void draw(){

 background(0);
 //  for(int i = 0; i < detcolx.length; i++){
 //    detcolx[i] = new ArrayList();
 //    detcoly[i] = new ArrayList();
 //  }


 for(int i = 0; i < orbs.length; i++){
   orbs[i].display();
 }

 //checkoverlap();


 for(int i = 0; i < orbs.length; i++){

   if(mouseX > orbs[i].orbx-orbs[i].orbdia/2 && mouseX < orbs[i].orbx+orbs[i].orbdia/2 && mouseY > orbs[i].orby-orbs[i].orbdia/2 && mouseY < orbs[i].orby+orbs[i].orbdia/2 && mousePressed){
     orbs[i].orbx = mouseX;
     orbs[i].orby = mouseY;
     for(int n = 0; n < 360; n++){
       detcolx[i][n] = (orbs[i].orbx + orbs[i].orbdia*cos(n));
       detcoly[i][n] = (orbs[i].orby + orbs[i].orbdia*sin(n));
       println(n + ". " + detcolx[i][n] + ", " + detcoly[i][n]);
     }
     stroke(255);
     fill(0);

     for (int x = 0; x < xtrail.length-1; x ++ ) {
       xtrail[x] = xtrail[x+1];
       ytrail[x] = ytrail[x+1];
     }
     xtrail[xtrail.length-1] = mouseX;
     ytrail[ytrail.length-1] = mouseY;

     for (int t = 0; t < xtrail.length; t ++ ) {
       ellipse(xtrail[t],ytrail[t],t,t);
     }

     orbs[i].mouse = true;
     //      if(once == true){
     //        gong.trigger();
     //        once = false;
     //      }
     //player.rewind();
     detectCollision();
   }
   else if(!mousePressed){
     orbs[i].mouse = false;
     //minim.stop();
     //player.cue(0);
     //player = minim.loadFile("gong_d4.wav");
     //      if(once == true){
     //        player = minim.loadFile("gong_d4.wav");
     //        once = false;
     //      }
   }
 }

}

void checkoverlap(){
 int temp = 0;
 for(int i = 0; i < orbsx.length; i++){
   for(int x = 1; x < orbsx.length; x++){
     if((sqrt(sq(orbs[i].orbx-orbs[x].orbx)+sq(orbs[i].orby-orbs[x].orby))) < 50){

       orbs[i].orbx+=2;
       orbs[i].orby+=2;
       orbs[x].orbx-=2;
       orbs[x].orby-=2;
       //orbs[x].mouse = true;
       //player.play();
       //player.rewind();

     }
   }
 }
}

void mouseReleased(){
 //  gong.close();
 //  minim.stop();
 //  once = true;
}



void detectCollision(){
 for(int i = 0; i < detcolx.length; i++){
   for(int x = 1; x < detcolx.length; x ++){
     for(int n = 0; n < 360; n++){
       for(int t = 0; t < 360; t++){
         if(detcolx[i][n] == detcolx[x][t] && detcoly[i][n] == detcoly[i][t]){
           //println("Success!");
           //orbs[i].orbx+=25;
         }
       }
     }
   }
 }
}
class Orb{
 float orbx;
 float orby;
 float orbdia;
 boolean mouse;
 
 Orb(float x, float y, float dia){
   orbx = x;
   orby = y;
   orbdia = dia;
   mouse = false;

 }

 void display(){
   if(mouse == false){
     stroke(100,100,100);
   }
   else if(mouse == true){
     stroke(255);
   }
   strokeWeight(2);
   fill(0);
   ellipseMode(CENTER);
   ellipse(orbx, orby, 50,50);
   
 }
 
}
Re: problem with collision detection
Reply #1 - Oct 20th, 2009, 7:29pm
 
Processing uses radians rather than degrees, 0 to TWO_PI.  And if orbdia is diameter, that might be a source of problems -- it looks like you are essentially swinging an arm from the midpoint of the circle, and the arm is 50px while the circle is being drawn with radius 25px.

I am still not sure about storing a two-dimensional array for collision detection ... what about simply putting collision detection code, and appropriate response, into the orb class itself?  You can also store the orbs' x and y values in the class.

By the way, if you respond to your original post, it'll pop to the top of the queue again -- no need for reposts.
Page Index Toggle Pages: 1