|
Author |
Topic: Collision question (Read 506 times) |
|
Hopper
|
Collision question
« on: Oct 3rd, 2004, 12:44am » |
|
I briefly went through this tutorial, and I did the extra challenge at the end where you make 50 balls bounce around from random start positions, and stuff. But I wanted to take it one slight step further and make it so the balls could collide and bounce off of eachother. I came up with the following (flawed) code: Code:// BOUNCE 1.1 Ball[] myBalls; int x; int y; float vx; float vy; int nballs; void setup() { size(200, 200); framerate(25); nballs = 10; myBalls = new Ball[nballs]; // initializing variables for (int i=0; i<nballs; i++) { myBalls[i] = new Ball(); myBalls[i].x = random(0, width); myBalls[i].y = random(0, height); myBalls[i].vx = random(0, 1); // the horizontal component of the velocity myBalls[i].vy = random(0, 1); // the vertical component of the velocity } } void loop() { background(127); for (int i=0; i<nballs; i++) { myBalls[i].run(); } } class Ball { int bid; float x; float y; float vx; float vy; void run() { drawShape(); // updating the position of the ball using velocity x = x + vx; y = y + vy; if (x < 0 || x > width) { vx = -vx; // inverting the horizontal velocity component of the ball whenever it reaches the left or the right of the screen } if (y < 0 || y > height) { vy = -vy; // inverting the vertical velocity component of the ball whenever it reaches the bottom or the top of the screen } //COLLISION DETECTOR <-- This bit right here for (int i=0; i<nballs; i++) { if (x == myBalls[i].x && y == myBalls[i].y) { vx = -vx; vy = -vy; } } } void drawShape() { ellipseMode(CENTER_RADIUS); noStroke(); fill(255); ellipse(x, y, 4, 4); } } |
| Of course it doesn't work because it checks every ball to see if it's in the same position as any ball. So basically since every ball is in the same location as itself, it is constantly changing direction and not going anywhere. Can anyone help with this?
|
|
|
|
fjen
|
Re: Collision question
« Reply #1 on: Oct 3rd, 2004, 1:01am » |
|
hi hopper ... since i solved your problem i leave it to you to find out where to put code and why it works .. here you go: Code: if (x == myBalls[i].x && y == myBalls[i].y && !(this == myBalls[i])) |
| just kiddin' .. you can use "this" to have a class-instance/object refer to itself. so i just check that the ball to which the instance is compared to is not itself .. !(this == myBalls[i]) best, /F
|
|
|
|
TomC
|
Re: Collision question
« Reply #2 on: Oct 3rd, 2004, 1:07am » |
|
on Oct 3rd, 2004, 1:01am, fjen wrote: Code: Please
|
|
|
|
Hopper
|
Re: Collision question
« Reply #3 on: Oct 3rd, 2004, 2:46am » |
|
Ooh, very nifty. Thanks
|
|
|
|
fjen
|
Re: Collision question
« Reply #4 on: Oct 3rd, 2004, 4:17am » |
|
ja, ja, i know .. btw. comparing myballs brings a strange picture to my head.
|
|
|
|
cello
|
Re: Collision question
« Reply #5 on: Oct 3rd, 2004, 5:41am » |
|
On a side note, you won't want to compare if the x/y coordinates are equal, because they basically never will be with floats. Instead, use something like: dist(x-myBalls[i].x, y-myBalls[i].y) < (radius + myBalls[i].radius) That will compare the distance of the two balls is less than their combined radii, that is, if the balls are overlapping. Marcello
|
|
|
|
TomC
|
Re: Collision question
« Reply #6 on: Oct 3rd, 2004, 11:33am » |
|
on Oct 3rd, 2004, 4:17am, fjen wrote:ja, ja, i know .. btw. comparing myballs brings a strange picture to my head. |
| Processing.myBalls[8];
|
|
|
|
Frederik
|
Re: Collision question
« Reply #7 on: Oct 3rd, 2004, 11:34am » |
|
Just my two (euro)cents... You're actually checking every collision twice: if (i) overlaps with (j), (j) will overlap with (i) so there's no need to check this again. But you can't implement it because the collision check is part of the Ball class. What I would do, is make a new class "ballSystem" in which you put an array of Ball e.g. balls[]. The collision-check then moves to this container, in pseudo code: for (int i=0; i<nballs; i++){ for (int j=i+1; j<nbalss; j++){ if collision_between_balls[i]_and_balls[j]? then adjust_balls[i]_and_balls[j] } } This loops through every couple (i,j), avoids the problem of self-reference and halves the time needed for collision checks... Seeya, Frederik BTW: I've made a remix of your code: // BOUNCE 1.1 - WMute remix by Frederik Vanhoutte ballSystem myBalls; int nballs; void setup() { size(200, 200); framerate(25); nballs = 10; myBalls = new ballSystem(0, width,0, height, -1, 1,-1, 1,4.0,nballs); } void loop() { background(127); for (int i=0; i<10; i++) { myBalls.run(); } } class ballSystem{ Ball[] myBalls; int nballs; ballSystem(){ } ballSystem(float lowerX, float upperX, float lowerY, float upperY, float lowerVX, float upperVX, float lowerVY, float upperVY, float rr, int nn){ nballs=nn; myBalls=new Ball[nballs]; for(int i=0;i<nballs;i++){ myBalls[i]=new Ball(random(lowerX,upperX),random(lowerY,upperY),random(lowerVX,upperVX) ,random(lowerVY,upperVY),rr); } } void run(){ for(int i=0;i<nballs;i++){ myBalls[i].run(); } detectAndAdjust(); } void detectAndAdjust(){ for(int i=0;i<nballs;i++){ for(int j=i+1;j<nballs;j++){ if(dist(myBalls[i].x,myBalls[i].y,myBalls[j].x,myBalls[j].y) < (2*myBalls[i].radius)){ myBalls[i].vx*=-1; myBalls[i].vy*=-1; myBalls[j].vx*=-1; myBalls[j].vy*=-1; } } } } } class Ball { int bid; float x; float y; float vx; float vy; float radius; Ball(){ } Ball(float xx, float yy, float vxx, float vyy, float rr){ x=xx; y=yy; vx=vxx; vy=vyy; radius=rr; } void run() { drawShape(); // updating the position of the ball using velocity x = x + vx; y = y + vy; if (x < 0 || x > width) { vx = -vx; // inverting the horizontal velocity component of the ball whenever it reaches the left or the right of the screen } if (y < 0 || y > height) { vy = -vy; // inverting the vertical velocity component of the ball whenever it reaches the bottom or the top of the screen } } void drawShape() { ellipseMode(CENTER_RADIUS); noStroke(); fill(255); ellipse(x, y, radius, radius); } }
|
« Last Edit: Oct 3rd, 2004, 9:57pm by Frederik » |
|
|
|
|
Hopper
|
Re: Collision question
« Reply #9 on: Oct 3rd, 2004, 9:36pm » |
|
Ooh, wow. Thanks to all of you.
|
|
|
|
|