Problem with object collision
in
Programming Questions
•
1 year ago
Hello,
I am new to Processing and am having some trouble with a project I am working on. What I am trying to do is make it so that when the circles (following the mouse) comes in contact with one of the moving circles, they all stop moving. Unfortunately I know next to oohing about collision and am having a tough time. Here is the code I have right now, if anyone could give me some pointers, or an example or something, I would really appreciate it.
float x = 100;
float y = 100;
float xspeed = 1;
float yspeed = 3.3;
float x1 = 200;
float y1 = 200;
float xspeed1 = 4;
float yspeed1 = 7.3;
float x2 = 300;
float y2 = 400;
float xspeed2 = 4;
float yspeed2 = 4.3;
float x3 = 30;
float y3 = 40;
float xspeed3 = 5;
float yspeed3 = 4.3;
void setup() {
size(800,800);
smooth();
background(255);
}
void draw() {
noStroke();
fill(255,10);
rect(0,0,width,height);
background(0,0,0);
fill(255,0,0);
ellipse(mouseX,mouseY,50,50);
fill(255);
// Add the current speed to the location.
x = x + xspeed;
y = y + yspeed;
// Check for bouncing
if ((x > width) || (x < 0)) {
xspeed = xspeed * -1;
}
if ((y > height) || (y < 0)) {
yspeed = yspeed * -1;
}
// Display at x,y location
stroke(0);
fill(175);
ellipse(x,y,16,16);
x1 = x1 + xspeed1;
y1 = y1 + yspeed1;
// Check for bouncing
if ((x1 > width) || (x1 < 0)) {
xspeed1 = xspeed1 * -1;
}
if ((y1 > height) || (y1 < 0)) {
yspeed1 = yspeed1 * -1;
}
// Display at x,y location
stroke(0);
fill(175);
ellipse(x1,y1,16,16);
x2 = x2 + xspeed2;
y2 = y2 + yspeed2;
// Check for bouncing
if ((x2 > width) || (x2 < 0)) {
xspeed2 = xspeed2 * -1;
}
if ((y2 > height) || (y2 < 0)) {
yspeed2 = yspeed2 * -1;
}
// Display at x,y location
stroke(0);
fill(175);
ellipse(x2,y2,16,16);
x3 = x3 + xspeed3;
y3 = y3 + yspeed3;
// Check for bouncing
if ((x3 > width) || (x3 < 0)) {
xspeed3 = xspeed3 * -1;
}
if ((y3 > height) || (y3 < 0)) {
yspeed3 = yspeed3 * -1;
}
// Display at x,y location
stroke(0);
fill(175);
ellipse(x3,y3,16,16);
}
1