DekuTree64 wrote: |
Actually sprite checks increase linearly if you don't keep checking the same ones over and over. It's like a linear sort, as opposed to bubble sort.
for(i = 0; i < sprites; i++) for(j = i; j < sprites; j++) ColSpriteSprite(i, j); |
Quote: |
As long as you do your collision response (stop them from moving or whatever) on both sprites involved in a collision at the same time |
Quote: |
Never could figure out how to deal with multiple collisions, where you have like 2 sprites hit, you move them apart |
tepples wrote: |
This is still O(n^2) and only half as fast. The key to speeding up the process is to sort the sprites by top y coordinate before you test them. Then you can break from the inner loop once sprite j's top is below sprite i's bottom. |
Code: |
for(i = 0; i < sprites - 1; i++) for(j = i + 1; j < sprites; j++) Check(i, j); |
Code: |
A check B, C, D, E B check C, D, E C check D, E D check E |
Quote: |
Multiple collisions should clear up after a few frames if you accelerate each pair of sprites along the lines between their centers. |
DekuTree64 wrote: | ||
Not quite n^2. |
Quote: | ||
... That's only 10 checks |
Quote: |
And yes, Y-sorting would speed it up a whole lot. Or X-sorting if it's a more left-to-right game would probably work even better. |
Quote: |
I never did get it working well enough to see if that would really be a problem, I just couldn't get it worked out in my head how it would keep from ending up with things inside of eachother. |
tepples wrote: |
More generally, for n sprites, your algorithm needs n*(n - 1)/2 checks. As n increases, this approaches a constant factor times n^2 checks. Thus, your algorithm's efficiency differs only by a small constant factor, and though you admit it's not linear, it's still quadratic. |
poslundc wrote: | ||
I didn't get a job when in the interview I said an algorithm that would take n choose 2 cycles (an improvement over the original algorithm, which was n^2) was still geometrically bounded by n^2. Stupid jerk... :( Dan. |
Quote: |
What happens when we start checking for collisions on angled platforms? |