really kicking myself for doing this instead of sleeping, but sometimes i just get obsessed! lucky for you.
Code:
PVector y_axis;
Ball ball_1, ball_2;
void setup()
{
size(800,800);
smooth();
ball_1 = new Ball(color(255,0,0),50,100, random(-5,5), random(-5,5), 20);
ball_2 = new Ball(color(0,0,255),200,200,random(-5,5), random(-5,5), 15);
y_axis = new PVector(0,1);
}
void draw()
{
background(0);
smooth();
ball_1.update();
ball_2.update();
ball_1.drawBall();
ball_2.drawBall();
}
class Ball
{
private color ball_color;
private PVector position, velocity;
private int radius;
private float angle;
Ball(color ball_color_, float x, float y, float x_v, float y_v, int radius_){
position = new PVector(x,y);
velocity = new PVector(x_v,y_v);
ball_color = ball_color_;
radius = radius_;
}
void update(){
position.add(velocity);
if(position.x < 0)
position.x = width;
if(position.x > width)
position.x = 0;
if (position.y < 0)
position.y = height;
if(position.y > height)
position.y = 0;
velocity.limit(velocity.mag()*.99);//friction
}
void up() {
velocity.x += cos(angle);
velocity.y += sin(angle);
velocity.limit(10);
}
void down() {
velocity.x -= cos(angle);
velocity.y -= sin(angle);
velocity.limit(10);
}
void left() {
angle -= .1;
}
void right() {
angle += .1;
}
/*PVector extend() {
}*/
void drawBall() {
fill(ball_color);
ellipse(position.x, position.y, radius, radius);
stroke(255);
line(position.x, position.y, position.x + cos(angle)*30, position.y + sin(angle)*30);
noStroke();
}
}
void keyPressed() {
switch (key) {
case CODED:
switch (keyCode) {
case UP:
ball_1.up();
break;
case DOWN:
ball_1.down();
break;
case LEFT:
ball_1.left();
break;
case RIGHT:
ball_1.right();
break;
}
break;
case 'w':
ball_2.up();
break;
case 's':
ball_2.down();
break;
case 'a':
ball_2.left();
break;
case 'd':
ball_2.right();
break;
}
}
puts all that stuff together. please don't just submit my code as your homework, but OWN it by understanding it thoroughly and modifying it. enjoy!