Sweep and prune collision detection lib + demo

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
dreadkillz
Party member
Posts: 223
Joined: Sun Mar 04, 2012 2:04 pm
Location: USA

Sweep and prune collision detection lib + demo

Post by dreadkillz »

Hello everyone, I've been very interested in learning about different broad phase/coarse collision detection along with teaching myself about lua and love. My last demo on here was a quadtree implementation, but it was pretty crappy and slow. This led me to research other methods.

From what I've seen on here and from my research online, using a grid to divide up the world space into smaller "cells" is a very fast and simple method to limit the amount of collision pairs you have to check. I then found another algorithm called sweep and prune to manage the broad phase collision detection.

For those that don't know about sweep and prune (SAP), the algorithm involves creating a list of AABB's intervals for each axis and doing an insertion sort pass for each axis. Different AABB's are checked with each other for intersection when you perform swaps on each list.

The performance of SAP is very fast when objects move slowly/maintain their temporal coherence/maintain relative position to other objects because of how insertion sort works, best case being O(n) where n = number of objects. Because of this, SAP has the potential to be even faster than a uniform grid under certain conditions. Likewise, SAP performs poorly when objects move quickly and the insertion sort has to perform many swaps to sort each list, worst case being O(n^2).

A simple way of mitigating the worst case scenario is to combine the grid method with the SAP method. Each cell in the grid is its own SAP instance. The amount of overall swaps is reduced because each SAP only has to sort local AABB's within its cell.

The only glaring weakness with the addition of the grid is that AABB's are added to all cells that it touches, which increases the amount of work the algorithm has to do because you can have duplicate entries in each SAP.

Libraries w/ MIT license + LOVE Demo: Github Repo
Demo v1.3a
Last edited by dreadkillz on Tue Aug 28, 2012 9:56 pm, edited 12 times in total.
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: Sweep and prune + grid collision detection demo

Post by MarekkPie »

Is the sort and sweep tied directly into insertion sort? If not, then you probably could just swap it out with something like merge sort or heap sort and decrease the worst case to O(nlogn).
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Sweep and prune + grid collision detection demo

Post by kikito »

This could not come at a better timing. Last night I started working on a grid-like structure for my game.

Please set up that github account. I might not take the code verbatim, but I'd like to use it as an inspiration.
When I write def I mean function.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Sweep and prune + grid collision detection demo

Post by vrld »

Very interesting. Did you try to do any measurements comparing grid/spatial hash vs. SAP vs. grid + SAP?
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
dreadkillz
Party member
Posts: 223
Joined: Sun Mar 04, 2012 2:04 pm
Location: USA

Re: Sweep and prune + grid collision detection demo

Post by dreadkillz »

MarekkPie wrote:Is the sort and sweep tied directly into insertion sort? If not, then you probably could just swap it out with something like merge sort or heap sort and decrease the worst case to O(nlogn).
The original SAP method is tied directly to using insertion sort because of comparisons done during swap. I've not found useful documentations for using other sort methods.
vrld wrote:Very interesting. Did you try to do any measurements comparing grid/spatial hash vs. SAP vs. grid + SAP?
From my unscientific testing:

high temporal coherence:
SAP+grid is sometimes faster than SAP is faster than grid

low temporal coherence:
grid is faster than SAP+grid is faster than SAP

very low temporal coherence:
grid scales waaaay better vs SAP and SAP+grid

This is to be expected because SAP is most effective when objects move very little or maintain their relative positions to one another, which reduces the amount of sorting it has to do. Using a grid to have separate instances of SAP per cell helps, but it is not a magic bullet.

Also, the lib could use some clean up/improvements before I post a licensed version. You're free to study the demo until then. If you want to use the code in the demo, just give me credit and all that jazz.
User avatar
ishkabible
Party member
Posts: 241
Joined: Sat Oct 23, 2010 7:34 pm
Location: Kansas USA

Re: Sweep and prune + grid collision detection demo

Post by ishkabible »

I was thinking about remaking my zombie game with a bigger map and different mechanics. I was thinking about not having the zombies stack on each other this time; that would require lots of collision detection. this might just be the thing(I was just going to use hashing to try and reduce the effects)
User avatar
dreadkillz
Party member
Posts: 223
Joined: Sun Mar 04, 2012 2:04 pm
Location: USA

Re: Sweep and prune collision detection lib + demo

Post by dreadkillz »

I updated my sweep and prune stuff + demo. I cleaned up the code and reorganized it. Also a prettier readme. If you're curious about implementing sweep and prune in Lua, check my stuff for inspiration!
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Sweep and prune collision detection lib + demo

Post by Roland_Yonaba »

Checked, demo downloaded, Repo watched & zipballed!
User avatar
dreadkillz
Party member
Posts: 223
Joined: Sun Mar 04, 2012 2:04 pm
Location: USA

Re: Sweep and prune collision detection lib + demo

Post by dreadkillz »

Updated the libraries and demo to v1.2. I got rid of the complicated flagging system when swapping items while sorting. Now it does a simple overlap test instead. The demo also runs a good amount faster ^^ .
User avatar
litearc
Citizen
Posts: 57
Joined: Thu May 17, 2012 12:40 am

Re: Sweep and prune collision detection lib + demo

Post by litearc »

Really cool, and very useful! I've never really looked into collision detection algorithms, but this seems pretty efficient since I get almost no lag every with 600 objects. Thanks a lot! I probably will be looking at the code if I decide to use this.
Post Reply

Who is online

Users browsing this forum: No registered users and 229 guests