November 30, 2020, 02:01:39 AM

### Author Topic: [bb] 2D Rectangle Collide (FAST) by Nigel Brown [ 1+ years ago ]  (Read 534 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] 2D Rectangle Collide (FAST) by Nigel Brown [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : 2D Rectangle Collide (FAST)
Author : Nigel Brown
Posted : 1+ years ago

Description : given any two rectangles x,y's and same width height.

Code :
Code: BlitzBasic
1. Graphics        640,480,16,2
2. SetBuffer       BackBuffer()
3.
4. xpos1% = 100:ypos1% = 100
5. xpos2% = 200:ypos2% = 200
6.
7. width%=10       :height%=10
8.
9.
10. ;---------------------------------------------------------------------
11. Repeat
12. ;---------------------------------------------------------------------
13.
14.         ; Check for move up.
15.         If KeyDown(200)
16.                 ypos1% = ypos1% - 1
17.         EndIf
18.
19.         ; Check for move down.
20.         If KeyDown(208)
21.                 ypos1% = ypos1% + 1
22.         EndIf
23.
24.         ; Check for move left.
25.         If KeyDown(203)
26.                 xpos1% = xpos1% - 1
27.         EndIf
28.
29.         ; Check for move right
30.         If KeyDown(205)
31.                 xpos1% = xpos1% + 1
32.         EndIf
33.
34.         If CheckCollide( xpos1%, ypos1%, xpos2%, ypos2%, width%, height% )
35.                 ClsColor 255,0,0
36.         Else
37.                 ClsColor        0,0,0
38.         EndIf
39.
40.         Cls
41.
42.         Rect    xpos1%,ypos1%,10,10,1
43.         Rect    xpos2%,ypos2%,10,10,1
44.
45.         Flip
46.
47.         Delay 50
48.
49. Until KeyDown(1)
50. End
51.
52.
53. ;---------------------------------------------------------------------
54. Function CheckCollide%( x1%, y1%, x2%, y2%, width%, height% )
55. ;---------------------------------------------------------------------
56.
57.         If Abs( x1% - x2% ) < width% And Abs( y1% - y2% ) < height%  Return True
58.
59.         Return False
60.
61. End Function