let cpos;
let vel;
let boxbottom = 150+20;
let boxtop = 150-20;
let boxright = 200+90;
let boxleft = 200-90;
function setup() {
createCanvas(400,300);
background(100);
cpos = createVector(0,0);
vel = createVector(1,1);
}
function draw() {
circle(cpos.x, cpos.y, 4);
cpos.add(vel)
if (boxleft < cpos.x && cpos.x < boxright) {
if (boxtop < cpos.y && cpos.y < boxbottom)
{
cpos = getNewStartDirection();
vel.x = cpos.x * -4;
vel.y = cpos.y * -4
cpos.mult(300);
cpos.add([width/2, height/2]);
}
}
}
function getNewStartDirection() {
let unit = createVector(1,0);
unit.rotate(random(2*PI));
return unit
}