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
56 changes: 35 additions & 21 deletions BouncingWithVectors/BouncingWithVectors.pde
Original file line number Diff line number Diff line change
@@ -1,37 +1,51 @@
//declare variables
float x, y, velX, velY, diam;
int count = 20;
float diam ;
//float loc.x,loc.y
PVector[] loc = new PVector[count]; //replaces loc.x & loc.y

//float vel.loc.x, vel.loc.y
PVector[] vel = new PVector[count]; //replaces vel.loc.x & vel.loc.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);
//loc.x = width/2
//loc.y = height/2
for (int i = 0; i<count; i++) {
loc[i]= new PVector(width/2, height/2);
diam = 80;
//vel.loc.x= random(-5,5)
//vel.loc.y= random(-5,5)
vel[i] = PVector.random2D();
}
}

void draw() {
//draw background to cover previous frame
background(0);
for (int i =0; i<count; i++) {

//draw ball
ellipse(x, y, diam, diam);
//draw ball
ellipse(loc[i].x, loc[i].y, diam, diam);

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

//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/2 >= width) {
vel[i].x = -abs(vel[i].x); //if the ball hits the right wall, assign loc.x velocityloc.y the negative version of itself
} else if (loc[i].x - diam/2 <= 0) {
vel[i].x = abs(vel[i].x); //if the ball hits the left wall, assign loc.x velocityloc.y the positive version of itself
}
if (loc[i].y + diam/2 >= height) {
vel[i].y = -abs(vel[i].y);
} else if (loc[i].y - diam/2 <= 0) {
vel[i].y = abs(vel[i].y);
}
}
}
57 changes: 36 additions & 21 deletions Wanderer/Wanderer.pde
Original file line number Diff line number Diff line change
@@ -1,37 +1,52 @@
//declare variables
float x, y, velX, velY, diam;
//float x, y, velX, velY, diam;
float diam;

PVector loc, vel, acc;

void setup() {
//set size of canvas
size(800, 600);

loc= new PVector(width/2, height/2);
//initialize variables
x = width/2;
y = height/2;
diam = 80;
velX = random(-5, 5);
velY = random(-5, 5);
//x = width/2;
//y = height/2;
diam = 73;
vel = PVector.random2D();
//vel.mult(0);
acc = PVector.random2D();
acc.mult(.05);


}

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

background(19,24,51);

//add here for ellipse to wander
acc = PVector.random2D();
acc.mult(.099);
//draw ball
ellipse(x, y, diam, diam);

vel.mult(1);

ellipse(loc.x, loc.y, diam, diam);
fill(232,239,144);
vel.add(acc);
vel.limit(15); //limit v to avoid it from overcoming large velocities

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

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