77 * vector.
88 */
99
10+ // Position of left hand side of floor
1011PVector base1;
12+ // Position of right hand side of floor
1113PVector base2;
14+ // Length of floor
1215float baseLength;
1316
17+ // An array of subpoints along the floor path
1418PVector [] coords;
1519
20+ // Variables related to moving ball
1621PVector position;
22+ PVector velocity;
1723float r = 6 ;
18- PVector direction;
1924float speed = 3.5 ;
20- PVector velocity;
2125
2226void setup () {
2327 size (640 , 360 );
@@ -30,10 +34,9 @@ void setup() {
3034 // start ellipse at middle top of screen
3135 position = new PVector (width / 2 , 0 );
3236
33- // calculate initial random direction
34- direction = PVector . random2D();
35-
36- velocity = new PVector ();
37+ // calculate initial random velocity
38+ velocity = PVector . random2D();
39+ velocity. mult(speed);
3740}
3841
3942void draw () {
@@ -42,11 +45,6 @@ void draw() {
4245 noStroke ();
4346 rect (0 , 0 , width , height );
4447
45- for (int i= 0 ; i< coords. length; i++ ) {
46- coords[i]. x = base1. x + ((base2. x- base1. x)/ baseLength)* i;
47- coords[i]. y = base1. y + ((base2. y- base1. y)/ baseLength)* i;
48- }
49-
5048 // draw base
5149 fill (200 );
5250 quad (base1. x, base1. y, base2. x, base2. y, base2. x, height , 0 , height );
@@ -61,15 +59,12 @@ void draw() {
6159 fill (255 );
6260 ellipse (position. x, position. y, r* 2 , r* 2 );
6361
64- // calculate ellipse velocity
65- velocity. set(direction);
66- velocity. mult(speed);
67-
6862 // move elipse
6963 position. add(velocity);
7064
7165 // normalized incidence vector
72- PVector incidence = PVector . mult(direction, - 1 );
66+ PVector incidence = PVector . mult(velocity, - 1 );
67+ incidence. normalize();
7368
7469 // detect and handle collision
7570 for (int i= 0 ; i< coords. length; i++ ) {
@@ -81,7 +76,8 @@ void draw() {
8176
8277 // calculate reflection vector
8378 // assign reflection vector to direction vector
84- direction. set(2 * normal. x* dot - incidence. x, 2 * normal. y* dot - incidence. y, 0 );
79+ velocity. set(2 * normal. x* dot - incidence. x, 2 * normal. y* dot - incidence. y, 0 );
80+ velocity. mult(speed);
8581
8682 // draw base top normal at collision point
8783 stroke (255 , 128 , 0 );
@@ -93,24 +89,26 @@ void draw() {
9389 // right
9490 if (position. x > width - r) {
9591 position. x = width - r;
96- direction . x *= - 1 ;
92+ velocity . x *= - 1 ;
9793 }
9894 // left
9995 if (position. x < r) {
10096 position. x = r;
101- direction . x *= - 1 ;
97+ velocity . x *= - 1 ;
10298 }
10399 // top
104100 if (position. y < r) {
105101 position. y = r;
106- direction . y *= - 1 ;
102+ velocity . y *= - 1 ;
107103 // randomize base top
108104 base1. y = random (height - 100 , height );
109105 base2. y = random (height - 100 , height );
110106 createGround();
111107 }
112108}
113109
110+
111+ // Calculate variables for the ground
114112void createGround () {
115113 // calculate length of base top
116114 baseLength = PVector . dist(base1, base2);
0 commit comments