From 1dfc9bfecfa90790748f44db39736be6340b58c4 Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Fri, 1 May 2026 22:30:06 +0100 Subject: [PATCH 01/37] setting up the original co-ordinates of the ball --- chapter3/Exercise_3_1/Exercise_3_1.pde | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 chapter3/Exercise_3_1/Exercise_3_1.pde diff --git a/chapter3/Exercise_3_1/Exercise_3_1.pde b/chapter3/Exercise_3_1/Exercise_3_1.pde new file mode 100644 index 0000000..0651f30 --- /dev/null +++ b/chapter3/Exercise_3_1/Exercise_3_1.pde @@ -0,0 +1,17 @@ +PVector ballOne; +PVector ballTwo; + + +void setup() { + + size(640, 360); + ballOne = new PVector(width/2, height/2 - 50); + ballTwo = new PVector(width/2, height/2 + 50); + +} + +void draw() { + background(255); + circle(ballOne.x, ballTwo.x, 2); + circle(ballTwo.x, ballTwo.x, 2); +} \ No newline at end of file From cc43c637a32bee1584c88258097f05d0c5a28f30 Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Fri, 1 May 2026 22:33:22 +0100 Subject: [PATCH 02/37] accidentaly set the y-coridnates of the circles to the value stored in the x-coordinate --- chapter3/Exercise_3_1/Exercise_3_1.pde | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/chapter3/Exercise_3_1/Exercise_3_1.pde b/chapter3/Exercise_3_1/Exercise_3_1.pde index 0651f30..e0b6f9b 100644 --- a/chapter3/Exercise_3_1/Exercise_3_1.pde +++ b/chapter3/Exercise_3_1/Exercise_3_1.pde @@ -12,6 +12,7 @@ void setup() { void draw() { background(255); - circle(ballOne.x, ballTwo.x, 2); - circle(ballTwo.x, ballTwo.x, 2); + circle(ballOne.x, ballTwo.y, 2); + circle(ballTwo.x, ballTwo.y, 2); + line(ballOne.x, ballOne.y, ballTwo.x, ballTwo.y); } \ No newline at end of file From a9544775e076035c2ff15beb460bf3a40d9610b6 Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Fri, 1 May 2026 22:34:03 +0100 Subject: [PATCH 03/37] ignore, whitespace --- chapter3/Exercise_3_1/Exercise_3_1.pde | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chapter3/Exercise_3_1/Exercise_3_1.pde b/chapter3/Exercise_3_1/Exercise_3_1.pde index e0b6f9b..cf4bd06 100644 --- a/chapter3/Exercise_3_1/Exercise_3_1.pde +++ b/chapter3/Exercise_3_1/Exercise_3_1.pde @@ -3,7 +3,7 @@ PVector ballTwo; void setup() { - + size(640, 360); ballOne = new PVector(width/2, height/2 - 50); ballTwo = new PVector(width/2, height/2 + 50); From ca7b68787cc8553184604cd0a97648706144e732 Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Fri, 1 May 2026 22:35:57 +0100 Subject: [PATCH 04/37] accidently set the ball to the same co-ordinates to chaning that and increasing radius because ball was two small --- chapter3/Exercise_3_1/Exercise_3_1.pde | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chapter3/Exercise_3_1/Exercise_3_1.pde b/chapter3/Exercise_3_1/Exercise_3_1.pde index cf4bd06..ca85740 100644 --- a/chapter3/Exercise_3_1/Exercise_3_1.pde +++ b/chapter3/Exercise_3_1/Exercise_3_1.pde @@ -12,7 +12,7 @@ void setup() { void draw() { background(255); - circle(ballOne.x, ballTwo.y, 2); - circle(ballTwo.x, ballTwo.y, 2); + circle(ballOne.x, ballOne.y, 5); + circle(ballTwo.x, ballTwo.y, 5); line(ballOne.x, ballOne.y, ballTwo.x, ballTwo.y); } \ No newline at end of file From bae9849ce570e5ac56a119aab47706c81f8dd347 Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Fri, 1 May 2026 22:38:09 +0100 Subject: [PATCH 05/37] making the radius 4 times bigger and filling in the circle to a greyish colour --- chapter3/Exercise_3_1/Exercise_3_1.pde | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/chapter3/Exercise_3_1/Exercise_3_1.pde b/chapter3/Exercise_3_1/Exercise_3_1.pde index ca85740..5c0dc50 100644 --- a/chapter3/Exercise_3_1/Exercise_3_1.pde +++ b/chapter3/Exercise_3_1/Exercise_3_1.pde @@ -12,7 +12,8 @@ void setup() { void draw() { background(255); - circle(ballOne.x, ballOne.y, 5); - circle(ballTwo.x, ballTwo.y, 5); + fill(175); + circle(ballOne.x, ballOne.y, 20); + circle(ballTwo.x, ballTwo.y, 20); line(ballOne.x, ballOne.y, ballTwo.x, ballTwo.y); } \ No newline at end of file From c0779a78b96328863e29df6f59153cdf8f4c9d07 Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Fri, 1 May 2026 22:42:19 +0100 Subject: [PATCH 06/37] shifting center to mid point of screen --- chapter3/Exercise_3_1/Exercise_3_1.pde | 2 ++ 1 file changed, 2 insertions(+) diff --git a/chapter3/Exercise_3_1/Exercise_3_1.pde b/chapter3/Exercise_3_1/Exercise_3_1.pde index 5c0dc50..3fb550f 100644 --- a/chapter3/Exercise_3_1/Exercise_3_1.pde +++ b/chapter3/Exercise_3_1/Exercise_3_1.pde @@ -13,6 +13,8 @@ void setup() { void draw() { background(255); fill(175); + pushMatrix(); + translate(width/2, height/2); circle(ballOne.x, ballOne.y, 20); circle(ballTwo.x, ballTwo.y, 20); line(ballOne.x, ballOne.y, ballTwo.x, ballTwo.y); From eebc8e82abe6cd3ab2949dad0d958313e71ecb81 Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Fri, 1 May 2026 22:42:37 +0100 Subject: [PATCH 07/37] restoring old co-ordinate system --- chapter3/Exercise_3_1/Exercise_3_1.pde | 1 + 1 file changed, 1 insertion(+) diff --git a/chapter3/Exercise_3_1/Exercise_3_1.pde b/chapter3/Exercise_3_1/Exercise_3_1.pde index 3fb550f..b691b1d 100644 --- a/chapter3/Exercise_3_1/Exercise_3_1.pde +++ b/chapter3/Exercise_3_1/Exercise_3_1.pde @@ -18,4 +18,5 @@ void draw() { circle(ballOne.x, ballOne.y, 20); circle(ballTwo.x, ballTwo.y, 20); line(ballOne.x, ballOne.y, ballTwo.x, ballTwo.y); + popMatrix(); } \ No newline at end of file From ebda6308018fec3e62e60ac30c4687adec9a2a0a Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Fri, 1 May 2026 22:43:50 +0100 Subject: [PATCH 08/37] since centre is shifted to midpoint of screen, we can adjust co-ordinates of each balls center relative to the midpoint of the screen --- chapter3/Exercise_3_1/Exercise_3_1.pde | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chapter3/Exercise_3_1/Exercise_3_1.pde b/chapter3/Exercise_3_1/Exercise_3_1.pde index b691b1d..57a89a9 100644 --- a/chapter3/Exercise_3_1/Exercise_3_1.pde +++ b/chapter3/Exercise_3_1/Exercise_3_1.pde @@ -5,8 +5,8 @@ PVector ballTwo; void setup() { size(640, 360); - ballOne = new PVector(width/2, height/2 - 50); - ballTwo = new PVector(width/2, height/2 + 50); + ballOne = new PVector(0, -50); + ballTwo = new PVector(0, 50); } From 768c00f3427786da4febbb7ba931157287b318ad Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Fri, 1 May 2026 22:54:44 +0100 Subject: [PATCH 09/37] rotating the baton like object by a constant angle amount of 0.1 radians --- chapter3/Exercise_3_1/Exercise_3_1.pde | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/chapter3/Exercise_3_1/Exercise_3_1.pde b/chapter3/Exercise_3_1/Exercise_3_1.pde index 57a89a9..41d3344 100644 --- a/chapter3/Exercise_3_1/Exercise_3_1.pde +++ b/chapter3/Exercise_3_1/Exercise_3_1.pde @@ -1,5 +1,6 @@ PVector ballOne; PVector ballTwo; +float angle = 0; void setup() { @@ -15,8 +16,10 @@ void draw() { fill(175); pushMatrix(); translate(width/2, height/2); + rotate(angle); circle(ballOne.x, ballOne.y, 20); circle(ballTwo.x, ballTwo.y, 20); - line(ballOne.x, ballOne.y, ballTwo.x, ballTwo.y); + line(ballOne.x, ballOne.y+10, ballTwo.x, ballTwo.y-10); popMatrix(); + angle += 0.1; } \ No newline at end of file From e767485a1ed18cf3d6b0fb3c7bae023d74da2a70 Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Fri, 1 May 2026 22:55:54 +0100 Subject: [PATCH 10/37] increasing the weight of the stroke to make the border of cirles and lines more thicker --- chapter3/Exercise_3_1/Exercise_3_1.pde | 1 + 1 file changed, 1 insertion(+) diff --git a/chapter3/Exercise_3_1/Exercise_3_1.pde b/chapter3/Exercise_3_1/Exercise_3_1.pde index 41d3344..3f43f65 100644 --- a/chapter3/Exercise_3_1/Exercise_3_1.pde +++ b/chapter3/Exercise_3_1/Exercise_3_1.pde @@ -14,6 +14,7 @@ void setup() { void draw() { background(255); fill(175); + strokeWeight(2); pushMatrix(); translate(width/2, height/2); rotate(angle); From 9afcfbd326adbe6f4e04deaab71cca285575c3eb Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Mon, 18 May 2026 14:00:19 +0100 Subject: [PATCH 11/37] adding the file responsible for this exercise --- chapter3/Exercise_3_2/Exercise_3_2.pde | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 chapter3/Exercise_3_2/Exercise_3_2.pde diff --git a/chapter3/Exercise_3_2/Exercise_3_2.pde b/chapter3/Exercise_3_2/Exercise_3_2.pde new file mode 100644 index 0000000..e69de29 From b3569d9e22b48fd4b4ebe90a8715b774ff46369e Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Mon, 18 May 2026 14:01:34 +0100 Subject: [PATCH 12/37] will contain the mover class used to define the object moving --- chapter3/Exercise_3_2/mover.pde | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 chapter3/Exercise_3_2/mover.pde diff --git a/chapter3/Exercise_3_2/mover.pde b/chapter3/Exercise_3_2/mover.pde new file mode 100644 index 0000000..3b3840b --- /dev/null +++ b/chapter3/Exercise_3_2/mover.pde @@ -0,0 +1,3 @@ +class Mover { + +} \ No newline at end of file From feb4950e0a654c97e978c88d9a7e89ec23b2ee07 Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Mon, 18 May 2026 14:06:28 +0100 Subject: [PATCH 13/37] adding the attributes for class --- chapter3/Exercise_3_2/mover.pde | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/chapter3/Exercise_3_2/mover.pde b/chapter3/Exercise_3_2/mover.pde index 3b3840b..13c5e32 100644 --- a/chapter3/Exercise_3_2/mover.pde +++ b/chapter3/Exercise_3_2/mover.pde @@ -1,3 +1,10 @@ class Mover { + + PVector Location; + PVector acceleration; + PVector velocity; + float mass; + + } \ No newline at end of file From e28ecc5616685858f5f4d7d567245f58530cd278 Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Mon, 18 May 2026 14:15:01 +0100 Subject: [PATCH 14/37] initlaise attributes --- chapter3/Exercise_3_2/mover.pde | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/chapter3/Exercise_3_2/mover.pde b/chapter3/Exercise_3_2/mover.pde index 13c5e32..04db47b 100644 --- a/chapter3/Exercise_3_2/mover.pde +++ b/chapter3/Exercise_3_2/mover.pde @@ -5,6 +5,12 @@ class Mover { PVector velocity; float mass; - + public Mover(PVector location_, double mass_) { + this.location = location_; + this.acceleration = new PVector (0, 0); + this.velocity = new PVector(0, 0); + this.mass = mass_; + } + } \ No newline at end of file From 20f8a12d5b0a481638bfb1fe61fdc33e08b3c173 Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Mon, 18 May 2026 14:15:40 +0100 Subject: [PATCH 15/37] changing the data type of mass --- chapter3/Exercise_3_2/mover.pde | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chapter3/Exercise_3_2/mover.pde b/chapter3/Exercise_3_2/mover.pde index 04db47b..29136fe 100644 --- a/chapter3/Exercise_3_2/mover.pde +++ b/chapter3/Exercise_3_2/mover.pde @@ -1,9 +1,9 @@ class Mover { - PVector Location; + PVector location; PVector acceleration; PVector velocity; - float mass; + double mass; public Mover(PVector location_, double mass_) { this.location = location_; From 62d237998802512ebd487791c3d95a2f4d3c5501 Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Mon, 18 May 2026 14:22:39 +0100 Subject: [PATCH 16/37] method to add the overall acceleration to an object --- chapter3/Exercise_3_2/mover.pde | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/chapter3/Exercise_3_2/mover.pde b/chapter3/Exercise_3_2/mover.pde index 29136fe..8011ccb 100644 --- a/chapter3/Exercise_3_2/mover.pde +++ b/chapter3/Exercise_3_2/mover.pde @@ -12,5 +12,11 @@ class Mover { this.mass = mass_; } - + public void applyForce(PVector force) { + PVector copyForce = PVector.mult(force, 1); + copyForce.div((float) mass); //resultant acceleration + acceleration.add(copyForce); + } + + public void } \ No newline at end of file From 791f75ddbff89ef561f43ca3d657a5a56e7d0503 Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Mon, 18 May 2026 14:25:20 +0100 Subject: [PATCH 17/37] adding the method to add resultnat force --- chapter3/Exercise_3_2/mover.pde | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/chapter3/Exercise_3_2/mover.pde b/chapter3/Exercise_3_2/mover.pde index 8011ccb..ecf2194 100644 --- a/chapter3/Exercise_3_2/mover.pde +++ b/chapter3/Exercise_3_2/mover.pde @@ -18,5 +18,12 @@ class Mover { acceleration.add(copyForce); } - public void + public void update() { + velocity.add(acceleration); + location.add(velocity); + + acceleration.mult(0); + } + + } \ No newline at end of file From 344de9ab84a9dce04db0e8ad087cacba0a6854c2 Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Mon, 18 May 2026 14:26:43 +0100 Subject: [PATCH 18/37] drawing the ball with specific mass --- chapter3/Exercise_3_2/mover.pde | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/chapter3/Exercise_3_2/mover.pde b/chapter3/Exercise_3_2/mover.pde index ecf2194..b9dc783 100644 --- a/chapter3/Exercise_3_2/mover.pde +++ b/chapter3/Exercise_3_2/mover.pde @@ -25,5 +25,9 @@ class Mover { acceleration.mult(0); } - + public void draw() { + fill(175); + circle(location.x, location.y, 5); + } + } \ No newline at end of file From a07d86fb9d8138b7c08a8929a05b800e3e0bfd70 Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Mon, 18 May 2026 14:43:17 +0100 Subject: [PATCH 19/37] implementing the logic for the projectile motion of ball --- chapter3/Exercise_3_2/Exercise_3_2.pde | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/chapter3/Exercise_3_2/Exercise_3_2.pde b/chapter3/Exercise_3_2/Exercise_3_2.pde index e69de29..c413733 100644 --- a/chapter3/Exercise_3_2/Exercise_3_2.pde +++ b/chapter3/Exercise_3_2/Exercise_3_2.pde @@ -0,0 +1,19 @@ +Mover ball; +float G = (float) 0.5; + +void setup() { + size(640, 360); + ball = new Mover(new PVector(0, height/2), 0.5); + ball.applyForce(new PVector(1, -1)); +} + +void draw() { + background(255); + //apply weight + // + float ball_mass = (float) ball.getMass(); + + ball.applyForce(new PVector(0.0, G*ball_mass)); + ball.update(); + ball.draw(); +} \ No newline at end of file From 977e2df62fbbbfc1cbba5340603ce8ac723f7264 Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Mon, 18 May 2026 14:44:16 +0100 Subject: [PATCH 20/37] adding getter method --- chapter3/Exercise_3_2/mover.pde | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/chapter3/Exercise_3_2/mover.pde b/chapter3/Exercise_3_2/mover.pde index b9dc783..659bb32 100644 --- a/chapter3/Exercise_3_2/mover.pde +++ b/chapter3/Exercise_3_2/mover.pde @@ -30,4 +30,8 @@ class Mover { circle(location.x, location.y, 5); } + public double getMass() { + return mass; + } + } \ No newline at end of file From 541450962f43f839c3f243e7b3231b2364b891d7 Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Mon, 18 May 2026 14:51:15 +0100 Subject: [PATCH 21/37] making the ball a bit bigger --- chapter3/Exercise_3_2/mover.pde | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chapter3/Exercise_3_2/mover.pde b/chapter3/Exercise_3_2/mover.pde index 659bb32..e874c27 100644 --- a/chapter3/Exercise_3_2/mover.pde +++ b/chapter3/Exercise_3_2/mover.pde @@ -27,7 +27,7 @@ class Mover { public void draw() { fill(175); - circle(location.x, location.y, 5); + circle(location.x, location.y, 10); } public double getMass() { From 5a807e705cade5459d8204a4efb70056e3fd4486 Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Mon, 18 May 2026 14:51:54 +0100 Subject: [PATCH 22/37] tweaking the values to make the simulation a bit more realistic --- chapter3/Exercise_3_2/Exercise_3_2.pde | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chapter3/Exercise_3_2/Exercise_3_2.pde b/chapter3/Exercise_3_2/Exercise_3_2.pde index c413733..738de87 100644 --- a/chapter3/Exercise_3_2/Exercise_3_2.pde +++ b/chapter3/Exercise_3_2/Exercise_3_2.pde @@ -1,10 +1,10 @@ Mover ball; -float G = (float) 0.5; +float G = (float) 0.1; void setup() { size(640, 360); ball = new Mover(new PVector(0, height/2), 0.5); - ball.applyForce(new PVector(1, -1)); + ball.applyForce(new PVector(2.5, -2.5)); } void draw() { From d030ca529418c49af5320b298c9c22f3255962b0 Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Mon, 18 May 2026 14:56:12 +0100 Subject: [PATCH 23/37] chaning the shape to square so we can make rotations much more obvious --- chapter3/Exercise_3_2/mover.pde | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/chapter3/Exercise_3_2/mover.pde b/chapter3/Exercise_3_2/mover.pde index e874c27..628269b 100644 --- a/chapter3/Exercise_3_2/mover.pde +++ b/chapter3/Exercise_3_2/mover.pde @@ -27,7 +27,8 @@ class Mover { public void draw() { fill(175); - circle(location.x, location.y, 10); + rectMode(CENTER); + rect(location.x, location.y, 10, 10); } public double getMass() { From 1db4ad0586ed213d670a0f0c1a527b366219f449 Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Mon, 18 May 2026 15:01:55 +0100 Subject: [PATCH 24/37] used to keep track of the angularVelocity of object --- chapter3/Exercise_3_2/mover.pde | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/chapter3/Exercise_3_2/mover.pde b/chapter3/Exercise_3_2/mover.pde index 628269b..66c0ec0 100644 --- a/chapter3/Exercise_3_2/mover.pde +++ b/chapter3/Exercise_3_2/mover.pde @@ -5,6 +5,10 @@ class Mover { PVector velocity; double mass; + float angle; + float aAcceleration; + float aVelocity; + public Mover(PVector location_, double mass_) { this.location = location_; this.acceleration = new PVector (0, 0); From d803710cf3cf39d3cdd7d5bad6ce5221a814c88f Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Mon, 18 May 2026 15:05:34 +0100 Subject: [PATCH 25/37] allowing angle to be updated --- chapter3/Exercise_3_2/mover.pde | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/chapter3/Exercise_3_2/mover.pde b/chapter3/Exercise_3_2/mover.pde index 66c0ec0..b0e26d0 100644 --- a/chapter3/Exercise_3_2/mover.pde +++ b/chapter3/Exercise_3_2/mover.pde @@ -25,7 +25,10 @@ class Mover { public void update() { velocity.add(acceleration); location.add(velocity); - + + aAcceleration = acceleration.y; + aVelocity += aAcceleration; + acceleration.mult(0); } From 32619b237b667b60d96ffbaf3f119d3bb3dcf8e7 Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Mon, 18 May 2026 15:06:20 +0100 Subject: [PATCH 26/37] updating the angle with the angular velocity --- chapter3/Exercise_3_2/mover.pde | 2 ++ 1 file changed, 2 insertions(+) diff --git a/chapter3/Exercise_3_2/mover.pde b/chapter3/Exercise_3_2/mover.pde index b0e26d0..4c7bed0 100644 --- a/chapter3/Exercise_3_2/mover.pde +++ b/chapter3/Exercise_3_2/mover.pde @@ -30,6 +30,8 @@ class Mover { aVelocity += aAcceleration; acceleration.mult(0); + + angle += aVelocity; } public void draw() { From 0b9c4af4c36ddf9ae42b0c0c989ac27868b6a157 Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Mon, 18 May 2026 15:09:19 +0100 Subject: [PATCH 27/37] rotating the square --- chapter3/Exercise_3_2/mover.pde | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/chapter3/Exercise_3_2/mover.pde b/chapter3/Exercise_3_2/mover.pde index 4c7bed0..f3adb40 100644 --- a/chapter3/Exercise_3_2/mover.pde +++ b/chapter3/Exercise_3_2/mover.pde @@ -36,8 +36,15 @@ class Mover { public void draw() { fill(175); + + pushMatrix(); + + translate(location.x, location.y); rectMode(CENTER); - rect(location.x, location.y, 10, 10); + rotate(angle); + rect(0, 0, 10, 10); + + popMatrix(); } public double getMass() { From 79fc6eca93fc87a7a05b49c652d9d6870363f1cf Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Mon, 18 May 2026 15:13:50 +0100 Subject: [PATCH 28/37] making the angular acceleration a bit smaller and constraining the velocity between -0.1 and 0.1 --- chapter3/Exercise_3_2/mover.pde | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/chapter3/Exercise_3_2/mover.pde b/chapter3/Exercise_3_2/mover.pde index f3adb40..0372b92 100644 --- a/chapter3/Exercise_3_2/mover.pde +++ b/chapter3/Exercise_3_2/mover.pde @@ -26,8 +26,9 @@ class Mover { velocity.add(acceleration); location.add(velocity); - aAcceleration = acceleration.y; + aAcceleration = acceleration.y/10.0; aVelocity += aAcceleration; + aVelocity = constrain(aVelocity,-0.1,0.1); acceleration.mult(0); @@ -42,8 +43,8 @@ class Mover { translate(location.x, location.y); rectMode(CENTER); rotate(angle); - rect(0, 0, 10, 10); - + rect(0, 0, 20, 20); + popMatrix(); } From d7f24de788e08f28efadb405d730364ed63636e5 Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Mon, 18 May 2026 16:44:03 +0100 Subject: [PATCH 29/37] doing exercise 3.3 --- chapter3/Exercise_3_3/Exercise_3_3.pde | 26 +++++++++++++++ chapter3/Exercise_3_3/mover.pde | 45 ++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 chapter3/Exercise_3_3/Exercise_3_3.pde create mode 100644 chapter3/Exercise_3_3/mover.pde diff --git a/chapter3/Exercise_3_3/Exercise_3_3.pde b/chapter3/Exercise_3_3/Exercise_3_3.pde new file mode 100644 index 0000000..e588057 --- /dev/null +++ b/chapter3/Exercise_3_3/Exercise_3_3.pde @@ -0,0 +1,26 @@ +Mover car; +float G = (float) 0.5; + +void setup() { + size(640, 360); + car = new Mover(new PVector(0, height/2), 0.5); +} + +void draw() { + background(255); + car.update(); + car.draw(); +} + +void keyPressed() { + float car_mass = (float) car.getMass(); + if (key == CODED) { + if (keyCode == RIGHT) { + car.applyForce(new PVector(G*car_mass, 0.0)); + } + else if (keyCode == LEFT) { + car.applyForce(new PVector(-G*car_mass, 0.0)); + } + } + +} \ No newline at end of file diff --git a/chapter3/Exercise_3_3/mover.pde b/chapter3/Exercise_3_3/mover.pde new file mode 100644 index 0000000..587089a --- /dev/null +++ b/chapter3/Exercise_3_3/mover.pde @@ -0,0 +1,45 @@ +class Mover { + + PVector location; + PVector acceleration; + PVector velocity; + double mass; + + public Mover(PVector location_, double mass_) { + this.location = location_; + this.acceleration = new PVector (0, 0); + this.velocity = new PVector(0, 0); + this.mass = mass_; + } + + public void applyForce(PVector force) { + PVector copyForce = PVector.mult(force, 1); + copyForce.div((float) mass); //resultant acceleration + acceleration.add(copyForce); + } + + public void update() { + velocity.add(acceleration); + location.add(velocity); + + acceleration.mult(0); + + } + + public void draw() { + fill(175); + + pushMatrix(); + + translate(location.x, location.y); + rectMode(CENTER); + rect(0, 0, 20, 20); + + popMatrix(); + } + + public double getMass() { + return mass; + } + +} \ No newline at end of file From 4118b6b675d8f89bf68721690360d5b8a9fe4c71 Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Wed, 20 May 2026 16:31:14 +0100 Subject: [PATCH 30/37] drawing vector representing velocity --- chapter3/Exercise_3_3/mover.pde | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chapter3/Exercise_3_3/mover.pde b/chapter3/Exercise_3_3/mover.pde index 587089a..f1e4f5a 100644 --- a/chapter3/Exercise_3_3/mover.pde +++ b/chapter3/Exercise_3_3/mover.pde @@ -28,13 +28,13 @@ class Mover { public void draw() { fill(175); + strokeWeight(5); pushMatrix(); - translate(location.x, location.y); rectMode(CENTER); rect(0, 0, 20, 20); - + line(0, 0, velocity.x, velocity.y); popMatrix(); } From 39eba4ccb9015f9895a4d455bdb49db86c782057 Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Wed, 20 May 2026 16:37:52 +0100 Subject: [PATCH 31/37] making the direction of the velocity more apparent --- chapter3/Exercise_3_3/mover.pde | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/chapter3/Exercise_3_3/mover.pde b/chapter3/Exercise_3_3/mover.pde index f1e4f5a..bfb380e 100644 --- a/chapter3/Exercise_3_3/mover.pde +++ b/chapter3/Exercise_3_3/mover.pde @@ -28,13 +28,14 @@ class Mover { public void draw() { fill(175); - strokeWeight(5); - + strokeWeight(1); pushMatrix(); translate(location.x, location.y); rectMode(CENTER); rect(0, 0, 20, 20); - line(0, 0, velocity.x, velocity.y); + fill(0); + strokeWeight(2); + line(0, 0, 4*velocity.x, 4*velocity.y); popMatrix(); } From 2de138a6b564c4bd829b7df3d35ea9eb7f596005 Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Wed, 20 May 2026 17:15:40 +0100 Subject: [PATCH 32/37] solution to exercise 3.4 --- chapter3/Exercise_3_4/Exercise_3_4.pde | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 chapter3/Exercise_3_4/Exercise_3_4.pde diff --git a/chapter3/Exercise_3_4/Exercise_3_4.pde b/chapter3/Exercise_3_4/Exercise_3_4.pde new file mode 100644 index 0000000..c3a49a0 --- /dev/null +++ b/chapter3/Exercise_3_4/Exercise_3_4.pde @@ -0,0 +1,22 @@ + +float theta = 0; +float r = 75*theta; +float a = 0.001; + +void setup() { + size(640,360); + background(255); +} +void draw() { + float x = r * cos(theta); + float y = r * sin(theta); + + noStroke(); + fill(0); + ellipse(x+ width/2, y+height/2, 6, 6); + + theta += 0.01; + a+=0.001; + r = 75*a; + +} From c58e2bfab8a97d075e3451f6da43fe8eb25bf895 Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Wed, 20 May 2026 18:04:50 +0100 Subject: [PATCH 33/37] class definition needed for space ship --- chapter3/Exercise_3_5/spaceShip.pde | 62 +++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 chapter3/Exercise_3_5/spaceShip.pde diff --git a/chapter3/Exercise_3_5/spaceShip.pde b/chapter3/Exercise_3_5/spaceShip.pde new file mode 100644 index 0000000..6f8072b --- /dev/null +++ b/chapter3/Exercise_3_5/spaceShip.pde @@ -0,0 +1,62 @@ +class SpaceShip { + + PVector location; + PVector acceleration; + PVector velocity; + + double mass; + float angle; + + public SpaceShip(PVector location_, double mass_) { + this.location = location_; + this.acceleration = new PVector (0, 0); + this.velocity = new PVector(0.5, -0.25); + this.mass = mass_; + angle = 0.0; + } + + public void applyForce(PVector force) { + PVector copyForce = PVector.mult(force, 1); + copyForce.div((float) mass); //resultant acceleration + acceleration.add(copyForce); + } + + public void update() { + velocity.add(acceleration); + location.add(velocity); + + acceleration.mult(0); + + } + + public void draw() { + + fill(175); + pushMatrix(); + translate(location.x, location.y); + rectMode(CENTER); + rotate(angle); + rect(0, 0, 20, 20); + popMatrix(); + + } + + public double getMass() { + return mass; + } + + public PVector getVelocity() { + return velocity; + } + + public void rotateClockwise() { + angle += 0.1; + velocity.rotate(angle); + } + + public void rotateAntiClockwise() { + angle -= 0.1; + velocity.rotate(angle); + } + +} \ No newline at end of file From 3f015bf65ec1eff12d96d16d0cb43c22176c80fd Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Wed, 20 May 2026 18:05:01 +0100 Subject: [PATCH 34/37] solution to exercise 3.5 --- chapter3/Exercise_3_5/Exercise_3_5.pde | 32 ++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 chapter3/Exercise_3_5/Exercise_3_5.pde diff --git a/chapter3/Exercise_3_5/Exercise_3_5.pde b/chapter3/Exercise_3_5/Exercise_3_5.pde new file mode 100644 index 0000000..3dd9371 --- /dev/null +++ b/chapter3/Exercise_3_5/Exercise_3_5.pde @@ -0,0 +1,32 @@ +SpaceShip ship; + +void setup() { + size(640, 360); + ship = new SpaceShip(new PVector(0, height), 0.5); +} + +void draw() { + background(255); + ship.update(); + ship.draw(); +} + +void keyPressed() { + + if (key == CODED) { + if (keyCode == RIGHT) { + ship.rotateClockwise(); + } + else if (keyCode == LEFT) { + ship.rotateAntiClockwise(); + } + } + + else if ((key == 'Z') || (key == 'z')) { + PVector velocity = ship.getVelocity(); + velocity.normalize(); + PVector thrust = velocity.mult(1.5); + ship.applyForce(thrust); + } + +} \ No newline at end of file From e033976f9b2e0af52aaa2eaecdcab7274e1c974e Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Thu, 21 May 2026 18:18:19 +0100 Subject: [PATCH 35/37] solution to exercise 3.6 --- chapter3/Exercise_3_6/Exercise_3_6.pde | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 chapter3/Exercise_3_6/Exercise_3_6.pde diff --git a/chapter3/Exercise_3_6/Exercise_3_6.pde b/chapter3/Exercise_3_6/Exercise_3_6.pde new file mode 100644 index 0000000..0ef0ce8 --- /dev/null +++ b/chapter3/Exercise_3_6/Exercise_3_6.pde @@ -0,0 +1,25 @@ +int frameCount = 0; +float period = 120; +float amplitude = 100; + +void setup() { + size(640,360); +} +void draw() { + background(255); + + float y = cos(TWO_PI * frameCount / period); + + stroke(0); + fill(175); + + pushMatrix(); + + translate(width/2,height/2); + float displacement = map(y, -1, 1, 0, amplitude); + line(0,0,0,displacement); + ellipse(0,displacement,20,20); + + popMatrix(); + frameCount+=1; +} \ No newline at end of file From a1db7c031122b69e2e311f46f352adbe91dab871 Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Sat, 23 May 2026 22:19:54 +0100 Subject: [PATCH 36/37] solution to exercise 3.7 --- chapter3/Exercise_3_7/Exercise_3_7.pde | 18 +++++++++++ chapter3/Exercise_3_7/oscillator.pde | 41 ++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 chapter3/Exercise_3_7/Exercise_3_7.pde create mode 100644 chapter3/Exercise_3_7/oscillator.pde diff --git a/chapter3/Exercise_3_7/Exercise_3_7.pde b/chapter3/Exercise_3_7/Exercise_3_7.pde new file mode 100644 index 0000000..36a48aa --- /dev/null +++ b/chapter3/Exercise_3_7/Exercise_3_7.pde @@ -0,0 +1,18 @@ +Oscillator[] oscillators = new Oscillator[10]; + +void setup() { + size(640, 360); + // Initialize all objects + for (int i = 0; i < 10; i++) { + oscillators[i] = new Oscillator(i); + } +} + +void draw() { + background(255); + // Run all objects + for (int i = 0; i < oscillators.length; i++) { + oscillators[i].update(); + oscillators[i].show(); + } +} \ No newline at end of file diff --git a/chapter3/Exercise_3_7/oscillator.pde b/chapter3/Exercise_3_7/oscillator.pde new file mode 100644 index 0000000..598116c --- /dev/null +++ b/chapter3/Exercise_3_7/oscillator.pde @@ -0,0 +1,41 @@ +// The Nature of Code +// Daniel Shiffman +// http://natureofcode.com + +class Oscillator { + PVector angle, angleVelocity, amplitude; + + public Oscillator(int index) { + this.angle = new PVector(); + this.amplitude = new PVector( + 50, 50 + ); + if (index <= 5) { + this.angleVelocity = new PVector(0.05, 0.05); + this.angleVelocity.rotate(-22.5*index); + } + else { + this.angleVelocity = new PVector(-0.05, 0.05); + this.angleVelocity.rotate(22.5*index); + } + + } + + void update() { + this.angle.add(this.angleVelocity); + } + + void show() { + float x = sin(this.angle.x) * this.amplitude.x; + float y = sin(this.angle.y) * this.amplitude.y; + + push(); + translate(width / 2, height / 2); + stroke(0); + strokeWeight(2); + fill(127); + line(0, 0, x, y); + circle(x, y, 32); + pop(); + } +} From 5ebde1439c4f8ecca809bfb40cc153bf104b757c Mon Sep 17 00:00:00 2001 From: Behlool Khan Date: Sat, 23 May 2026 22:26:05 +0100 Subject: [PATCH 37/37] adding angular acceleration --- chapter3/Exercise_3_8/Exercise_3_8.pde | 23 ++++++++++++++++ chapter3/Exercise_3_8/Oscillator.pde | 38 ++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 chapter3/Exercise_3_8/Exercise_3_8.pde create mode 100644 chapter3/Exercise_3_8/Oscillator.pde diff --git a/chapter3/Exercise_3_8/Exercise_3_8.pde b/chapter3/Exercise_3_8/Exercise_3_8.pde new file mode 100644 index 0000000..5923247 --- /dev/null +++ b/chapter3/Exercise_3_8/Exercise_3_8.pde @@ -0,0 +1,23 @@ +// The Nature of Code +// Daniel Shiffman +// http://natureofcode.com + +// An array of objects +Oscillator[] oscillators = new Oscillator[10]; + +void setup() { + size(640, 360); + // Initialize all objects + for (int i = 0; i < 10; i++) { + oscillators[i] = new Oscillator(); + } +} + +void draw() { + background(255); + // Run all objects + for (int i = 0; i < oscillators.length; i++) { + oscillators[i].update(); + oscillators[i].show(); + } +} diff --git a/chapter3/Exercise_3_8/Oscillator.pde b/chapter3/Exercise_3_8/Oscillator.pde new file mode 100644 index 0000000..56c0562 --- /dev/null +++ b/chapter3/Exercise_3_8/Oscillator.pde @@ -0,0 +1,38 @@ +// The Nature of Code +// Daniel Shiffman +// http://natureofcode.com + +class Oscillator { + PVector angle, angleVelocity, amplitude, angularAcceleration; + + Oscillator() { + this.angle = new PVector(); + this.angleVelocity = new PVector(random(-0.05, 0.05), random(-0.05, 0.05)); + this.amplitude = new PVector( + random(20, width / 2), + random(20, height / 2) + ); + this.angularAcceleration = new PVector(random(-0.001, 0.001), random(-0.001, 0.001)); + } + + void update() { + + this.angleVelocity.add(this.angularAcceleration); + this.angle.add(this.angleVelocity); + + } + + void show() { + float x = sin(this.angle.x) * this.amplitude.x; + float y = sin(this.angle.y) * this.amplitude.y; + + push(); + translate(width / 2, height / 2); + stroke(0); + strokeWeight(2); + fill(127); + line(0, 0, x, y); + circle(x, y, 32); + pop(); + } +}