xxxxxxxxxx
36
/*
----- Coding Tutorial by Patt Vira -----
Name: Collision Detection (Rect to Rect)
Video Tutorial: https://youtu.be/gUTeWOfwECc?si=c6VZPQRFRp4MMSW4
Connect with Patt: @pattvira
https://www.pattvira.com/
----------------------------------------
*/
let x; let y;
let w = 100; let h = 50;
function setup() {
createCanvas(400, 400);
x = width/2;
y = height/2;
}
function draw() {
background(220);
let rightEdge = x + w;
let leftEdge = x;
let topEdge = y;
let bottomEdge = y + h;
if (rightEdge >= mouseX && leftEdge <= mouseX + w && topEdge <= mouseY + h && bottomEdge >= mouseY) {
fill(255, 255, 0);
} else {
fill(255);
}
rect(x, y, w, h);
fill(255, 0, 0);
rect(mouseX, mouseY, w, h);
}