One interesting note on this approach is that the author suggests using a "fast" sorting algorithm like mergesort/quicksort as the sorting algorithm for best performance. But in practice, you may get better performance from a "worse" sorting algorithm: insertion sort.<p>The reason is that objects in collision detection systems tend to move relatively small steps between frames, meaning you can keep lists from the previous frame that are already mostly sorted. For sorting these mostly sorted lists, insertion sort tends towards O(n) while Quicksort tends towards O(n^2)!
This was really well put together.<p>What’s funny is that I’ve been doing some form of gamedev since late 90s and most of this is abstracted by engines at this point, but this is essential to understanding how complex system simulations work.<p>Thanks to the author for making a very accessible article
I've always enjoyed this document regarding continuous collision detection:<p><a href="https://github.com/bepu/bepuphysics2/blob/master/Documentation/ContinuousCollisionDetection.md">https://github.com/bepu/bepuphysics2/blob/master/Documentati...</a><p>The library itself is amazing in terms of performance. It is a bit challenging to integrate with though due to the amount of optimization involved.
> This naive algorithm runs in O(n2) time in Big O terms.<p>Is this true? The naive algorithm's outer loop (i) counts n - 1, while the inner loop (j) begins at i + 1 (so counts progressively less than n - 1).<p>Not a CS major, is this roughly equivalent (for large values of n) to O(n2) or is it, as it appears, something less?
I enjoyed the use of illustration. Seems like an appropriate usage.<p>Sometimes I feel like these articles with interactive illustrations are more like an excuse to put together a bunch of cool demos, like there's a lot of fluff with not much substance (a bit like a TED talk), but this one didn't let the illustrations take over.
Part 2: <a href="https://leanrada.com/notes/sweep-and-prune-2/" rel="nofollow">https://leanrada.com/notes/sweep-and-prune-2/</a><p>Check out his other goodies <a href="https://leanrada.com/" rel="nofollow">https://leanrada.com/</a>
A long time ago I did something similar but rather than sorting I just kept index lists for each direction and the objects sorted themselves. Meaning like there are 4 lists `objectIndicesSortedByLeftEdge/RightEdge/TopEdge/BottomEdge` If an object moves horizontally then it updates its own indices in the leftEdge and rightEdge arrays. This is because as it moves it likely only has to swap 1 or 2 indices at most to move itself.
> <i>I won’t cover other approaches, such as space partitioning or spatial tree subdivision</i><p>I'm curious about this comment, anyone know if the algorithm in the article is generally faster than space partitioning/spatial tree subdivision?<p>A long time ago I used a spatial tree type of approach which seemed naively to be a pretty good approach, but I never investigated or compared algorithms other people were using (this was 80's, pre-internet).
I was curious if any linear programming methods have been proposed for the problem since you just need to see if a point is inside a polyhedron or if two polyhedra intersect. There are.<a href="https://users.encs.concordia.ca/~akgunduz/CollisionDetection.pdf" rel="nofollow">https://users.encs.concordia.ca/~akgunduz/CollisionDetection...</a>
A tangent, but HNers interested in an article on collision detection might know: are there any similar articles that show how to compute the intersection of two capsules (as in the space that that a sphere moving in a straight line in a time step occupies) in 3D? My own hobby 3D game got stuck on that hurdle and I couldn’t find any examples anywhere :(
I'm a big fan of spatial grid hashes to simplify situations like this. Nice to see other approaches. Iterative Insertion sorting would be easier to port to a compute shader than a spatial grid hash, so maybe this method is better if we get into the millions of objects range?
Not mentioned is spatial hashing, which works well without complex data structures.<p>Also sad that you need all this complexity to test 25 balls in Javascript. 25^2 is a small number in C, especially when your balls fit in cache.<p>Still a good introduction to various algorithms, though.
The title was curious to me because I expected more a post about the `intersects` function, e.g the pip algorithm... turns out it's more about the complexity involved by the number of objects to test, which in the end is also interesting.
This is awesome, very beautiful and well written!<p>Are there other handy 2-d algorithms that are this well explained? I know redblobgames has a treasure trove too, but curious about more.