![]() |
|
error doing image collision detection with _MemGet - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3) +---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10) +---- Thread: error doing image collision detection with _MemGet (/showthread.php?tid=3910) |
RE: error doing image collision detection with _MemGet - Herve - 09-25-2025 Oh no — it’s not just tinkering, it’s also an attempt to improve performance. Your approach of searching for common pixels in the intersection of bounding boxes is effective, but I face a rapid drop in FPS (17 fps / 40 spiders). In the program from post #21 it’s an equivalent technique plus a zoning system to avoid testing all pairs of bounding boxes. FPS is a bit better (from 60 fps / 40 shapes to 17 fps / 130 shapes) but I don’t find that satisfactory. In the program from post #29 I abandon the search for common pixels but keep the zoning technique and add polygon intersection testing. It’s much more complicated to set up, but we get much better FPS (60 fps / 120 shapes), although a few detection bugs remain. In any case, there’s one thing I completely agree with you on: DIY is fun and I’m not about to stop
RE: error doing image collision detection with _MemGet - madscijr - 09-25-2025 I know I kind of started this craziness asking questions and trying some crazy methods, but it turns out I got nice fast and accurate detection by pre-populating a 2D bitmask for each object using a basic 2D boolean (_BYTE) array, and checking for simple bounding box overlap, and if yes, find pixel overlap by comparing the bitmask arrays. You can avoid having to check blank rows/columns around the edges by doing a simple edge detect and saving offsets for top / bottom / left / right. I had a demo in the works which turned into a game, which delayed my posting the demo. Things got hella busy, so I haven't had a chance to try these demos y'all have posted. I should get around to running your code and also have something to share in the next couple of days. RE: error doing image collision detection with _MemGet - madscijr - 09-27-2025 Here's my collision code - see attached "MultiCustomFontsAndCollisions131.bas" for the full working program. To run around and shoot things, use the cursor keys and the right Ctrl key. Also it breaks if you choose both the max robots AND max big robots. If you want to see a lot of things moving and colliding, choose 127 robots and 3 or 4 big robots. First I'll explain that the graphics / sprites / etc. are all done with custom fixed-width fonts, drawn to the screen with _PrintString. It's possible to do "multicolor" sprites by layering 2 or more characters, so you will see the sprite type has members fgChar, bgChar, fgColor, bgColor, although this demo only really uses fgChar for single color "sprites". All graphics (fonts) are defined in text (makes it easy to edit without ever leaving the code) and are used to generate BDF font files on the fly everytime the program runs (this can be easily changed to only create the files if they're not found in the program folder, but I've been constantly editing and adding graphics so it's just easier to have it regenerate them, and it's fast enough that it doesn't cause any noticeable extra delay when the program starts). To see the actual graphic definitons for the fonts, search in the code for these routines: Code: (Select All)
Next, here are a couple UDTs and array definitions the collision detection uses: Code: (Select All)
This routine pre-populates the bit mask array for all characters/fonts that gets compared to detect overlapping pixels: Code: (Select All)
And finally, the collision detection routine: Code: (Select All)
Note that I also started playing with shrinking the boundary box by storing the edges for each character (see Sub FindEdges) but I realized it was redundant as GetBitMask was already storing them in x1, x2, y1, y2, and I saw it would be unwieldy to have to keep recalculating, so in the next version I started developing a 2nd version of GetBitMask that generates the bit mask and finds edges for each unique fgChar / bgChar combination. But I didn't need it for this game, and so it's neither here nor there. The point is that when I get to it, it will use the precalculated edges for each character, for the boundary boxes, and thus avoid having to compare empty rows / columns. Anyway, I had this checking collisions for a few hundred sprites ranging in size from 8x8 upto 64x64 and it was plenty fast. Attached also is "MultiCustomFontsAndCollisions144.bas" which is the updated version with more developed logic for shooting and players/robots/bullets being shot, and upto 16 players on the screen, where I started adding the updated "find edges" and collision routines for sprites using both fgChar and bgChar. The current part I'm stuck at is that when a robot gets shot, it's supposed to explode, but that animation isn't displaying. I have similar animation when the player gets shot, and that's visible, so I'm not sure what's going on. Also there's a weird bug that happens every so often, where if a player gets shot by another player's bullet, ALL the players die. Very weird. Try running it and let me know what you think. RE: error doing image collision detection with _MemGet - madscijr - 09-27-2025 And here are some screenshots... "MultiCustomFontsAndCollisions131.bas" selecting options: "MultiCustomFontsAndCollisions131.bas" in action. Currently it draws each sprite's array index next to the given sprite, and a red box appears around any that are colliding. ""MultiCustomFontsAndCollisions144.bas" has simplified options (the robot size is now selected at random, everything from 8x8 upto 64x64 robots): Currently it reminds me of Robotron:2084 meets Berzerk, but eventually we'll have walls / mazes / etc. once I add any collision with background logic ... "MultiCustomFontsAndCollisions144.bas" in action - currently the cursor keys (movement) & right ctrl key (fire) control all the players, players' speeds are chosen at random:
RE: error doing image collision detection with _MemGet - Herve - 09-27-2025 Wow man, amazing work! Redefining the characters must have taken you a long time — I’m impressed! My Linux setup doesn’t recognize the _button instruction to read the keyboard, so I couldn’t move the player or make them shoot. Everything else works well: rendering, robot movement, and collision detection are fine. To gauge performance I added an FPS counter. All tests were done with the default size 3 for the player and robots. I also varied the resolution to see the performance impact (the higher the resolution, the fewer collisions). Results:
RE: error doing image collision detection with _MemGet - madscijr - 09-27-2025 (09-27-2025, 09:43 PM)Herve Wrote: Wow man, amazing work! Redefining the characters must have taken you a long time — I’m impressed! It took some work to initially figure out how to generate BDF files, but I've been building on earlier code and this system has gradually evolved, so at this point it's very little work, which was kind of the goal - to arrive at a simple system that could be used for lots of different projects. (09-27-2025, 09:43 PM)Herve Wrote: My Linux setup doesn’t recognize the _button instruction to read the keyboard, Hmm, so _button doesn't work in Linux for the keyboard? I wonder if that's the case for Mac as well? (09-27-2025, 09:43 PM)Herve Wrote: To gauge performance I added an FPS counter. That sounds really slow for just using bounding box - then again, I'm not known as the most efficient or skilled programmer (there's a reason I call my programs Softintheheadware! LoL) so there could be a million things that could be improved in my code. (09-27-2025, 09:43 PM)Herve Wrote: Pixel-perfect detection has a significant cost. It’s fun to dig into, but if your goal is a game with many sprites you should prefer other, more performant techniques. Well, my ultimate goal is just to turn some of my crazy ideas into fun or interesting old school type games that people can enjoy or be inspired by. I would like to figure out how to improve performance, which I hope the smaller bounding boxes (using the edge detection) will help with. I'm going to study the examples you and others have left and see if any of that can be applied. For the collisions I really would prefer a general reusable pixel-perfect technique that works with any characters in those fixed-width fonts, that can be called with a simple function call. Any suggestions? Also, I realize my code is all over the place and will give Steve and BPlus a headache. Hopefully I'll be able to clean it up & simplify it as time goes on, to make it more maintainable and easier for others to understand & modify. Anyway, thanks for giving it a try! RE: error doing image collision detection with _MemGet - madscijr - 09-29-2025 (09-10-2025, 12:23 AM)Unseen Machine Wrote: As youre going with simpler collision detection methods for now (wise idea in my box) I knocked this up for you...never know it might come in handy! (09-14-2025, 10:45 PM)Herve Wrote: Pixel-perfect collision detection demo; it was fun to create. There's definitely room for performance improvements. (09-23-2025, 10:10 PM)Herve Wrote: Here’s the new program that performs pixel‑perfect detection without using mask techniques; it’s based on polygon intersection calculations. I ran Unseen's program, and Herve's first and second programs (VERY nice work, BTW!) and have a question for all 3. They work for these vector shapes - even irregular shapes, in Herve's case - but how would you use these to detect collisions between the raster characters my program is using? Would each characters need to mapped to an equivalent vector shape? (At some point I think I wrote some code that may have done that, but it's been a couple years, and I don't recall how well it worked...) Thanks again for sharing these fascinating demos. RE: error doing image collision detection with _MemGet - madscijr - 09-29-2025 (09-06-2025, 10:22 AM)bplus Wrote: +1 Thankyou @madscijr of reminding me of this delightful little piece of code. As I recall @TerryRitchie got us started on this topic of collision with irregular objects. Thankyou Terry, where the heck are you anyway? BPlus, thanks for sharing this (again), it's very impressive, but I have one major problem - it's way too advanced and/or cryptic. I can't even see where those spider shapes are defined, how would I modify it to use my own shapes, such as the robots in my demo? Thanks again RE: error doing image collision detection with _MemGet - bplus - 09-29-2025 Quote:BPlus, thanks for sharing this (again), it's very impressive, but I have one major problem - it's way too advanced and/or cryptic. Honestly I don't remember what I did except carry forward Terry's idea into my own coded app. Checking if the image boxes intersect is the crucial first step for saving time as opposed to looking for intersects over the whole scren for each image. If two images are going to intersect then certainly their image boxes will. OK I will assume you know how to do box or rectangular intersects. Now what did we do next? ...stay tuned for bplus to reread his code. RE: error doing image collision detection with _MemGet - madscijr - 09-29-2025 Yes, my prog first checks for boundary overlap, then if those intersect, it compared the bitmask arrays of the overlapping areas for overlapping pixels, comparing 2 precalculated 2D boolean (_BYTE) arrays. It's fast enough on my PC but @Herve was citing some low FPS performance testing on their machine (not sure what hardware Herve is running or how they're measuring FPS, plus they're on Linux & I'm on Windows. How fast or slow is my prog on your computer?) Anyway, I was just curious about the other methods you all use & how those might be applied to my use case. I'm sure my code can also be made faster by updating the test to use the real edges of each shape (which I'm already finding & storing), simplifying the variables instead of using UDTs, using more global vars & less parameter passing, and generally simplifying & cleaning up the logic, which is kinda convoluted. |