From 564ce98b4ea4da4f874fd4f018c953c54a78c073 Mon Sep 17 00:00:00 2001 From: Sconnda Date: Fri, 13 Nov 2015 12:56:26 -0500 Subject: [PATCH 1/5] Added vectors --- BouncingWithVectors/BouncingWithVectors.pde | 27 +++++++++------------ 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 2683b50..1bf8b4c 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,16 +1,15 @@ //declare variables -float x, y, velX, velY, diam; +float diam; +PVector loc, vel; void setup() { //set size of canvas size(800, 600); //initialize variables - x = width/2; - y = height/2; diam = 80; - velX = random(-5, 5); - velY = random(-5, 5); + loc = new PVector(width/2, height/2); + vel = new PVector(random(-5,5), random(-5,5)); } void draw() { @@ -18,20 +17,16 @@ void draw() { background(0); //draw ball - ellipse(x, y, diam, diam); + ellipse(loc.x, loc.y, diam, diam); //add velocity to position - x += velX; - y += velY; + loc.add(vel); //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 (loc.x + diam/2 >= width || loc.x - diam/2 <= 0) { + vel.x *= -1; //if the ball hits the side, assign the x velocity the opposite version } - if (y + diam/2 >= height) { - velY = -abs(velY); - } else if (y - diam/2 <= 0) { - velY = abs(velY); + if (loc.y + diam/2 >= height || loc.y - diam/2 <= 0) { + vel.y *= -1; } +} \ No newline at end of file From 47e8b01d18aaff13c8141b38b6dbee1c0c469aad Mon Sep 17 00:00:00 2001 From: Sconnda Date: Fri, 13 Nov 2015 13:17:58 -0500 Subject: [PATCH 2/5] Add 3D --- BouncingWithVectors/BouncingWithVectors.pde | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 1bf8b4c..dcce157 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -4,12 +4,12 @@ PVector loc, vel; void setup() { //set size of canvas - size(800, 600); + size(800, 600, P3D); //initialize variables diam = 80; - loc = new PVector(width/2, height/2); - vel = new PVector(random(-5,5), random(-5,5)); + loc = new PVector(width/2, height/2, 0); + vel = new PVector(random(-10,10), random(-10,10), 0); } void draw() { @@ -17,7 +17,10 @@ void draw() { background(0); //draw ball - ellipse(loc.x, loc.y, diam, diam); + pushMatrix(); + translate(loc.x,loc.y,loc.z); + sphere(diam); + popMatrix(); //add velocity to position loc.add(vel); @@ -29,4 +32,9 @@ void draw() { if (loc.y + diam/2 >= height || loc.y - diam/2 <= 0) { vel.y *= -1; } + if (loc.z - diam/2 <= -sqrt(sq(width)+sq(height))/2) { + vel.z *= -1; + } else { + vel.z -= 1; + } } \ No newline at end of file From 934caa04e506ebfabf292fe7fc091c825737ddd2 Mon Sep 17 00:00:00 2001 From: Sconnda Date: Tue, 17 Nov 2015 12:26:58 -0500 Subject: [PATCH 3/5] Made multiple balls possible --- BouncingWithVectors/BouncingWithVectors.pde | 58 +++++++++++++-------- 1 file changed, 37 insertions(+), 21 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index dcce157..1151ba2 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,40 +1,56 @@ //declare variables -float diam; -PVector loc, vel; +int num = 15; +float [] diam = new float[num]; +PVector [] loc = new PVector[num]; +PVector [] vel = new PVector[num]; void setup() { //set size of canvas size(800, 600, P3D); //initialize variables - diam = 80; - loc = new PVector(width/2, height/2, 0); - vel = new PVector(random(-10,10), random(-10,10), 0); + for(int i = 0; i < num; i++) { + diam[i] = 80; + loc[i] = new PVector(width/2, height/2, 0); + vel[i] = PVector.random3D(); + vel[i].mult(5); + } } void draw() { //draw background to cover previous frame background(0); - //draw ball + //draw box pushMatrix(); - translate(loc.x,loc.y,loc.z); - sphere(diam); + translate(width/2,height/2); + noFill(); + stroke(255); + box(width); popMatrix(); + + for(int i = 0; i < num; i++) { + //draw ball + pushMatrix(); + stroke(255,0,0); + translate(loc[i].x,loc[i].y,loc[i].z); + sphere(diam[i]); + popMatrix(); - //add velocity to position - loc.add(vel); + //add velocity to position + loc[i].add(vel[i]); - //bounce ball if it hits walls - if (loc.x + diam/2 >= width || loc.x - diam/2 <= 0) { - vel.x *= -1; //if the ball hits the side, assign the x velocity the opposite version - } - if (loc.y + diam/2 >= height || loc.y - diam/2 <= 0) { - vel.y *= -1; - } - if (loc.z - diam/2 <= -sqrt(sq(width)+sq(height))/2) { - vel.z *= -1; - } else { - vel.z -= 1; + //bounce ball if it hits walls + if (loc[i].x + diam[i]/2 >= width || loc[i].x - diam[i]/2 <= 0) { + vel[i].x *= -1; //if the ball hits the side, assign the x velocity the opposite version + } + if (loc[i].y + diam[i]/2 >= height || loc[i].y - diam[i]/2 <= 0) { + vel[i].y *= -1; + } + if (loc[i].z - diam[i]/2 <= -sqrt(sq(width)+sq(height))/2) { + vel[i].z *= -1; + } else { + vel[i].z -= 1; + } } } \ No newline at end of file From 266e8453cce14567c51b2dfb60869dcbf0a7fc37 Mon Sep 17 00:00:00 2001 From: Sconnda Date: Tue, 17 Nov 2015 12:40:13 -0500 Subject: [PATCH 4/5] Changed values to vector --- Wanderer/Wanderer.pde | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index b6ce247..fbc26bc 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,16 +1,15 @@ //declare variables -float x, y, velX, velY, diam; +PVector loc; +PVector vel = PVector.random2D(); +int diam = 80; void setup() { //set size of canvas size(800, 600); - //initialize variables - x = width/2; - y = height/2; - diam = 80; - velX = random(-5, 5); - velY = random(-5, 5); + //initialize location + loc = new PVector(width/2, height/2); + vel.mult(10); } void draw() { @@ -18,20 +17,21 @@ void draw() { background(0); //draw ball - ellipse(x, y, diam, diam); + ellipse(loc.x, loc.y, diam, diam); //add velocity to position - x += velX; - y += velY; + loc.x += vel.x; + loc.y += vel.y; //wrap the ball's position - if (x + diam/2 >= width) { - x = -diam/2; - } else if (x - diam/2 <= 0) { - x = width + diam/2; + if (loc.x > width) { + loc.x = 0; + } else if (loc.x < 0) { + loc.x = width; } - if (y + diam/2 >= height) { - y = -diam/2; - } else if (y - diam/2 <= 0) { - y = height + diam/2; + if (loc.y > height) { + loc.y = 0; + } else if (loc.y < 0) { + loc.y = height; } +} \ No newline at end of file From 88a906702048678b40987cd5e5da986bd4897d5f Mon Sep 17 00:00:00 2001 From: Sconnda Date: Tue, 17 Nov 2015 13:25:43 -0500 Subject: [PATCH 5/5] Added acceleration and ripples --- Wanderer/Wanderer.pde | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index fbc26bc..09b3242 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,7 +1,9 @@ //declare variables PVector loc; PVector vel = PVector.random2D(); +PVector acc; int diam = 80; +PVector [] ripples = new PVector[100]; void setup() { //set size of canvas @@ -9,19 +11,43 @@ void setup() { //initialize location loc = new PVector(width/2, height/2); - vel.mult(10); + + //assign initial ripples + for (int i = 0; i < 100; i++) { + ripples[i] = new PVector(0, 0); + } } void draw() { //draw background to cover previous frame - background(0); + background(128,196,255); + + PVector acc = PVector.random2D(); + acc.mult(0.1); //draw ball ellipse(loc.x, loc.y, diam, diam); //add velocity to position - loc.x += vel.x; - loc.y += vel.y; + loc.add(vel); + + //add acceleration to velocity + vel.add(acc); + vel.limit(100); + + //reassign ripple values + for (int i = 99; i > 0; i--) { + ripples[i] = ripples[i-1]; + } + + //draw ripples + for (int i = 0; i < 50; i++) { + ripples[0] = loc; + noFill(); + strokeWeight(2); + stroke(0,255-25*i); + ellipse(ripples[i].x, ripples[i].y, diam+100*i, diam+100*i); + } //wrap the ball's position if (loc.x > width) {