i've seen the topic example in the reference:
http://processing.org/learning/topics/bouncybubbles.html (or see code at the bottom)
i've accepted, with difficulty, the way it works to calculate collisions between the actual bubble and the others,
but I can't figure out how to create two arrays of bouncing bubbles, and make colliding the arrays each other...
I mean: the way this class accomplish the task is to consider the same class as data, ok.
the array is passed just after it is created.
but if there are two arrays of object from the same class, for example this balls[] and balls2[],
it is not possible to make something like this: (taken from the original code you can see at the bottom)
it's crazy!!! in fact i get the message:
cannot convert from Object to sketch_apr18b.Ball[]
so what is the solution????
you'll make me happy! :)
davide
the whole code is:
i've accepted, with difficulty, the way it works to calculate collisions between the actual bubble and the others,
but I can't figure out how to create two arrays of bouncing bubbles, and make colliding the arrays each other...
I mean: the way this class accomplish the task is to consider the same class as data, ok.
the array is passed just after it is created.
balls[i] = new Ball(random(width), random(height), random(20, 40), i, balls)
it is not possible to make something like this: (taken from the original code you can see at the bottom)
- int numBalls = 7;
float spring = 0.05;
float gravity = 0.03;
float friction = -0.9;
Ball[] balls = new Ball[numBalls];
Ball[] balls2=new Ball[numBalls];
Ball[] balls3=new Ball[numBalls];
void setup()
{
size(640, 200);
noStroke();
smooth();
for (int i = 0; i < numBalls; i++) {
balls[i] = new Ball(random(width), random(height), random(20, 40), i, balls);
balls2[i] = new Ball(random(width), random(height), random(20, 40), i, balls2);
balls3[i] = new Ball(random(width), random(height), random(20, 40), i, balls3);
}
arraycopy(balls,balls3);
balls3 = append(balls3,balls2);
for (int i = 0; i < numBalls; i++) {
balls[i] = new Ball(random(width), random(height), random(20, 40), i, balls3);
balls2[i] = new Ball(random(width), random(height), random(20, 40), i, balls3);
}
}
it's crazy!!! in fact i get the message:
cannot convert from Object to sketch_apr18b.Ball[]
so what is the solution????
you'll make me happy! :)
davide
the whole code is:
int numBalls = 12;
float spring = 0.05;
float gravity = 0.03;
float friction = -0.9;
Ball[] balls = new Ball[numBalls];
void setup()
{
size(640, 200);
noStroke();
smooth();
for (int i = 0; i < numBalls; i++) {
balls[i] = new Ball(random(width), random(height), random(20, 40), i, balls);
}
}
void draw()
{
background(0);
for (int i = 0; i < numBalls; i++) {
balls[i].collide();
balls[i].move();
balls[i].display();
}
}
class Ball {
float x, y;
float diameter;
float vx = 0;
float vy = 0;
int id;
Ball[] others;
Ball(float xin, float yin, float din, int idin, Ball[] oin) {
x = xin;
y = yin;
diameter = din;
id = idin;
others = oin;
}
void collide() {
for (int i = id + 1; i < numBalls; i++) {
float dx = others[i].x - x;
float dy = others[i].y - y;
float distance = sqrt(dx*dx + dy*dy);
float minDist = others[i].diameter/2 + diameter/2;
if (distance < minDist) {
float angle = atan2(dy, dx);
float targetX = x + cos(angle) * minDist;
float targetY = y + sin(angle) * minDist;
float ax = (targetX - others[i].x) * spring;
float ay = (targetY - others[i].y) * spring;
vx -= ax;
vy -= ay;
others[i].vx += ax;
others[i].vy += ay;
}
}
}
void move() {
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() {
fill(255, 204);
ellipse(x, y, diameter, diameter);
}
}
1