Bouncing Balls
in
Programming Questions
•
1 year ago
Hi, I,ve made an sketch with bouncing balls, but there is a kind of strange phenomena when the balls are too tight. I've tried diferent solutions but they doesn´t work. I haven´t found examples of codes where the ball bounce between them
This is the source code:
ArrayList bolas;
int size=700;
int i;
int j;
float acum;
class Bola{
float x;
float y;
float boladiam;
float velx;
float vely;
int r;
int g;
int b;
//auxiliar para cambiar valores
Bola(int xini, int yini,float diamini,int rini,int gini, int bini){
x=xini;
y=yini;
boladiam=40;
velx=1;
vely=0;
boladiam=diamini;
r=rini;
g=gini;
b=bini;
}
}
int size=700;
int i;
int j;
float acum;
class Bola{
float x;
float y;
float boladiam;
float velx;
float vely;
int r;
int g;
int b;
//auxiliar para cambiar valores
Bola(int xini, int yini,float diamini,int rini,int gini, int bini){
x=xini;
y=yini;
boladiam=40;
velx=1;
vely=0;
boladiam=diamini;
r=rini;
g=gini;
b=bini;
}
}
void setup(){
noStroke();
size(size,size);
bolas=new ArrayList();
}
void draw(){
frameRate(1000);
background(255);
for(i=0;i < bolas.size();i++){
Bola bola = (Bola) bolas.get(i);
fill(bola.r,bola.g,bola.b,200);
ellipse(bola.x,bola.y,bola.boladiam,bola.boladiam);//dibuja la bola
bola.vely=bola.vely + 0.1;//acelera
bola.x=bola.x + bola.velx;//mueve en x
bola.y=bola.y + bola.vely;//mueve en y
if (bola.y > size-bola.boladiam/2){//rebote verical
bola.vely=-bola.vely;
}
if (bola.y+bola.boladiam/2-size>0.15){
bola.y=size-bola.boladiam/2;
if (abs(bola.vely)<0.1){
bola.vely*=1.25;
}
}
if ((bola.x>size-bola.boladiam/2)||(bola.x<bola.boladiam/2)){//rebote horizontal
bola.velx=-bola.velx;
}
for(j=i+1;j < bolas.size();j++){//rebote entre ellas
Bola otrabola = (Bola) bolas.get(j);
if((abs(bola.x-otrabola.x)<(bola.boladiam+otrabola.boladiam)/2)&&(abs(bola.y-otrabola.y)<(bola.boladiam+otrabola.boladiam)/2)){
acum=bola.velx;
bola.velx=otrabola.velx;
otrabola.velx=acum;
acum=bola.vely;
bola.vely=otrabola.vely;
otrabola.vely=acum;
}
}
}
}
frameRate(1000);
background(255);
for(i=0;i < bolas.size();i++){
Bola bola = (Bola) bolas.get(i);
fill(bola.r,bola.g,bola.b,200);
ellipse(bola.x,bola.y,bola.boladiam,bola.boladiam);//dibuja la bola
bola.vely=bola.vely + 0.1;//acelera
bola.x=bola.x + bola.velx;//mueve en x
bola.y=bola.y + bola.vely;//mueve en y
if (bola.y > size-bola.boladiam/2){//rebote verical
bola.vely=-bola.vely;
}
if (bola.y+bola.boladiam/2-size>0.15){
bola.y=size-bola.boladiam/2;
if (abs(bola.vely)<0.1){
bola.vely*=1.25;
}
}
if ((bola.x>size-bola.boladiam/2)||(bola.x<bola.boladiam/2)){//rebote horizontal
bola.velx=-bola.velx;
}
for(j=i+1;j < bolas.size();j++){//rebote entre ellas
Bola otrabola = (Bola) bolas.get(j);
if((abs(bola.x-otrabola.x)<(bola.boladiam+otrabola.boladiam)/2)&&(abs(bola.y-otrabola.y)<(bola.boladiam+otrabola.boladiam)/2)){
acum=bola.velx;
bola.velx=otrabola.velx;
otrabola.velx=acum;
acum=bola.vely;
bola.vely=otrabola.vely;
otrabola.vely=acum;
}
}
}
}
void mousePressed() {
bolas.add ( new Bola(mouseX,mouseY,10+mouseX/10,int(random(100,255)),int(random(0,255)),int(random(0,255))));
}
bolas.add ( new Bola(mouseX,mouseY,10+mouseX/10,int(random(100,255)),int(random(0,255)),int(random(0,255))));
}
1