Login

2D collision detection

Kohaku's picture
rem Standard Setup Code
sync on : sync rate 60
set text font "arial" : set text size 12 : set text transparent
 
box1width = 50
box1height = 50
 
box2x = 220
box2y = 220
box2width = 300
box2height = 100
 
do
	cls
	box mousex(), mousey(), mousex()+box1width, mousey()+box1height
 
	box box2x, box2y, box2x+box2width, box2y+box2height
 
	if Col2D(mousex(), mousey(), box1width, box1height, box2x, box2y, box2width, box2height)
		text 10, 10, "collision!"
	endif
 
	sync
loop
 
function Col2D(x1, y1, w1, h1, x2, y2, w2, h2)
	if (x1+w1) > (x2) and (x1) < (x2+w2)
		if (y1+h1) > (y2) and (y1) < (y2+h2)
			exitfunction 1
		endif
	endif
endfunction 0
No votes yet