Collision Detection in 2D Squares
in
Programming Questions
•
3 years ago
Hey all,
Just wondering if anyone can guide with my code I've sort of got it working but it's not perfect I'm trying to make it so the smaller Square is detected as soon as any part of it enters the area of the bigger one but I have no clue how to do that.
any ideas?
thanks so much,
Harrison
code
- float blockX = random(500);
- float blockY = random(500);
- void setup(){
- size(500,500);
- smooth();
- noStroke();
- }
- void draw()
- {
- collide();
- background(200);
- fill(0);
- rect(mouseX,mouseY,20,20);
- fill(0,0,255);
- rect(blockX,blockY,5,5);
- }
- void collide(){
- if(((mouseX + 10 >= blockX)&&(mouseX <= blockX )&&(mouseY + 10 >= blockY)&&(mouseY <= blockY))
- ||((mouseX - 10 >= blockX)&&(mouseX <= blockX )&&(mouseY - 10 >= blockY)&&(mouseY <= blockY))){
- blockX = random(500);
- blockY = random(500);
- }
- }
1