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
35 changes: 19 additions & 16 deletions BouncingWithVectors/BouncingWithVectors.pde
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
//declare variables
float x, y, velX, velY, diam;
float diam;

PVector loc; //declared loc, replaces x & y
PVector vel; //delcared vel, replaced velx & vel y

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

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

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

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

//add velocity to position
x += velX;
y += velY;
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);

}
}
57 changes: 41 additions & 16 deletions Wanderer/Wanderer.pde
Original file line number Diff line number Diff line change
@@ -1,37 +1,62 @@
//declare variables
float x, y, velX, velY, diam;
float diam;

PVector loc; //declared loc
PVector vel; //declared vel
PVector acc; //declared acc; acceleration

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

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

void draw() {
//draw background to cover previous frame
acc = PVector.random2D(); //initialize acceleration
acc.mult(.5); //gradually increase velocity with acceleration
background(0);

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

//add acceleration to velocity
vel.add(acc);

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

//Limit velocity
vel.limit(5);


//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 (loc.y >= height) {
loc.y = 0;
} else if (loc.y <= 0) {
loc.y = height;
}
if(loc.x >= width/2);{
fill(0,0,255); //blue on right side
}
if(loc.x <= width/2){
fill(255,0,0); //red on left side
}
if(loc.y <= height/2){
text("Up is Yup", width/2, 450);
}
if (y + diam/2 >= height) {
y = -diam/2;
} else if (y - diam/2 <= 0) {
y = height + diam/2;
if(loc.y >= height/2){
text("Down is Bound", width/2, 150);
}

}