From ed6427450e8fda43dfdd302f74eece60ab011e4f Mon Sep 17 00:00:00 2001 From: nafahmed Date: Mon, 9 Nov 2015 13:14:34 -0500 Subject: [PATCH 1/5] created one gravity bouncing ball --- GravityBallCode/GravityBallCode.pde | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index df2493b..a7c7337 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -1,5 +1,5 @@ //declare variables -float x, y, velX, velY, diam; +float x, y, velX, velY, diam, a; void setup() { //set size of canvas @@ -7,32 +7,29 @@ void setup() { //initialize variables x = width/2; - y = height/2; + y = 0; diam = 80; - velX = random(-5, 5); - velY = random(-5, 5); +a=1; + } void draw() { //draw background to cover previous frame background(0); + velY = a+velY; + //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) { + + if (y >= height) { + y=height; velY = -abs(velY); - } else if (y - diam/2 <= 0) { - velY = abs(velY); - } + } } \ No newline at end of file From c57f302cc98257d8180ce0ceb9e10a7d46865150 Mon Sep 17 00:00:00 2001 From: nafahmed Date: Mon, 9 Nov 2015 13:24:36 -0500 Subject: [PATCH 2/5] add if loop, started arrays --- GravityBallCode/GravityBallCode.pde | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index a7c7337..403ad60 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -1,15 +1,20 @@ //declare variables -float x, y, velX, velY, diam, a; +float []x= new float [30]; +float []y= new float [30]; +float []velX= new float [30]; +float []velY= new float [30]; +float []diam= new float [30]; +float []a= new float [30]; void setup() { //set size of canvas size(800, 600); //initialize variables - x = width/2; - y = 0; - diam = 80; -a=1; +int i=0; +for(i=0; i<30; i++){ + +} } @@ -20,7 +25,7 @@ void draw() { velY = a+velY; //draw ball - ellipse(x, y, diam, diam); + ellipse(x[30], y[30], diam[30], diam[30]); //add velocity to position From d109eeb6bc76cbdae4ed90aac58eb41cf5f15f93 Mon Sep 17 00:00:00 2001 From: nafahmed Date: Tue, 10 Nov 2015 12:39:44 -0500 Subject: [PATCH 3/5] finished arrays, added while statements --- GravityBallCode/GravityBallCode.pde | 56 ++++++++++++++++------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index 403ad60..9804ba1 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -1,10 +1,10 @@ //declare variables -float []x= new float [30]; -float []y= new float [30]; -float []velX= new float [30]; -float []velY= new float [30]; -float []diam= new float [30]; -float []a= new float [30]; +int count = 30; +float [] x= new float [count]; +float [] y= new float [count]; +float [] velY= new float [count]; +float [] diam= new float [count]; +float [] a= new float [count]; void setup() { //set size of canvas @@ -12,29 +12,35 @@ void setup() { //initialize variables int i=0; -for(i=0; i<30; i++){ - -} + + while(i= height) { - y=height; - velY = -abs(velY); - } +if(y[i]>=height){ + velY[i]=-abs(velY[i]); +y[i]=height; +} +i++; +} } \ No newline at end of file From 4a47172e39ac3c1fc951156857c561beb49b0464 Mon Sep 17 00:00:00 2001 From: nafahmed Date: Tue, 10 Nov 2015 13:04:27 -0500 Subject: [PATCH 4/5] balls increase diameter when mouse touches them --- GravityBallCode/GravityBallCode.pde | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index 9804ba1..1078cce 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -32,7 +32,9 @@ void draw() { while(i Date: Tue, 10 Nov 2015 13:10:43 -0500 Subject: [PATCH 5/5] annotated code --- GravityBallCode/GravityBallCode.pde | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index 1078cce..fb9ef0d 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -8,40 +8,42 @@ float [] a= new float [count]; void setup() { //set size of canvas - size(800, 600); + size(800, 600); //set size //initialize variables -int i=0; +int i=0; //set local variable while(i=height){ velY[i]=-abs(velY[i]); -y[i]=height; +y[i]=height; //change y direction when it hits the bottom } i++; }