Skip to main content
GameDev.net gamedev.net
🔒 Locked

QuickNeighborhood : broad phase collision detection algorithm

Started by mathgladiator Nov 8, 2007 at 12:03 AM 6 replies 6k views
Original Post
mathgladiator
mathgladiator
In two recent projects, I needed a lean way for points to query a neighborhood around themselves; my problem reduces to the collision detection of a soup of spheres. Since my primitive is so basic, the problem is basically a broad phase algorithm. Furthermore, I can't take advantage of coherence because my points are moving fast. The solution that I have devised and currently testing is as located at http://www.mathgladiator.com/code/QuickNeighborhood.cpp, and I would like feedback as to the potential of using it in games. I only have one big assumption, namely all the spheres are the same size. After I complete my testing, I aim to optimize the algorithm; the current version has... imperfections. My current benchmark is included in the code. I tested 10,000 spheres of radius 0.1 uniformly distributed inside a box with all dimensions 1; it generates 194,593 intersections in 0.254453 seconds.
-mathgladiator
oliii
oliii
Looking at the code. a quick mention of a potential bug

plane GetSplitPlane(point3d * P, int n){	// pick a random point	int idx = rand()%n;	// compute the desire midpoint	point3d pMid;	pMid.x = (P[idx].x + P[n-1].x)/2.0;	pMid.y = (P[idx].y + P[n-1].y)/2.0;	pMid.z = (P[idx].z + P[n-1].z)/....


I suppose you should do

	// pick a random point	int idx = rand()%(n-1);


since idx could in fact be equal to (n-1) (then you end up with a singularity and the universe explodes).

Looking at the rest now...
Everything is better with Metal.
oliii
oliii
Really cool. Looks like a really fast BSP tree sort of thing.

How deep the recursion goes? I'd check the stack space usage just to be sure. Otherwise, that's quite neat. Have you checked the validity of the results as well?

Maybe use axis-aligned only split axes? To save the trouble of 'randomness'. As you compute the histogram, you compute the bounding boxes around the sets, and use the longest axis as your split axis.
Everything is better with Metal.
Cranky
Cranky
oliii: 3%3 equals 0 as does 1%1, 2%2 etc.
if you do rand()%n, rand()%n can never equal n :)

But there are other reasons why one shouldn't do rand()%n, as that isn't really random enough in most cases.
oliii
oliii
and rand() is slow. but rand()%n can be equal to (n-1), which is my point. [grin]
Everything is better with Metal.
Cranky
Cranky
Oh sorry, I thought your line "since idx could in fact be equal to (n-1) (then you end up with a singularity and the universe explodes)." was refering to your fix
mathgladiator
mathgladiator
Quote:
Original post by oliii
and rand() is slow. but rand()%n can be equal to (n-1), which is my point.


fixed the bug.

Quote:
Original post by oliii
Really cool. Looks like a really fast BSP tree sort of thing.

How deep the recursion goes? I'd check the stack space usage just to be sure. Otherwise, that's quite neat. Have you checked the validity of the results as well?

Maybe use axis-aligned only split axes? To save the trouble of 'randomness'. As you compute the histogram, you compute the bounding boxes around the sets, and use the longest axis as your split axis.


The stack space is limited by depth. I plan to remove recursion and use a finite stack. Furthermore, I plan to have a version of the sort that operates on the major axis in a sequence.

I am tempted to create a static BSP tree (a sieve) that can update based on singular updates or a global update.

As to testing, I test the results against the naive version for 10,000 (used excel to sort and compare the 190,000 some intersections.

The proof for correctness is straightforward. Namely, it is obvious that it generates some intersections. All that is required is to demonstrate that every collision is generated. You do this by picking any intersection and then trace the program to make sure they are never separated.
-mathgladiator

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.