Bouncing Balls from Echo

Hi all,

One Bouncing Ball: I started with one bouncing ball. I want the ball to drop from the top of the canvas, and gradually stop bouncing. When pressing mouse, the ball will start bouncing again, and the ball’s diameter, color and speed will change.

BoucingBall1_Echo(1080)

float xPos = 250;
float yPos = 0;
float diam = 50;
float ySpeed = 2;

color ballColor=color(random(255),random(ySpeed),random(255));

void setup() {
size(500,400);
noStroke();
smooth();
}

void draw() {
background(0);
ellipse(xPos, yPos, diam, diam);
fill(ballColor);

ySpeed = ySpeed + 0.75;
yPos = yPos + ySpeed;
if (yPos > height – diam) {
yPos = height – diam;
ySpeed = ySpeed * -0.9;
}
if (yPos <= 0) {
ySpeed = -ySpeed;
}
}
void mousePressed() {
ySpeed = random(2, 10);
yPos=0;
xPos=250;
diam=random(30,70);
ballColor=color(random(ySpeed),random(255),random(255));
}

Two Bouncing Balls: Then I tried to code two bouncing balls, the code is based on the previous one and the code from last class. Basically, when the two bouncing balls meet, the background will flash red. Also, when pressing mouse, two balls’ diameters and colors will change.

TwoBoucingBalls_Echo(720)

float xPos = 250;
float yPos = 0;
float diam = 50;
float ySpeed = 2;
float xPos2 = 350;
float yPos2 = 25;
float xSpeed2 = 3;
float ySpeed2 = 4;

color ballColor1=color(random(255),random(ySpeed),random(255));
color ballColor2=color(random(ySpeed),random(255),random(255));

void setup() {
size(500,400);
noStroke();
smooth();
}

void draw() {
background(0);
fill(ballColor1);
ellipse(xPos, yPos, diam, diam);
fill(ballColor2);
ellipse(xPos2, yPos2, diam, diam);

ySpeed = ySpeed + 0.75;
yPos = yPos + ySpeed;

xPos2 = xPos2 + xSpeed2;
yPos2 = yPos2 + ySpeed2;

if (yPos > height – diam) {
yPos = height – diam;
ySpeed = ySpeed * -0.9;
}
if (yPos <= 0) {
ySpeed = -ySpeed;
}

if (xPos2 > width-diam/2 || xPos2 < diam/2) {
xSpeed2 = -xSpeed2;

}
if (yPos2 > height-diam/2 || yPos2 < diam/2) {
ySpeed2 = -ySpeed2;
}

if(dist(xPos, yPos, xPos2, yPos2)<=diam){
ySpeed=ySpeed-2*ySpeed;
xSpeed2=xSpeed2-2*xSpeed2;
ySpeed2=ySpeed2-2*ySpeed2;
}

if(dist(xPos, yPos, xPos2, yPos2)<=diam){
background(#F70505);
}
}

void mousePressed(){
ySpeed = random(2, 10);
xPos=250;
yPos=0;
xSpeed2=2;
ySpeed2=4;
xPos2=350;
yPos2=25;
diam=random(30,50);
ballColor1=color(random(255),random(ySpeed),random(255));
ballColor2=color(random(ySpeed),random(255),random(255));
}

Two Crazy Bouncing Balls: When I coded the two bouncing balls, I accidentally messed up the code a little bit. I got a crazy version of bouncing balls. When two balls hit each time, one of the balls will speed up and finally goes crazy. Here are my crazy balls:

TwoBouncingBall2Crazy_Echo

float xPos = 250;
float yPos = 0;
float diam = 50;
float ySpeed = 2;
float xPos2 = 350;
float yPos2 = 25;
float xSpeed2 = 3;
float ySpeed2 = 4;

color ballColor1=color(random(255),random(ySpeed),random(255));
color ballColor2=color(random(ySpeed),random(255),random(255));

void setup() {
size(500,400);
noStroke();
smooth();
}

void draw() {
background(0);
fill(ballColor1);
ellipse(xPos, yPos, diam, diam);
fill(ballColor2);
ellipse(xPos2, yPos2, diam, diam);

ySpeed = ySpeed + 0.75;
yPos = yPos + ySpeed;

xPos2 = xPos2 + xSpeed2;
yPos2 = yPos2 + ySpeed2;

if (yPos > height – diam) {
yPos = height – diam;
ySpeed = ySpeed * -0.9;
}
if (yPos <= 0) {
ySpeed = -ySpeed;
}

if (xPos2 > width-diam/2 || xPos2 < diam/2) {
xSpeed2 = -xSpeed2;

}
if (yPos2 > height-diam/2 || yPos2 < diam/2) {
ySpeed2 = -ySpeed2;
}

if(dist(xPos, yPos, xPos2, yPos2)<=diam){
ySpeed=ySpeed-2*ySpeed;
xSpeed2=xSpeed2-2*xSpeed2;
ySpeed2=ySpeed2-2*ySpeed; // I accidentally missed the “2,” That’s why it goes crazy!!!
}

if(dist(xPos, yPos, xPos2, yPos2)<=diam){
background(#F70505);
}
}

void mousePressed(){
ySpeed = random(2, 10);
xPos=250;
yPos=0;
xSpeed2=2;
ySpeed2=4;
xPos2=350;
yPos2=25;
diam=random(30,50);
ballColor1=color(random(255),random(ySpeed),random(255));
ballColor2=color(random(ySpeed),random(255),random(255));
}

 

Comments are closed.