Author Topic: Collide Circle and Box  (Read 2648 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Collide Circle and Box
« on: February 10, 2021, 01:23:37 am »
This is like the Game Operation get mouse from one side to other wo a squeak:
Code: QB64: [Select]
  1. _Title "Collide Circle and Box" 'b+ 2021-02-09
  2. ' Johnno submits ref:  https://www.qb64.org/forum/index.php?topic=3499.msg129726#msg129726
  3. Const True = -1, False = Not True
  4. Type box
  5.     x As Single
  6.     y As Single
  7.     w As Single
  8.     h As Single
  9. Screen _NewImage(1024, 700, 32)
  10. _Delay .25
  11. 'Print True, False
  12. ReDim b(1 To 50) As box
  13. For i = 1 To 50
  14.     b(i).x = Rnd * 1024
  15.     b(i).y = Rnd * 700
  16.     b(i).w = Rnd * 400 + 20
  17.     b(i).h = Rnd * 50 + 5
  18. mr = 5
  19.     Cls
  20.     mx = _MouseX: my = _MouseY
  21.     Circle (mx, my), mr, &HFF0000FF
  22.     For i = 1 To 50
  23.         Line (b(i).x, b(i).y)-Step(b(i).w, b(i).h), _RGB32(i * 5, -1 * (i < 25) + 128 + (i > 25), 255 - i * 5), B
  24.         If collide(mx, my, mr, b(i).x, b(i).y, b(i).w, b(i).h) Then Beep
  25.     Next
  26.     _Display
  27.     _Limit 60
  28.  
  29. Function collide (cx, cy, radius, rx, ry, rw, rh)
  30.     distX = Abs(cx - rx - rw / 2)
  31.     distY = Abs(cy - ry - rh / 2)
  32.  
  33.     If distX > (rw / 2 + radius) Then
  34.         collide = False: Exit Function
  35.     End If
  36.     If distY > (rh / 2 + radius) Then
  37.         collide = False: Exit Function
  38.     End If
  39.  
  40.     If distX <= rw / 2 Then
  41.         collide = True: Exit Function
  42.     End If
  43.     If distY < rh / 2 Then
  44.         collide = True: Exit Function
  45.     End If
  46.  
  47.     'still here?
  48.     dx = distX - rw / 2
  49.     dy = distY - rh / 2
  50.  
  51.     collide = (dx * dx + dy * dy <= (radius * radius))
  52.  
  53.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Collide Circle and Box
« Reply #1 on: February 10, 2021, 01:26:39 am »
Press spacebar to change scenery:
Code: QB64: [Select]
  1. _Title "Collide Circle and Box" 'b+ 2021-02-09
  2. ' Johnno submits ref:  https://www.qb64.org/forum/index.php?topic=3499.msg129726#msg129726
  3. Const True = -1, False = Not True
  4. Type box
  5.     x As Single
  6.     y As Single
  7.     w As Single
  8.     h As Single
  9. Screen _NewImage(1024, 700, 32)
  10. _Delay .25
  11. 'Print True, False
  12. ReDim b(1 To 50) As box
  13. For i = 1 To 50
  14.     b(i).x = Rnd * 1024
  15.     b(i).y = Rnd * 700
  16.     b(i).w = Rnd * 400 + 20
  17.     b(i).h = Rnd * 50 + 5
  18. mr = 5
  19.     Cls
  20.     mx = _MouseX: my = _MouseY
  21.     Circle (mx, my), mr, &HFF0000FF
  22.     For i = 1 To 50
  23.         Line (b(i).x, b(i).y)-Step(b(i).w, b(i).h), _RGB32(i * 5, -1 * (i < 25) + 128 + (i > 25), 255 - i * 5), B
  24.         If collide(mx, my, mr, b(i).x, b(i).y, b(i).w, b(i).h) Then Beep
  25.     Next
  26.     If Len(InKey$) Then
  27.         For i = 1 To 50
  28.             b(i).x = Rnd * 1024
  29.             b(i).y = Rnd * 700
  30.             b(i).w = Rnd * 400 + 20
  31.             b(i).h = Rnd * 50 + 5
  32.         Next
  33.     End If
  34.     _Display
  35.     _Limit 60
  36.  
  37. Function collide (cx, cy, radius, rx, ry, rw, rh)
  38.     distX = Abs(cx - rx - rw / 2)
  39.     distY = Abs(cy - ry - rh / 2)
  40.  
  41.     If distX > (rw / 2 + radius) Then
  42.         collide = False: Exit Function
  43.     End If
  44.     If distY > (rh / 2 + radius) Then
  45.         collide = False: Exit Function
  46.     End If
  47.  
  48.     If distX <= rw / 2 Then
  49.         collide = True: Exit Function
  50.     End If
  51.     If distY < rh / 2 Then
  52.         collide = True: Exit Function
  53.     End If
  54.  
  55.     'still here?
  56.     dx = distX - rw / 2
  57.     dy = distY - rh / 2
  58.  
  59.     collide = (dx * dx + dy * dy <= (radius * radius))
  60.  
  61.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Collide Circle and Box
« Reply #2 on: February 10, 2021, 01:29:09 am »
Fix colors
Code: QB64: [Select]
  1. _Title "Collide Circle and Box" 'b+ 2021-02-09
  2. ' Johnno submits ref:  https://www.qb64.org/forum/index.php?topic=3499.msg129726#msg129726
  3. Const True = -1, False = Not True
  4. Type box
  5.     x As Single
  6.     y As Single
  7.     w As Single
  8.     h As Single
  9. Screen _NewImage(1024, 700, 32)
  10. _Delay .25
  11. 'Print True, False
  12. ReDim b(1 To 50) As box
  13. For i = 1 To 50
  14.     b(i).x = Rnd * 1024
  15.     b(i).y = Rnd * 700
  16.     b(i).w = Rnd * 400 + 20
  17.     b(i).h = Rnd * 50 + 5
  18. mr = 5
  19.     Cls
  20.     mx = _MouseX: my = _MouseY
  21.     Circle (mx, my), mr, &HFF0000FF
  22.     For i = 1 To 50
  23.         Line (b(i).x, b(i).y)-Step(b(i).w, b(i).h), _RGB32(i * 5, -50 * (i < 25) + 128 + 50 * (i > 25), 255 - i * 5), B
  24.         If collide(mx, my, mr, b(i).x, b(i).y, b(i).w, b(i).h) Then Beep
  25.     Next
  26.     If Len(InKey$) Then
  27.         For i = 1 To 50
  28.             b(i).x = Rnd * 1024
  29.             b(i).y = Rnd * 700
  30.             b(i).w = Rnd * 400 + 20
  31.             b(i).h = Rnd * 50 + 5
  32.         Next
  33.     End If
  34.     _Display
  35.     _Limit 60
  36.  
  37. Function collide (cx, cy, radius, rx, ry, rw, rh)
  38.     distX = Abs(cx - rx - rw / 2)
  39.     distY = Abs(cy - ry - rh / 2)
  40.  
  41.     If distX > (rw / 2 + radius) Then
  42.         collide = False: Exit Function
  43.     End If
  44.     If distY > (rh / 2 + radius) Then
  45.         collide = False: Exit Function
  46.     End If
  47.  
  48.     If distX <= rw / 2 Then
  49.         collide = True: Exit Function
  50.     End If
  51.     If distY < rh / 2 Then
  52.         collide = True: Exit Function
  53.     End If
  54.  
  55.     'still here?
  56.     dx = distX - rw / 2
  57.     dy = distY - rh / 2
  58.  
  59.     collide = (dx * dx + dy * dy <= (radius * radius))


Dang I thought I was in the Programs Board, sorry. :p
« Last Edit: February 10, 2021, 01:32:30 am by bplus »

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Collide Circle and Box
« Reply #3 on: February 10, 2021, 12:35:22 pm »
Nice little snippet for learning and building on to make a new game.  Easy to comprehend.  Thanks for sharing.

- Dav