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
60 changes: 36 additions & 24 deletions BouncingWithVectors/BouncingWithVectors.pde
Original file line number Diff line number Diff line change
@@ -1,37 +1,49 @@
int count = 80; //makes 60 balls

//declare variables
float x, y, velX, velY, diam;

float [] diam = new float [count]; //diameter


//float loc.x, loc.y
PVector [] loc = new PVector [count];
PVector [] vel = new PVector [count]; // replaces x,y, vel[i]x, vel[i]y,


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);
for (int i = 0; i < count; i ++) { //for arrays
//initialize variables
loc [i] = new PVector (random(350,450), height/2); //starts ellipse off in a random position from 350-450, middle of screen
vel[i] = PVector.random2D(); //gives speed magnitude of one
vel[i].mult(random(5,8)); //increased speed
diam [i] = random(3,88); //diameter
}
}

void draw() {
//draw background to cover previous frame
background(0);
for (int i = 0; i < count; i ++) { //for arrays
strokeWeight(3); // increased stroke weight
stroke(random(206), random(235), random(245)); //gives random stroke
//draw ball
ellipse(loc[i].x, loc[i].y, diam[i], diam[i]);

//draw ball
ellipse(x, y, diam, diam);
//add vel[i]ocitloc.y to position
loc[i].add(vel[i]);

//add velocity to position
x += velX;
y += velY;

//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 (y + diam/2 >= height) {
velY = -abs(velY);
} else if (y - diam/2 <= 0) {
velY = abs(velY);
//bounce ball if it hits walls
if (loc[i].x + diam[i]/2 >= width) {
vel[i].x = -abs(vel[i].x); //if the ball hits the right wall, assign loc.x vel[i]ocitloc.y the negative version of itself
} else if (loc[i].x - diam[i]/2 <= 0) {
vel[i].x = abs(vel[i].x); //if the ball hits the left wall, assign loc.x vel[i]ocitloc.y the positive version of itself
}
if (loc[i].y + diam[i]/2 >= height) {
vel[i].y = -abs(vel[i].y);
} else if (loc[i].y - diam[i]/2 <= 0) {
vel[i].y = abs(vel[i].y);
}
}
}
66 changes: 41 additions & 25 deletions Wanderer/Wanderer.pde
Original file line number Diff line number Diff line change
@@ -1,37 +1,53 @@
int count = 8;

//declare variables
float x, y, velX, velY, diam;
PVector [] loc = new PVector [count];
PVector [] vel = new PVector [count];
PVector [] a = new PVector [count];
float [] diam = new float [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);
for (int i = 0; i < count; i++) {
//initialize variables
loc [i] = new PVector (random(width), random(height));
diam [i] = random(10, 80);
vel[i] = PVector.random2D();
vel[i].mult(1);

}
}

void draw() {
//draw background to cover previous frame
background(0);
for (int i = 0; i < count; i ++) {
println("x" + i + " = " + loc[i].x + ", y" + i + " = " + loc[i].y);
//draw background to cover previous frame
background(204, 240, 229);
stroke(random(237), random(250), random(246));
strokeWeight(3);

//draw ball
ellipse(x, y, diam, diam);
//draw ball
ellipse(loc[i].x, loc[i].y, diam[i], diam[i]);
a[i] = PVector.random2D();
a[i].mult(0.1);

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

//wrap the ball's position
if (x + diam/2 >= width) {
x = -diam/2;
} else if (x - diam/2 <= 0) {
x = width + diam/2;
}
if (y + diam/2 >= height) {
y = -diam/2;
} else if (y - diam/2 <= 0) {
y = height + diam/2;

//wrap the ball's position
if (loc[i].x >= width) {
loc[i].x = 0;
} else if (loc[i].x <= 0) {
loc[i].x = width ;
}
if (loc[i].y >= height) {
loc[i].y = 0;
} else if (loc[i].y <= 0) {
loc[i].y = height ;
}
}
}