Author Topic: collision questions and hard work  (Read 7705 times)

0 Members and 1 Guest are viewing this topic.

Offline codevsb12

  • Newbie
  • Posts: 23
    • View Profile
collision questions and hard work
« on: February 22, 2020, 07:52:18 pm »
hello again! u guys may know me as the newbie of qb64 forums, but honestly, i worked a lot on my game... sort of...

in resume i cant find a good way to collide in objects.

first, i cant go to the qb64 wiki because there's always this message like "bandwidth limit". second, i've found some collision detection methods in a site, but they doesn't fit in my game.

it took me 2 days to finally ask for help from the experienced ones.

here's the code of my sub.

Code: QB64: [Select]
  1. SUB winner
  2.     IF (xp + sx2 = xb) AND (xp = xb + r * 2) AND (yp + sy2 = yb) AND (yp = yb + r * 2) THEN win = 1
  3.  

FellippeHeitor

  • Guest
Re: collision questions and hard work
« Reply #1 on: February 22, 2020, 07:53:09 pm »
You are probably trying to go to the now defunct [abandoned, outdated and now likely malicious qb64 dot net website - don’t go there] site, which was taken over by someone we don't know, for some purpose we still haven't learned.

Fix your bookmarks and visit the right wiki at www.qb64.org/wiki

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: collision questions and hard work
« Reply #2 on: February 22, 2020, 07:56:57 pm »
Do you want the collision of two rectangles or 2 circles?

For circles just find the distance between their centers and if it less than sum of their radii they collided.

Collision of two rectangles is this:
Code: QB64: [Select]
  1. FUNCTION collision% (b1x, b1y, b1w, b1h, b2x, b2y, b2w, b2h)
  2.     ' x, y represent the box left most x and top most y
  3.     ' w, h represent the box width and height which is the usual way sprites / tiles / images are described
  4.     ' such that boxbottom = by + bh
  5.     '        and boxright = bx + bw
  6.  
  7.     IF (b1y + b1h < b2y) OR (b1y > b2y + b2h) OR (b1x > b2x + b2w) OR (b1x + b1w < b2x) THEN
  8.         collision% = 0
  9.     ELSE
  10.         collision% = 1
  11.     END IF
  12.  

Offline codevsb12

  • Newbie
  • Posts: 23
    • View Profile
Re: collision questions and hard work
« Reply #3 on: February 22, 2020, 08:40:38 pm »
uh, no... it's actually a collision between a rectangle AND a circle.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: collision questions and hard work
« Reply #4 on: February 22, 2020, 08:55:43 pm »
uh, no... it's actually a collision between a rectangle AND a circle.

LOL! I'd do two rectangles (specially if the rectangle is long and skinny) and use square sides for circle just slightly less than 2*radius OR get STxAxTIC to work his physics magic. :)
« Last Edit: February 22, 2020, 08:57:16 pm by bplus »

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: collision questions and hard work
« Reply #5 on: February 22, 2020, 08:58:45 pm »
Yeah sooooo... are you looking to simply detect the collision, or are you interested in the actual dynamics that occur when a tennis ball hits a brick?
You're not done when it works, you're done when it's right.

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: collision questions and hard work
« Reply #6 on: February 22, 2020, 09:07:56 pm »
Hey, bplus, you need a transparent PNG for your avatar, otherwise the background colours dont match depending on if your post is odd or even. It can be worked out with
http://www.imagemagick.org/script/command-line-options.php#transparent
but you also have some antialiasing

Offline codevsb12

  • Newbie
  • Posts: 23
    • View Profile
Re: collision questions and hard work
« Reply #7 on: February 23, 2020, 05:37:56 pm »
Yeah sooooo... are you looking to simply detect the collision, or are you interested in the actual dynamics that occur when a tennis ball hits a brick?
i look for simple collision detection. if the player collides with the ball then he wins

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: collision questions and hard work
« Reply #8 on: February 23, 2020, 05:40:21 pm »
Are you ok with surrounding the ball with a box for simplicity's sake? Then its box-on-box, my favorite internet search.
You're not done when it works, you're done when it's right.

