09-07-2025, 08:48 AM
Let me showcase a very simple collision detection routine that will work with all shapes and sizes of images:
Now notice that I'm being lazy here and just making my "images" to be plain squares of 64x64 pixels (like a lot of sprites used in sprite sheets). This ***DOES NOT*** do square detection or rectangle detection or any real math at all. Like I mentioned on a different post about this, this is simply tossing paint at the wall and looking for colors that blend together.
Yellow + Blue = Green type logic.
Even using POINT, as you can see, this has no problems running and maintaining a nice fast animation speed for us. If you truly need, you can easily swap in a _MEM check here to speed it up even faster, but the overall process is really quite simple and efficient. Toss your images on a background screen (I'm doing all this on the main screen so you can watch it in real time as it happens), read that screen once and look for the existence of any collision colors.
Simple. Direct. Efficient. What's not to love about it, and no math is involved whatsoever!
Code: (Select All)
Dim As _Unsigned Long BSC, BJC, SJC, BSJC, CP 'bob+ sam collision, bob + joe collision, ect
$Console
Randomize Timer
Display = _NewImage(800, 600, 32)
Screen Display
_Console On
Bob = _NewImage(64, 64, 32)
Sam = _NewImage(64, 64, 32)
Joe = _NewImage(64, 64, 32)
Cls , _RGBA(255, 0, 0, 128), Bob
Cls , _RGBA(0, 255, 0, 128), Sam
Cls , _RGBA(0, 0, 255, 128), Joe
CollisionScreen = _NewImage(64, 64, 32)
_Source CollisionScreen 'to read the blended colors on that screen
_Dest CollisionScreen
_PutImage , Bob 'put bob on the screen
_PutImage , Sam 'put sam on the screen
BSC = Point(0, 0) 'we now know the blended color for a bob + sam collision
_PutImage , Joe 'put joe on the screen
BSJC = Point(0, 0) 'we now know the blended color for a bob + sam + joe collision
Cls , 0 'clear the screen we use for blending
_PutImage , Bob 'bob back on the screen
_PutImage , Joe 'joe on the screen
BJC = Point(0, 0) 'and now we know the blended color for bob + joe collison
Cls , 0 'clear the screen one last time
_PutImage , Sam 'put Sam on the screen
_PutImage , Joe 'put Joe on the screen
SJC = Point(0, 0) 'and now we know the blended color for a sam + joe collision
_Source Display 'restore the source to original screen
_Dest Display
_FreeImage CollisionScreen 'free that screen as we're done getting our blended colors
Dim As Long x(2), y(2)
For i = 0 To 2
x(i) = Rnd * 700: y(i) = Rnd * 500 'random staring point of a character on the screen
Next
For i = 0 To 2
Do Until xmove(i) <> 0: xmove(i) = Rnd * 10 - 5: Loop
Do Until ymove(i) <> 0: ymove(i) = Rnd * 10 - 5: Loop
Next
Do
Cls , 0 'clear the screen
For i = 0 To 2 'generate movement
If x(i) + xmove(i) >= 800 - 64 Then xmove(i) = -xmove(i)
If x(i) + xmove(i) <= 0 Then xmove(i) = -xmove(i)
If y(i) + ymove(i) >= 600 - 64 Then ymove(i) = -ymove(i)
If y(i) + ymove(i) <= 0 Then ymove(i) = -ymove(i)
x(i) = x(i) + xmove(i)
y(i) = y(i) + ymove(i)
Next
_PutImage (x(0), y(0)), Bob
_PutImage (x(1), y(1)), Sam
_PutImage (x(2), y(2)), Joe
'now do a quick collision detection check
BSCE = 0: BSJCE = 0: SJCE = 0: BJCE = 0
For x = 0 To 799: For y = 0 To 679 'detect collisions
Select Case Point(x, y)
Case BSC: BSCE = -1
Case BSJC: BSJCE = -1
Case SJC: SJCE = -1
Case BJC: BJCE = -1
End Select
Next y, x
If BSCE Then _Echo "Bob and Sam Collided!" 'report collisions
If BSJCE Then _Echo "All three collided!"
If SJCE Then _Echo "Sam and Joe Collided!"
If BJCE Then _Echo "Bob and Joe Collided!"
_Limit 120
_Display
Loop Until _KeyHit
System
Now notice that I'm being lazy here and just making my "images" to be plain squares of 64x64 pixels (like a lot of sprites used in sprite sheets). This ***DOES NOT*** do square detection or rectangle detection or any real math at all. Like I mentioned on a different post about this, this is simply tossing paint at the wall and looking for colors that blend together.
Yellow + Blue = Green type logic.
Even using POINT, as you can see, this has no problems running and maintaining a nice fast animation speed for us. If you truly need, you can easily swap in a _MEM check here to speed it up even faster, but the overall process is really quite simple and efficient. Toss your images on a background screen (I'm doing all this on the main screen so you can watch it in real time as it happens), read that screen once and look for the existence of any collision colors.
Simple. Direct. Efficient. What's not to love about it, and no math is involved whatsoever!

