Skip to content

Instantly share code, notes, and snippets.

@EmreNtm
Created March 10, 2019 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EmreNtm/f369452ca16ebd6a9facc0bae84ef0dc to your computer and use it in GitHub Desktop.
Save EmreNtm/f369452ca16ebd6a9facc0bae84ef0dc to your computer and use it in GitHub Desktop.
Bouncing Balls
class Ball {
float r;
float vx;
float vy;
float xpos;
float ypos;
float m;
int clrMode;
int clr;
int red;
int green;
int blue;
boolean gravity;
float g;
Ball(float r, float xpos, float ypos){
this.xpos = xpos;
this.ypos = ypos;
this.r = r;
vx = 0;
vy = 0;
m = 5;
clr = 255;
red = 0;
green = 0;
blue = 0;
clrMode = 0;
gravity = false;
g = 0;
}
void display(){
ellipse(xpos, ypos, 2*r, 2*r);
}
void update(){
if(xpos >= width - r || xpos <= r){
vx = -vx;
}
if(ypos >= height - r || ypos <= r){
vy = -vy - g;
}
if(gravity){
float tmp = vy;
vy += g;
if(tmp * vy < 0){
vy = 0;
}
}
xpos += vx;
if(xpos > width - r){
xpos = width - r;
} else if (xpos < r){
xpos = r;
}
ypos += vy;
if(ypos > height - r){
ypos = height - r;
} else if (ypos < r){
ypos = r;
}
getColor();
}
void setColor(int clr){
this.clr = clr;
this.clrMode = 0;
}
void setColor(int red, int green, int blue){
this.red = red;
this.green = green;
this.blue = blue;
this.clrMode = 1;
}
void getColor(){
if(this.clrMode == 0){
fill(clr);
} else {
fill(red, green, blue);
}
}
float getVelX(){
return vx;
}
float getVelY(){
return vy;
}
float getPosX(){
return xpos;
}
float getPosY(){
return ypos;
}
void setPosX(float p){
this.xpos = p;
}
void setPosY(float p){
this.xpos = p;
}
float getRadius(){
return r;
}
void setGravity(boolean c){
gravity = c;
}
void setGravity(boolean c, float g){
gravity = c;
this.g = g;
}
void setSpeed(float vx, float vy){
this.vx = vx;
this.vy = vy;
}
void setMass(float m){
this.m = m;
}
float getMass(){
return m;
}
}
Ball[] balls;
int n;
float k;
float g;
void setup(){
size(720, 720);
background(125);
//noStroke();
n = 5;
k = 1;
g = 0.3;
balls = new Ball[n];
for(int i = 0; i < n; i++){
balls[i] = new Ball(10 + random(40), random(width*3/4) + 50, random(height*3/4) + 50);
balls[i].setGravity(true, g);
balls[i].setSpeed(k*(random(10) - 5), k*(random(10) - 5));
balls[i].setColor((int)random(255), (int)random(255), (int)random(255));
//balls[i].setColor((int)random(255));
balls[i].setMass(balls[i].getRadius() / 5);
}
}
void draw(){
background(125);
for(Ball ball : balls){
if(n > 1){
checkCrash(ball);
}
ball.update();
ball.display();
}
}
void checkCrash(Ball a){
for(Ball b : balls){
if(b != a){
if(sqrt(sq(a.getPosX() - b.getPosX()) + sq(a.getPosY() - b.getPosY())) <= a.getRadius() + b.getRadius()){
calculateCrash(a, b);
}
}
}
}
void calculateCrash(Ball a, Ball b){
float vxa, vya, vxb, vyb, vxa1, vya1, vxb1, vyb1;
vxa = (a.getMass() - b.getMass()) / (a.getMass() + b.getMass()) * a.getVelX();
vxa1 = 2*b.getMass() / (a.getMass() + b.getMass()) * b.getVelX();
vxa += vxa1;
vxb = 2*b.getMass() / (a.getMass() + b.getMass()) * a.getVelX();
vxb1 = (b.getMass() - a.getMass()) / (a.getMass() + b.getMass()) * b.getVelX();
vxb += vxb1;
vya = (a.getMass() - b.getMass()) / (a.getMass() + b.getMass()) * a.getVelY();
vya1 = 2*b.getMass() / (a.getMass() + b.getMass()) * b.getVelY();
vya += vya1;
vyb = 2*b.getMass() / (a.getMass() + b.getMass()) * a.getVelY();
vyb1 = (b.getMass() - a.getMass()) / (a.getMass() + b.getMass()) * b.getVelY();
vyb += vyb1;
a.setSpeed(vxa, vya);
b.setSpeed(vxb, vyb);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment