Need some help with broad phase collisions

So I’ve been working more on a Flash game called Spewer lately, and a key element of the game is the inclusion of a particle based fluid sim. The problem at this point is that the fluid sim is too ■■■■ slow.

At first I was using a grid based maneuver to separate up the particles. This worked, but wasn’t quite good enough.

Earlier today, I switched to Sort and Sweep. AS3’s sorting is mega fast, so that’s really easy to handle. The slow part is looking for axis overlaps. I made a little test prototype of the method, so here’s what the “sweep” loops look like at this point.


boxSort.sort(sortX)
for (i=0;i<boxSort.length;i++) {
//"tar" is the current particle.
	tar=this[boxSort*]
	tar.hits=[]
	tar.hitAnything=false
	for (j=i+1;j<boxSort.length;j++) {
		tar2=this[boxSort[j]]
		if (tar2.x<tar.x+r*2) {
			tar.hits.push(boxSort[j])
		} else {
			break
		}
	}
}
boxSort.sort(sortY)
for (i=0;i<boxSort.length;i++) {
	tar=this[boxSort*]
	tar.finalHits=[]
	for (j=i+1;j<boxSort.length;j++) {
		tar2=this[boxSort[j]]
		if (tar2.y<tar.y+r*2) {
			if (tar.hits.indexOf(boxSort[j])!=-1) {
				tar.hitAnything=true
				tar2.hitAnything=true
			} else if (tar2.hits.indexOf(boxSort*)!=-1) {
				tar.hitAnything=true
				tar2.hitAnything=true
			}
		} else {
			break
		}
	}
}

(Just look at that gorgeous code, with no spacing and no semicolons. Someone probably wants to rip my throat out.)

This method works just fine, but it’s still too slow. I can’t think of any sort of trivial optimization, so is there some sort of brilliant change I could make to speed things up? Some guy said that using this method in Flash, he managed to get down to 5ms for finding overlaps in both axes for 700 particles. What the hell could he have done?