FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Programs
(Moderators: fry, REAS)
   beginner: collision detection
« No topic | Next topic »

Pages: 1 
   Author  Topic: beginner: collision detection  (Read 8576 times)
roland_g


beginner: collision detection
« on: Apr 17th, 2005, 10:35pm »

so I made this little app some time ago that let's a bunch of balls bounce off the edges and let's them change the size once they do hit the edge.
 
This is the first time I've used a class and it seems to be working, but sometimes a ball gets stuck and I don't quite know why.
 
If anybody could tell me how to optimize the code I'd welcome it.
 
Now the next thing I want to do is make the balls bounce off of each other, but I haven't yet found anything that would help on my way to getting there.
 
Could anybody point me to a link that explains basic object collision or give me some tips?
 
thanks
 
Roland
Code:

Ball[] ball;
int objectnumber = 20;
 
void setup (){
 
  size (500,500);
  framerate(30);
  ball = new Ball[objectnumber];
 
  for (int i= 0; i < objectnumber; i++){
    ball[i] = new Ball(#940B35,random(50,500),random(10,400),random(1,7),random(1,5), 10, 20);
  }
 
}
 
void loop (){
  background(#280915);
 
  for (int i = 0; i < objectnumber; i++){
    ball[i].draw ();
    ball[i].move ();
  }
}
 
class Ball {
 
 
  float size= 10;
  float xpos;
  float ypos;
  float xspeed;
  float yspeed;
  float radius;
  float radius2;
  color c;
  int xdirection = 1;
  int ydirection = 1;
 
  Ball (color c_,float xp, float yp,float xs, float ys, float r, float r2){
    c = c_;
    xpos = xp;
    ypos = yp;
    xspeed = xs;
    yspeed = ys;
    radius = r;
    radius2 = r2;
 
  }
 
  void draw(){
    ellipseMode(CENTER_DIAMETER);
    noStroke();
    fill (c);
    smooth();
    ellipse(xpos,ypos,radius,radius);
    if (radius > 10){
    radius--;}
}
 
  void move(){
    xpos = xpos +(xspeed * xdirection);
    ypos = ypos +(yspeed * ydirection);
    if (xpos > width-radius2/2 || xpos < radius2/2){
 xdirection *=-1;
 radius = radius2;    
    }
    if (ypos > height-radius2/2 || ypos <radius2/2){
 ydirection *=-1;
 radius = radius2;
    }
  }
}
 
mflux

fluxhavoc WWW
Re: beginner: collision detection
« Reply #1 on: Apr 18th, 2005, 3:28am »

where does it get stuck? I haven't seen it get stuck yet.
 
Nothing to optimize here really, besides silly things like *.5 instead of /2. I don't think that'll speed it up noticibly here, though.  
 
As far as ball to ball collision, simply make a function (call it "collide()" or something) inside the ball class, then have it loop through all the other instances of that class (in your case.. Ball[]). Have it test each ball's distance against itself, minus their radius.  
 
If they are within radius-length of each other, swap their velocities (direction of motion).
 
Fish-face

308232952308232952thedemonsheep WWW Email
Re: beginner: collision detection
« Reply #2 on: Apr 18th, 2005, 5:02pm »

mflux's collision method is slightly inaccurate; it only works for head on collisions. If two balls 'glance' each other then their momentum is shared between them according to the angle at which they strike. To do collision checking simply:
 
Step 1: Iterate through Ball classes, check if this one is touching/covering another one
Step 2: Move the two balls apart so that they are edge to edge
Step 3: Work out the momentum to give to each ball. This can be worked out thus:
 
Each ball's momentum is comprised of two separate, equal momentums each at 45 degrees to its direction of motion. If the balls collide head on (collision angle of 0 degrees) then all of both momentums is given to the other. If the balls are of equal masses and velocities, this causes them both to halt.
If they collide at 45 degrees, then all of one momentum is given, but the momentum that is in a direction perpendicular to the angle of collision is retained. This causes the balls to glance of each other, at (again, only if equal masses + velocities, opposite directions) 45 degree angles compared to their original  directions.
At a collision angle of 90 degrees, there has not actually been a collision, so all momentum is retained.
 
This is difficult to explain, but you can have great fun sliding coins across the table to work stuff out.
 

--

The C@ S@ on the M@
=Fish-Face=
~#Chris#~
Pages: 1 

« No topic | Next topic »