Offline codevsb12

  • Newbie
  • Posts: 23
    • View Profile
Re: collision questions and hard work
« Reply #9 on: February 23, 2020, 06:10:41 pm »
i would if the ball wasn't drawn with the CIRCLE statement

just take a look:




oh i dont know how to insert pictures in qb64 forums

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: collision questions and hard work
« Reply #10 on: February 23, 2020, 06:17:56 pm »
No worries, I know what a ball looks like.

I'm just worried that if you want the real thing you won't know what's going on really. A box intersecting a circle can be reduced to solving four problems of a line intersecting a circle, maybe 2 problems...

Hm... Maybe a SCREEN-based method for you...
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: collision questions and hard work
« Reply #11 on: February 23, 2020, 08:25:03 pm »
Code: QB64: [Select]
  1. _TITLE "Collision Rectangle versus 'Circle', move your mouse!"
  2.  
  3. '   Collision - Test 1  orig by johnno copied and mod b+ 2018-06-10
  4. '
  5. '   Bounding Box
  6. '
  7. ' 2018-06-10 mod by B+ change for x, y, w, h of images
  8. ' by readjusting all the variables and use STEP for box drawing
  9. ' Generalize the specific gosub routine from this one app so can reuse IN ANY APP using sprites / tiles / images
  10. ' 2019-08-30 rewrite this so mouse box never gets inside the sides of box for maze study
  11. '2020-02-23 mod for circle and rectangle
  12.  
  13.  
  14. SCREEN _NEWIMAGE(800, 600, 32) '<<< something more standard center is 400, 300
  15.  
  16. ' sprites / tiles / images are typically referred to by their left top corner ie X, Y  and their width and height
  17.  
  18. 'lets do the height and width first
  19. box1Width = 400 '<<< mod add this instead of above
  20. box1Height = 100 '<<< mod add this instead of above
  21.  
  22. 'now center box
  23. box1Left = 400 - box1Width / 2 'same as box1X
  24. 'box1Right = 370 '100 width
  25. box1Top = 300 - box1Height / 2 'same as box1Y
  26. ' box1Bottom = 290 '100 height
  27. bdx = 5 * (RND * 2 - 1): bdy = 5 * (RND * 2 - 1)
  28. mouseboxWidth = 40 '<<< mod add these constants  for circle radius 25?
  29. mouseboxHeight = 40 '<<< mod add these constants
  30.  
  31. f& = _RGB32(255, 255, 255)
  32. b& = _RGB32(0, 0, 0)
  33.  
  34. DIM hey$(10)
  35. hey$(0) = "Hey!"
  36. hey$(1) = "I beg your pardon."
  37. hey$(2) = "Bang!"
  38. hey$(3) = "Yikes!"
  39. hey$(4) = "Ouch!"
  40. hey$(5) = "Watch where you are going."
  41.     k$ = INKEY$
  42.     WHILE _MOUSEINPUT: WEND '<<< this is all the loop needed for mouse input
  43.     CLS
  44.     box1Left = box1Left + bdx
  45.     IF box1Left < 0 THEN box1Left = 0: bdx = -bdx
  46.     IF box1Left + box1Width > _WIDTH THEN box1Left = _WIDTH - box1Width: bdx = -bdx
  47.     box1Top = box1Top + bdy
  48.     IF box1Top < 0 THEN box1Top = 0: bdy = -bdy
  49.     IF box1Top + box1Height > _HEIGHT THEN box1Top = _HEIGHT - box1Height: bdy = -bdy
  50.  
  51.     LINE (box1Left, box1Top)-STEP(box1Width, box1Height), _RGB32(255, 255, 255), BF
  52.  
  53.     ombx = mouseboxX: omby = mouseboxy 'middle of mouse circle/square
  54.  
  55.     mouseboxX = _MOUSEX - mouseboxWidth / 2
  56.     mouseboxy = _MOUSEY - mouseboxHeight / 2
  57.  
  58.     IF collision%(box1Left, box1Top, box1Width, box1Height, mouseboxX, mouseboxy, mouseboxWidth, mouseboxHeight) = 1 THEN
  59.         COLOR _RGB32(100, 0, 85), _RGB32(255, 255, 255)
  60.         r$ = hey$(INT(RND * 6))
  61.         _PRINTSTRING (box1Left + (box1Width - LEN(r$) * 8) / 2, box1Top + 42), r$
  62.         COLOR f&, b&
  63.         mouseboxX = ombx: mouseboxy = omby
  64.         lim = 1 'delay when bump to read what the box says
  65.     ELSE
  66.         lim = 20
  67.     END IF
  68.     CIRCLE (mouseboxX + mouseboxWidth / 2, mouseboxy + mouseboxHeight / 2), 25, &HFFFF8800
  69.     LINE (mouseboxX, mouseboxy)-STEP(mouseboxWidth, mouseboxHeight), _RGB32(255, 128, 0), BF '<<< use step with width and height
  70.     _DISPLAY
  71.     _LIMIT 60 'lim '<<< save the fan
  72. LOOP UNTIL k$ = CHR$(27)
  73.  
  74. FUNCTION collision% (b1x, b1y, b1w, b1h, b2x, b2y, b2w, b2h)
  75.     ' x, y represent the box left most x and top most y
  76.     ' w, h represent the box width and height which is the usual way sprites / tiles / images are described
  77.     ' such that boxbottom = by + bh
  78.     '        and boxright = bx + bw
  79.     'so the specific gosub above is gerealized to a function procedure here!
  80.     IF (b1y + b1h < b2y) OR (b1y > b2y + b2h) OR (b1x > b2x + b2w) OR (b1x + b1w < b2x) THEN
  81.         collision% = 0
  82.     ELSE
  83.         collision% = 1
  84.     END IF
  85.  
  86.  

