Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
1dfc9bf
setting up the original co-ordinates of the ball
BehloolKhan May 1, 2026
cc43c63
accidentaly set the y-coridnates of the circles to the value stored i…
BehloolKhan May 1, 2026
a954477
ignore, whitespace
BehloolKhan May 1, 2026
ca7b687
accidently set the ball to the same co-ordinates to chaning that and …
BehloolKhan May 1, 2026
bae9849
making the radius 4 times bigger and filling in the circle to a greyi…
BehloolKhan May 1, 2026
c0779a7
shifting center to mid point of screen
BehloolKhan May 1, 2026
eebc8e8
restoring old co-ordinate system
BehloolKhan May 1, 2026
ebda630
since centre is shifted to midpoint of screen, we can adjust co-ordin…
BehloolKhan May 1, 2026
768c00f
rotating the baton like object by a constant angle amount of 0.1 radians
BehloolKhan May 1, 2026
e767485
increasing the weight of the stroke to make the border of cirles and …
BehloolKhan May 1, 2026
9afcfbd
adding the file responsible for this exercise
BehloolKhan May 18, 2026
b3569d9
will contain the mover class used to define the object moving
BehloolKhan May 18, 2026
feb4950
adding the attributes for class
BehloolKhan May 18, 2026
e28ecc5
initlaise attributes
BehloolKhan May 18, 2026
20f8a12
changing the data type of mass
BehloolKhan May 18, 2026
62d2379
method to add the overall acceleration to an object
BehloolKhan May 18, 2026
791f75d
adding the method to add resultnat force
BehloolKhan May 18, 2026
344de9a
drawing the ball with specific mass
BehloolKhan May 18, 2026
a07d86f
implementing the logic for the projectile motion of ball
BehloolKhan May 18, 2026
977e2df
adding getter method
BehloolKhan May 18, 2026
5414509
making the ball a bit bigger
BehloolKhan May 18, 2026
5a807e7
tweaking the values to make the simulation a bit more realistic
BehloolKhan May 18, 2026
d030ca5
chaning the shape to square so we can make rotations much more obvious
BehloolKhan May 18, 2026
1db4ad0
used to keep track of the angularVelocity of object
BehloolKhan May 18, 2026
d803710
allowing angle to be updated
BehloolKhan May 18, 2026
32619b2
updating the angle with the angular velocity
BehloolKhan May 18, 2026
0b9c4af
rotating the square
BehloolKhan May 18, 2026
79fc6ec
making the angular acceleration a bit smaller and constraining the ve…
BehloolKhan May 18, 2026
d7f24de
doing exercise 3.3
BehloolKhan May 18, 2026
4118b6b
drawing vector representing velocity
BehloolKhan May 20, 2026
39eba4c
making the direction of the velocity more apparent
BehloolKhan May 20, 2026
2de138a
solution to exercise 3.4
BehloolKhan May 20, 2026
c58e2bf
class definition needed for space ship
BehloolKhan May 20, 2026
3f015bf
solution to exercise 3.5
BehloolKhan May 20, 2026
e033976
solution to exercise 3.6
BehloolKhan May 21, 2026
a1db7c0
solution to exercise 3.7
BehloolKhan May 23, 2026
5ebde14
adding angular acceleration
BehloolKhan May 23, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions chapter3/Exercise_3_1/Exercise_3_1.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
PVector ballOne;
PVector ballTwo;
float angle = 0;


void setup() {

size(640, 360);
ballOne = new PVector(0, -50);
ballTwo = new PVector(0, 50);

}

void draw() {
background(255);
fill(175);
strokeWeight(2);
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+10, ballTwo.x, ballTwo.y-10);
popMatrix();
angle += 0.1;
}
19 changes: 19 additions & 0 deletions chapter3/Exercise_3_2/Exercise_3_2.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Mover ball;
float G = (float) 0.1;

void setup() {
size(640, 360);
ball = new Mover(new PVector(0, height/2), 0.5);
ball.applyForce(new PVector(2.5, -2.5));
}

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();
}
55 changes: 55 additions & 0 deletions chapter3/Exercise_3_2/mover.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class Mover {

PVector location;
PVector acceleration;
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);
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);

aAcceleration = acceleration.y/10.0;
aVelocity += aAcceleration;
aVelocity = constrain(aVelocity,-0.1,0.1);

acceleration.mult(0);

angle += aVelocity;
}

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;
}

}
26 changes: 26 additions & 0 deletions chapter3/Exercise_3_3/Exercise_3_3.pde
Original file line number Diff line number Diff line change
@@ -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));
}
}

}
46 changes: 46 additions & 0 deletions chapter3/Exercise_3_3/mover.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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);
strokeWeight(1);
pushMatrix();
translate(location.x, location.y);
rectMode(CENTER);
rect(0, 0, 20, 20);
fill(0);
strokeWeight(2);
line(0, 0, 4*velocity.x, 4*velocity.y);
popMatrix();
}

public double getMass() {
return mass;
}

}
22 changes: 22 additions & 0 deletions chapter3/Exercise_3_4/Exercise_3_4.pde
Original file line number Diff line number Diff line change
@@ -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;

}
32 changes: 32 additions & 0 deletions chapter3/Exercise_3_5/Exercise_3_5.pde
Original file line number Diff line number Diff line change
@@ -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);
}

}
62 changes: 62 additions & 0 deletions chapter3/Exercise_3_5/spaceShip.pde
Original file line number Diff line number Diff line change
@@ -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);
}

}
25 changes: 25 additions & 0 deletions chapter3/Exercise_3_6/Exercise_3_6.pde
Original file line number Diff line number Diff line change
@@ -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;
}
18 changes: 18 additions & 0 deletions chapter3/Exercise_3_7/Exercise_3_7.pde
Original file line number Diff line number Diff line change
@@ -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();
}
}
41 changes: 41 additions & 0 deletions chapter3/Exercise_3_7/oscillator.pde
Original file line number Diff line number Diff line change
@@ -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();
}
}
Loading