_TITLE "Collision Rectangle versus 'Circle', move your mouse!"
' Collision - Test 1 orig by johnno copied and mod b+ 2018-06-10
'
' Bounding Box
'
' 2018-06-10 mod by B+ change for x, y, w, h of images
' by readjusting all the variables and use STEP for box drawing
' Generalize the specific gosub routine from this one app so can reuse IN ANY APP using sprites / tiles / images
' 2019-08-30 rewrite this so mouse box never gets inside the sides of box for maze study
'2020-02-23 mod for circle and rectangle
SCREEN _NEWIMAGE(800, 600, 32) '<<< something more standard center is 400, 300
' sprites / tiles / images are typically referred to by their left top corner ie X, Y and their width and height
'lets do the height and width first
box1Width = 400 '<<< mod add this instead of above
box1Height = 100 '<<< mod add this instead of above
'now center box
box1Left = 400 - box1Width / 2 'same as box1X
'box1Right = 370 '100 width
box1Top = 300 - box1Height / 2 'same as box1Y
' box1Bottom = 290 '100 height
bdx
= 5 * (RND * 2 - 1): bdy
= 5 * (RND * 2 - 1)mouseboxWidth = 40 '<<< mod add these constants for circle radius 25?
mouseboxHeight = 40 '<<< mod add these constants
hey$(0) = "Hey!"
hey$(1) = "I beg your pardon."
hey$(2) = "Bang!"
hey$(3) = "Yikes!"
hey$(4) = "Ouch!"
hey$(5) = "Watch where you are going."
box1Left = box1Left + bdx
IF box1Left
< 0 THEN box1Left
= 0: bdx
= -bdx
box1Top = box1Top + bdy
IF box1Top
< 0 THEN box1Top
= 0: bdy
= -bdy
LINE (box1Left
, box1Top
)-STEP(box1Width
, box1Height
), _RGB32(255, 255, 255), BF
ombx = mouseboxX: omby = mouseboxy 'middle of mouse circle/square
mouseboxX
= _MOUSEX - mouseboxWidth
/ 2 mouseboxy
= _MOUSEY - mouseboxHeight
/ 2
IF collision%
(box1Left
, box1Top
, box1Width
, box1Height
, mouseboxX
, mouseboxy
, mouseboxWidth
, mouseboxHeight
) = 1 THEN mouseboxX = ombx: mouseboxy = omby
lim = 1 'delay when bump to read what the box says
lim = 20
CIRCLE (mouseboxX
+ mouseboxWidth
/ 2, mouseboxy
+ mouseboxHeight
/ 2), 25, &HFFFF8800 LINE (mouseboxX
, mouseboxy
)-STEP(mouseboxWidth
, mouseboxHeight
), _RGB32(255, 128, 0), BF
'<<< use step with width and height _LIMIT 60 'lim '<<< save the fan
FUNCTION collision%
(b1x
, b1y
, b1w
, b1h
, b2x
, b2y
, b2w
, b2h
) ' x, y represent the box left most x and top most y
' w, h represent the box width and height which is the usual way sprites / tiles / images are described
' such that boxbottom = by + bh
' and boxright = bx + bw
'so the specific gosub above is gerealized to a function procedure here!
IF (b1y
+ b1h
< b2y
) OR (b1y
> b2y
+ b2h
) OR (b1x
> b2x
+ b2w
) OR (b1x
+ b1w
< b2x
) THEN collision% = 0
collision% = 1