I have one problem at the line 20-23. What I need is when the blue box touches the red rectangle, the blue box gets resetted (it goes back to where it starts). However, the problem is, it doesn't touch the red rectangle area properly. Any suggestions? Thanks in advance.
float moveX, moveY;
float speed = 2;
float controlX, controlY;
int square = 50;
float rectHori = 120;
float rectVert = 20;
void setup() {
size(400, 400);
background(255);
moveX = width/2;
moveY = 50;
reset();
}
void draw() {
background(255);
rectMove();
rectControl();
if (dist(controlX, controlY, moveX, moveY) <= rectHori/2 + rectVert/2) // THIS METHOD IS NOT WORKING!
reset();
}
void rectMove() {
rectMode(CENTER);
noStroke();
fill(255,0,0);
rect(moveX, moveY, rectHori, rectVert);
moveX += speed;
if (moveX <= square/2 || moveX >= width-square/2)
speed *= -1;
}
void rectControl() {
fill(0,0,255);
rect(controlX, controlY, square, square);
}
void reset() {
controlX = width/2;
controlY = height-100;
}
void keyPressed() {
if (keyCode == RIGHT)
controlX += 10;
else if (keyCode == LEFT)
controlX -= 10;
else if (keyCode == DOWN)
controlY += 10;
else if (keyCode == UP)
controlY -= 10;
}