From 6e58223c955780639baa500d4f2d473791443f63 Mon Sep 17 00:00:00 2001 From: Daniel Shiffman Date: Mon, 18 Mar 2013 15:39:47 -0400 Subject: [PATCH] comments for reflection1 example --- .../Topics/Motion/Reflection1/Reflection1.pde | 40 +++++++++---------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/java/examples/Topics/Motion/Reflection1/Reflection1.pde b/java/examples/Topics/Motion/Reflection1/Reflection1.pde index 730e0ed0b..a6bc6073b 100644 --- a/java/examples/Topics/Motion/Reflection1/Reflection1.pde +++ b/java/examples/Topics/Motion/Reflection1/Reflection1.pde @@ -7,17 +7,21 @@ * vector. */ +// Position of left hand side of floor PVector base1; +// Position of right hand side of floor PVector base2; +// Length of floor float baseLength; +// An array of subpoints along the floor path PVector[] coords; +// Variables related to moving ball PVector position; -float r = 6; -PVector direction; -float speed = 3.5; PVector velocity; +float r = 6; +float speed = 3.5; void setup() { size(640, 360); @@ -30,10 +34,9 @@ void setup() { // start ellipse at middle top of screen position = new PVector(width/2, 0); - // calculate initial random direction - direction = PVector.random2D(); - - velocity = new PVector(); + // calculate initial random velocity + velocity = PVector.random2D(); + velocity.mult(speed); } void draw() { @@ -42,11 +45,6 @@ void draw() { noStroke(); rect(0, 0, width, height); - for (int i=0; i width-r) { position.x = width-r; - direction.x *= -1; + velocity.x *= -1; } // left if (position.x < r) { position.x = r; - direction.x *= -1; + velocity.x *= -1; } // top if (position.y < r) { position.y = r; - direction.y *= -1; + velocity.y *= -1; // randomize base top base1.y = random(height-100, height); base2.y = random(height-100, height); @@ -111,6 +107,8 @@ void draw() { } } + +// Calculate variables for the ground void createGround() { // calculate length of base top baseLength = PVector.dist(base1, base2);