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
120 changes: 93 additions & 27 deletions BouncingWithVectors/BouncingWithVectors.pde
Original file line number Diff line number Diff line change
@@ -1,37 +1,103 @@
//declare variables
float x, y, velX, velY, diam;
float gravity_acceleration = 0.98;
float air_resistance = 0.99;

int bnum = 50;

int frame_rate = 60;

//declare a class which contains the ball variables
class Ball{
PVector pos;
PVector vel;

float diam;
float hue;

Ball(){
//initialize variables
pos = new PVector(random(width), random(height));
vel = PVector.random2D();
vel.mult(5);

diam = random(10, 90);
hue = random(255);
}

void run(Ball[] balls){
//bounce off of other balls
for(Ball ball : balls){
if(ball != this && dist(pos.x, pos.y, ball.pos.x, ball.pos.y) < diam/2 + ball.diam/2){
pos.x += ((diam)) * ((pos.x - ball.pos.x)/dist(pos.x, pos.y, ball.pos.x, ball.pos.y) * 0.1);
pos.y += ((diam)) * ((pos.y - ball.pos.y)/dist(pos.x, pos.y, ball.pos.x, ball.pos.y) * 0.1);
vel.x *= -air_resistance;
vel.y *= -air_resistance;
}
}

//bounce ball if it hits walls
if (pos.x + diam/2 >= width) {
vel.x = -abs(vel.x); //if the ball hits the right wall, assign x velocity the negative version of itself
pos.x = width - diam/2;
} else if (pos.x - diam/2 <= 0) {
vel.x = abs(vel.x); //if the ball hits the left wall, assign x velocity the positive version of itself
pos.x = diam/2;
}
if (pos.y + diam/2 >= height) {
vel.y = -abs(vel.y);
pos.y = height - diam/2;
} else if (pos.y - diam/2 <= 0) {
vel.y = abs(vel.y);
pos.y = diam/2;
}

//bounce off of the mouse cursor
if(dist(pos.x, pos.y, mouseX, mouseY) < diam){
vel.y = -10;
}

//simulate air resistance
vel.x *= air_resistance;
vel.y *= air_resistance;

//simulate gravity
vel.y += gravity_acceleration;

//add velocity to position
pos.add(vel);

//increment the hue
hue++;
}
void bdraw(){
//draw ball
fill(hue % 255, 255, 255);
ellipse(pos.x, pos.y, diam, diam);
}
}

Ball[] balls = new Ball[bnum];

void setup() {

frameRate(frame_rate);

colorMode(HSB, 255);

//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);

//initialize all of the balls
for(int i = 0; i < bnum; i++){
balls[i] = new Ball();
}
}

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

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

//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);

for(Ball ball: balls){
ball.run(balls);
ball.bdraw();
}
}
98 changes: 67 additions & 31 deletions Wanderer/Wanderer.pde
Original file line number Diff line number Diff line change
@@ -1,37 +1,73 @@
//declare variables
float x, y, velX, velY, diam;
PImage doge;

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

//initialize variables
x = width/2;
y = height/2;
diam = 80;
velX = random(-5, 5);
velY = random(-5, 5);
}

void draw() {
//draw background to cover previous frame
background(0);
PVector pos, vel, acc;

//draw ball
ellipse(x, y, diam, diam);
int time = 0;

//add velocity to position
x += velX;
y += velY;
void setup(){
doge = loadImage("doge.png");

size(640, 480);

noStroke();

background(0);

pos = new PVector(width/2, height/2);
vel = new PVector(0, 0);
acc = new PVector(0, 0);

textFont(createFont("comicbd.ttf", 16));

colorMode(HSB, 100);
}

//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;
void draw(){
if(time == 0){
vel.mult(0.75);

acc = PVector.random2D();
acc.mult(random(0.25));

time = (int)random(1, 30);

fill(random(100), 255, 255);

switch((int)random(20)){
case 0: text("much wow", random(width), random(height)); break;
case 1: text("many centipde", random(width), random(height)); break;
case 2: text("great amaze", random(width), random(height)); break;
case 3: text("many programming", random(width), random(height)); break;
case 4: text("such binary", random(width), random(height)); break;
case 5: text("many letter", random(width), random(height)); break;
case 6: text("lots of process", random(width), random(height)); break;
case 7: text("much random", random(width), random(height)); break;
case 8: text("so unpredictable", random(width), random(height)); break;
case 9: text("wowe", random(width), random(height)); break;
}
}

time--;

vel.add(acc);
pos.add(vel);

translate(pos.x, pos.y);
scale(0.25, 0.25);
translate(-doge.width/2, -doge.height/2);

image(doge, 0, 0);

resetMatrix();

if(pos.x > width)
pos.x = 0;
if(pos.y > height)
pos.y = 0;
if(pos.x < 0)
pos.x = width;
if(pos.y < 0)
pos.y = height;
}
Binary file added Wanderer/comicbd.ttf
Binary file not shown.
Binary file added Wanderer/doge.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.