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 & HelpSyntax Questions › Bouncing Bubbles sketch - Missing comments
Page Index Toggle Pages: 1
Bouncing Bubbles sketch - Missing comments (Read 1361 times)
Bouncing Bubbles sketch - Missing comments
Aug 12th, 2009, 6:49am
 
Trying to fill in the missing comments on this sketch..
Here is what i have so far:

Code:
int numBalls = 25;			 //total number of balls
float spring = .1; //how springy something is?
float gravity = 1; //gravity
float friction = -0.1; //friction between balls?
Ball[] balls = new Ball[numBalls]; //an array of Ball called balls set to the number of balls in length?

void setup()
{
size(1000, 700);
noStroke();
smooth();
for (int i = 0; i < numBalls; i++) { //cycle thru the array of balls
balls[i] = new Ball(random(width), random(height), random(50, 250), i, balls); //construct each one.. what are the last two parameters?
}
}

void draw()
{
background(50);
for (int i = 0; i < numBalls; i++) {
balls[i].collide();
balls[i].move();
balls[i].display(); //call balls functions
}
}

class Ball {
float x, y; //variables for x,y
float diameter; //variable for diameter
float vx = 0; //velocity in x direction?
float vy = 0; //velocity in y direction?
int id; //which ball?
Ball[] others; //another array of Ball called others

Ball(float xin, float yin, float din, int idin, Ball[] oin) { //arguments for Ball
x = xin;
y = yin;
diameter = din;
id = idin;
others = oin;
}

void collide() {
for (int i = id + 1; i < numBalls; i++) { //scroll thru every ball in the array setting its position up one?
float dx = others[i].x - x; //distance to the other ball in x direction?
float dy = others[i].y - y; //distance to the other ball in y direction?
float distance = sqrt(dx*dx + dy*dy);
float minDist = others[i].diameter/2 + diameter/2; //min distance = distance between other ball radius plus radius?
if (distance < minDist) { //if the distance is less than the minimum distance?
float angle = atan2(dy, dx); //calculate angle of?
float targetX = x + cos(angle) * minDist;
float targetY = y + sin(angle) * minDist;
float ax = (targetX - others[i].x) * spring; //i dont have clue
float ay = (targetY - others[i].y) * spring;
vx -= ax;
vy -= ay;
others[i].vx += ax;
others[i].vy += ay; //still no clue
}
}
}

void move() { //no clue
vy += gravity;
x += vx;
y += vy;
if (x + diameter/2 > width) {
x = width - diameter/2;
vx *= friction;
}
else if (x - diameter/2 < 0) {
x = diameter/2;
vx *= friction;
}
if (y + diameter/2 > height) {
y = height - diameter/2;
vy *= friction;
}
else if (y - diameter/2 < 0) {
y = diameter/2;
vy *= friction;
}
}

void display() { //draw ball
fill(0,200,50, 204);
stroke(0);
strokeWeight(5);
ellipse(x, y, diameter, diameter);
}
}
Re: Bouncing Bubbles sketch - Missing comments
Reply #1 - Aug 12th, 2009, 2:04pm
 
smells like homework so only pointers:

http://www3.ntu.edu.sg/home/ehchua/programming/java/J8a_GameIntro-BouncingBalls.html

http://stackoverflow.com/questions/345838/ball-to-ball-collision-detection-and-handling

the 4 walls are at x = 0 and y = 0 and x = width and y = height

generally look at what the program does and then try and map the behaviour onto the code.
Re: Bouncing Bubbles sketch - Missing comments
Reply #2 - Aug 13th, 2009, 6:27am
 
koogy wrote on Aug 12th, 2009, 2:04pm:
smells like homework so only pointers:

generally look at what the program does and then try and map the behaviour onto the code.



Well its homework that i have given myself - its the only way for me to keep at it and learn this stuff - Im actually a 31 yo  CAD industrial designer.

 Thanks for pointing me to that info on bouncing. Cool
Re: Bouncing Bubbles sketch - Missing comments
Reply #3 - Aug 13th, 2009, 1:23pm
 
ah, sorry, you never can tell. (and to some of us 31 is still quite young 8)

it was actually quite involved for a small program. there's gravity in there (which means things'll accelerate downwards) and elastic bouncing (which means things will slow down after collisions, the spring variable). and bouncing off the walls. but that's all in the links.

this bit:
for (int i = id + 1; i < numBalls; i++)
is probably the subtlest bit, it just means that the balls only compare their positions (id) with the other balls left in the list. so the first one will check against all the others, the second will check against the 3rd and 4th and 5th and... but not the 1st - that's already been checked (as the distance between ball 2 and 1 is the same as the distance between 1 and 2 which you've already done. and he starts at id + 1 because there's no point checking a ball against itself.

and this:
if (distance < minDist)
is the check for collisions, two balls have collided if the distance between them is less than the sum of the two radii. that block works out the bounce angles and changes the velocities of both balls.
Re: Bouncing Bubbles sketch - Missing comments
Reply #4 - Aug 13th, 2009, 1:29pm
 
and having run that now, it's kinda chaotic and hard to see what it's doing. it's easier to see if you reduce the size of the bubbles.
Page Index Toggle Pages: 1