Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
37 changes: 19 additions & 18 deletions BouncingWithVectors/BouncingWithVectors.pde
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
//declare variables
float x, y, velX, velY, diam;
float diam;
PVector loc; //declare loc, replaces x and y
PVector vel; //declare vel, replaces velx and vely

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 = PVector.random2D(); //random vector - speed of ball stays the same while direction changes
vel.mult(10); //multiply vector by 10 - random direction with magnitude of 10
diam = 80;
}

void draw() {
//draw background to cover previous frame
background(0);

//draw ball
ellipse(x, y, diam, diam);
ellipse(loc.x, loc.y, diam, diam); //loc.x is the x component of loc

//add velocity to position
x += velX;
y += velY;
//add velocity to position using vector addition - add vel vector to loc vector
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) {
vel.x = -abs(vel.x); //if the ball hits the right wall, assign x velocity the negative version of itself
} else if (loc.x - diam/2 <= 0) {
vel.x = abs(vel.x); //if the ball hits the left wall, assign x velocity the positive version of itself
}
if (y + diam/2 >= height) {
velY = -abs(velY);
} else if (y - diam/2 <= 0) {
velY = abs(velY);
if (loc.y + diam/2 >= height) {
vel.y = -abs(vel.y);
} else if (loc.y - diam/2 <= 0) {
vel.y = abs(vel.y);
}
}
65 changes: 46 additions & 19 deletions Wanderer/Wanderer.pde
Original file line number Diff line number Diff line change
@@ -1,37 +1,64 @@
//declare variables
float x, y, velX, velY, diam;

int count=30;
float[] diam = new float[count];
PVector[] loc = new PVector[count];
PVector[] vel = new PVector[count];
PVector[]acc = new PVector[count];


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);
int i=0;
while(i<count){
//initialize PVectors
loc[i] = new PVector(width/2, height/2);
vel[i] = new PVector (0,0);
acc[i] = PVector.random2D();
acc[i].mult(0.1);
diam[i] = 80;
i++;
}
}

void draw() {
int i=0;
while(i<count){

acc[i] = PVector.random2D();

//draw background to cover previous frame
background(0);

//draw ball
ellipse(x, y, diam, diam);
ellipse(loc[i].x, loc[i].y, diam[i], diam[i]);
ellipse(mouseX, mouseY, 30, 30);
//multiply acc by 0.1
acc[i].mult(0.1);


//add acc to vel
vel[i].add(acc[i]);

//magnitude of vel cannot be greater than 1
vel[i].limit(1);

//add velocity to position
x += velX;
y += velY;
loc[i].add(vel[i]);

//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[i].x >= width) {
loc[i].x = -loc[i].x;
} else if (loc[i].x <= 0) {
loc[i].x = -loc[i].x;
}
if (y + diam/2 >= height) {
y = -diam/2;
} else if (y - diam/2 <= 0) {
y = height + diam/2;
if (loc[i].y+diam[i]/2 >= height) {
loc[i].y = -loc[i].y;
} else if (loc[i].y+diam[i]/2 <= 0) {
loc[i].y = -loc[i].y;
}

i++;
}
}