Offline codevsb12

  • Newbie
  • Posts: 23
    • View Profile
Re: collision questions and hard work
« Reply #12 on: February 26, 2020, 07:49:41 pm »
oh, so thats how functions work... anyway

now i need to add the spikes, and make them being generated randomly in the edges of the frame. i've heard of this function called _MAPTRIANGLE and i wondered: hmm... my spike image iis in the form of a triangle, so maybe i can use this statement to rotate it....

am i right?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: collision questions and hard work
« Reply #13 on: February 26, 2020, 08:32:23 pm »
Yeah _MAPTRIANGLE works great, one of first things I learned at QB64 forums, cool way to fill a triangle check SUB ftri
here: https://www.qb64.org/forum/index.php?topic=1511.msg111300#msg111300


spikey demo
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. FOR y = 0 TO _HEIGHT STEP 6
  3.     x = 0
  4.     x2 = 10
  5.     ftri x, y, x2, y + 3, x, y + 6, &HFFFF0000
  6.  
  7. SUB ftri (x1, y1, x2, y2, x3, y3, K AS _UNSIGNED LONG)
  8.     STATIC a&
  9.     D = _DEST
  10.     IF a& = 0 THEN a& = _NEWIMAGE(1, 1, 32)
  11.     _DEST a&
  12.     PSET (0, 0), K
  13.     _DEST D
  14.     _MAPTRIANGLE _SEAMLESS(0, 0)-(0, 0)-(0, 0), a& TO(x1, y1)-(x2, y2)-(x3, y3)
  15.  
  16.  
  17.  
« Last Edit: February 26, 2020, 08:40:45 pm by bplus »

Offline codevsb12

  • Newbie
  • Posts: 23
    • View Profile
Re: collision questions and hard work
« Reply #14 on: February 26, 2020, 08:50:40 pm »
that was a good code, and the link's code was amazing! but i meant a image, y'know, a bmp file containing a gray spike on it.