xxxxxxxxxx
var x, y;
var d = 50;
var velocity, angle;
function setup() {
createCanvas(windowWidth, windowHeight);
resetthatstuff();
}
function draw() {
background(0);
fill(255, 128, 0);
ellipse(x, y, d, d);
x = x + velocity * cos(angle);
y = y + velocity * sin(angle);
// boundary checking:
// OPTION #1 : TELEPORT
// if(x>width) x = 0;
// if(x<0) x = width;
// if(y>height) y = 0;
// if(y<0) y = height;
// OPTION #2 : VELOCITY INVERSION
// if(x>width) velocity = -velocity;
// if(x<0) velocity = -velocity;
// if(y>height) velocity = -velocity;
// if(y<0) velocity = -velocity;
// OPTION #3 : ANGULAR REFLECTION
if(x>width) angle = PI - angle;
if(x<0) angle = PI - angle;
if(y>height) angle = TWO_PI - angle;
if(y<0) angle = TWO_PI - angle;
}
function keyTyped()
{
resetthatstuff();
}
function resetthatstuff()
{
x = width/2;
y = height/2;
velocity = random(10, 40); // how fast is the circle moving
angle = random(0, TWO_PI); // random angle
}