Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 50 additions & 26 deletions GravityBallCode/GravityBallCode.pde
Original file line number Diff line number Diff line change
@@ -1,38 +1,62 @@
//declare variables
float x, y, velX, velY, diam;
int num = 100;
float [] x = new float[num];
float [] y = new float[num];
float [] velX = new float[num];
float [] velY = new float[num];
float [] diam = new float[num];
color [] c = new color[num];
float g = 1;

void setup() {
//set size of canvas
//set size of canvas and background
size(800, 600);
background(0);

//initialize variables
x = width/2;
y = height/2;
diam = 80;
velX = random(-5, 5);
velY = random(-5, 5);
for(int i = 0; i < num; i++) {
c[i] = color(random(128,255),128);
diam[i] = random(20,50);
x[i] = random(diam[i]/2,width-diam[i]/2);
y[i] = random(diam[i]/2,height-diam[i]/2);
velX[i] = -2+round(random(0,1))*random(2, 10); //make sure velocity is never too small or 0
velY[i] = -2+round(random(0,1))*random(2, 10); //make sure velocity is never too small or 0
}
}

void draw() {
//draw background to cover previous frame
background(0);
//draw a translucent rectangle to cover previous frame
fill(0,100);
rect(-1,-1,width+2,height+2);

strokeWeight(5);
stroke(255,0,0);
line(pmouseX,pmouseY,mouseX,mouseY); //draw a streak

noStroke();
for(int i = 0; i < num; i++) {
if(dist(x[i], round(y[i]), mouseX, mouseY) <= diam[i]/2) {
c[i] = color(255,0,0); //color the balls that the mouse touches red
} else {
c[i] = color(random(128,255),128);
}

//draw ball
fill(c[i]);
ellipse(x[i], y[i], diam[i], diam[i]);

//draw ball
ellipse(x, y, diam, diam);

//add velocity to position
x += velX;
y += velY;

//bounce ball if it hits walls
if (x + diam/2 >= width) {
velX = -abs(velX); //if the ball hits the right wall, assign x velocity the negative version of itself
} else if (x - diam/2 <= 0) {
velX = abs(velX); //if the ball hits the left wall, assign x velocity the positive version of itself
}
if (y + diam/2 >= height) {
velY = -abs(velY);
} else if (y - diam/2 <= 0) {
velY = abs(velY);
//add velocity to position
x[i] += velX[i];
y[i] += velY[i];

//bounce ball if it hits walls
if (x[i] + diam[i]/2 >= width || x[i] - diam[i]/2 <= 0) {
velX[i] *= -1; //if the ball hits the side wall, assign x velocity the negative version of itself
}
if (y[i] + diam[i]/2 >= height || y[i] - diam[i]/2 <= 0) {
velY[i] *= -1; //if the ball hits the top or bottom; assign x velocity the negative version of itself
} else {
velY[i] += g;
}
}
}
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,3 @@
* On this branch, modify your code to have 30 of these gravity balls (or some other number in that range). Once again, remember to commit and sync
* Merge this new branch back in to the master branch
* You're done for the day!


##Acceleration
Just as _velocity_ is change in position over time, _acceleration_ is change in _velocity_ over time. Creating acceleration is going to be very similar to creating velocity. I recommend a variable - perhaps call it "gravity"? I also recommend a small value - perhaps somewhere around .1