_TITLE "Collision of Rectangular Images started by Johnno mod b+ 2018-06-10" '^^^^^^^^^^^^^^^^^^^^^^^^ no () needed but must call before SCREEN line around the string title
'
' 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
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 calculation of box1Right
box1Height = 100 '<<< mod add this instead of calculation of box1Bottom
'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
mouseboxWidth = 50 '<<< mod add these constants
mouseboxHeight = 40 '<<< mod add these constants
DIM hey$
(10) 'hey if boxes could talk.... 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."
LINE (box1Left
, box1Top
)-STEP(box1Width
, box1Height
), _RGB32(255, 255, 255), BF
'box2left = _MOUSEX - 50
'box2top = _MOUSEY - 50
'box2right = box2left + 100
'box2bottom = box2top + 100
mouseboxX
= _MOUSEX - mouseboxWidth
/ 2 mouseboxY
= _MOUSEY - mouseboxHeight
/ 2 LINE (mouseboxX
, mouseboxY
)-STEP(mouseboxWidth
, mouseboxHeight
), _RGB32(255, 128, 0), BF
'<<< use step with width and height
'GOSUB collide <<< generalize this to a call to a reuseable routine
IF collision%
(box1Left
, box1Top
, box1Width
, box1Height
, mouseboxX
, mouseboxY
, mouseboxWidth
, mouseboxHeight
) = 1 THEN lim = 1
lim = 50
'collide:
'IF (box1bottom < box2top) OR (box1top > box2bottom) OR (box1left > box2right) OR (box1right < box2left) THEN
' collision = 0
'ELSE
' collision = 1
'END IF
'RETURN
FUNCTION collision%
(b1x
, b1y
, b1w
, b1h
, b2x
, b2y
, b2w
, b2h
) ' yes a type smaller than integer might be used
' 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