Is distance() quicker than detectSpriteCollision()?
-
After doing some basic 2D with an overviewable number of sprites, I got now 250 sprites and check if they collide with 20 other sprites. So, in the worst case I get 250x20 = 5000 collision checks per frame (= 60 x 5000 collision checks per second = 30'000 checks / sec).
Before I go into splitting up the screen into sectors and check only the ones that contain one of the 20 sprites, I wanted to know if there is a quick solution to avoid frame drops, just by using thedistance()
function instead of thedetectSpriteCollision()
? -
People on the internet always say that distance() is slow because it has square roots in it.
-
I suspect distance would be quickest. While both are are in C behind the scenes, sprite collision will be doing more comparisons per check, I think, as distance is only two points as opposed to checking a number of overlaps. However, Will may have already employed a number of other optimisations, i.e. splitting the screen up and so on.
The only way to know for sure would be a simple benchmark test... anyone..?
However, depending on how many other things you are doing, 5,000 checks per frame would be Ok if that was all you are doing. FUZE can manage around 6,000 IF statements with variable lookups per frame. When we get a bit of Bytecode (still in the planning) in there, it will jump up somewhat.
The trick at the moment is to reduce the number of checks, keep variables as simple as possible, use vectors to store variables when possible as four values can be accessed / compared in one instance. All of which do produce mixed results and only experimentation will help you to decide.
Interestingly, I have a problem with severe slowdown in a Sudoku game i'm writing. Using pencil marks means almost 800 items need checking (should it be displayed, should it highlight duplicated marks, should it automatically remove marks etc.etc. etc. It is only the drawing section that causes my problem. Thanks to your post it made me think outside the box a little bit (sorry for the pun). I don't need to update a bunch of static numbers every frame! - I'm off to try it out!
-
Thank you both for your inputs. This means to me I am close to the borders, where the CPU/Interpreter forgives me quick solutions ;-). I think I could get along, if I review my checks, but if I want to get further with more sprites/objects, I will have to review my code and apply optimizations.