# Collision Detection: Simulation Inexplicably Slowing Down/How To Efficiently Calculate Extremes In Each Dimension

BACKGROUND: As discussed here: Fast 2D collision detection in an unbounded space I am working on a collision detection algorithm for a simulation. The catch that makes this case unique is that the objects in the simulation could all be in a very small area (e.g. 1000x1000) or in a very large area (e.g. 9 bil x 10 bil). Moreover, changes like this could occur very rapidly because of high object velocities, so I needed a somewhat sophisticated algorithm to deal with this situation. In the end, I went with quadtrees (http://en.wikipedia.org/wiki/Quadtree). When I finished writing the quadtree class and determined that it was functioning correctly, I uploaded it into my simulation.

THE PROBLEM: At first, the quadtree worked wonderfully. I was just testing it out for the first time, so instead of feeding it updated bounds every time step of the objects with the least x value/greatest x value/least y value/greatest y value, I just fed is a large area that I knew would be good enough for the test case (10 mil x 10 mil).

In a test involving 1600 objects, my simulation ran almost four times faster than the brute force solution of checking every object against itself! (about 28 fps instead of 7 fps) Wonderful! I then proceeded to feed the quadtree the updated "bounds" of the simulation by finding the min/max x/y each frame (this is where the problem begins). After updating each object's position, I simply checked to see if it was the new min/max extreme in each dimension:

    double tempradius = (*getMoveableEntityVector()[n]).Retradius();
double tempx = (*getMoveableEntityVector()[n]).Retxpos();
double tempy = (*getMoveableEntityVector()[n]).Retypos();

if (n == 0) { //if it's the first object initialize the variables based off it
}
else { //all other objects
if (minx > tempx - tempradius) { //if this object has the lowest x value
}
else if (maxx < tempx + tempradius) { //if has the highest x value
}

if (miny > tempy - tempradius) { //if has lowest y value
}
else if (maxy < tempy + tempradius) { //if has highest y value
}
}


Running through this simple code a mere 1600 times per time step slowed the simulation back down below the speed of my original brute force implementation! (about 7 fps)

WHY THIS DOESN'T MAKE SENSE TO ME: The original brute force implementation had to run through the relatively computationally intense distance formula (http://math.about.com/library/bldistance.htm) about 1600*1600 times per time step, or more exactly, 1600*1599/2 = 1,279,200 times per time step. I would think running through the above code once would be about as computationally intensive as doing the distance formula once. How is going through the above code only 1,600 times costing me as much processor time as going through the distance formula more than a million times?!?

EDIT: The question is not directly about the quadtree; it is about the code above that finds the objects with the least/greatest x/y coordinates. (This code feeds its calcuations to the quadtree so the quadtree knows where to start, but it is definitely not quadtree code). The quadtree code ran quite fast until I added the above helper code. The only reason I mentioned the quadtree and the background to this issue is because the quadtree/background stuff gives context on why I think that this code should be going much faster (1000 comparable checks vs 1,000,000!)

TLDR (TOO LONG DIDN'T READ): Check out the code above. Is there something making it take up WAY more (about 1000x more!) processor time than I think it should be taking?

Thanks in advance for the time and help!

• A 4:1 change could come from anywhere from cache thrashing when data layouts changed to extra work in one place eating up the savings elsewhere. You need to profile each change for cache hits, call counts and time spent in each logical block to figure it all out. My first reaction is that rebuilding the tree every frame is eating all your time, and that's destroying any savings from avoiding the computations later. But it really does need to be measured, especially since we don't know what's inside your quadtree code. – Patrick Hughes Aug 17 '13 at 22:44
• Cache misses are going to dominate a lot of performance. Your tree might have been written without the cache in mind. With tons of fast moving objects if you have to rebuild your tree from frame to frame, you might defeat the purpose of using the tree at all. – RandyGaul Aug 17 '13 at 23:04
• @PatrickHughes @ RandyGaul Thanks for the information! However, the question isn't directly referring to the quadtree. As I mentioned above, things were going well when I first implemented the quadtree, it's just the code above that is finding the outer-most bounds of the quadtree that is causing the slowdown. I will try to be more clear with an edit. – MindSeeker Aug 17 '13 at 23:46
• @PatrickHughes @ RandyGaul Even though your two responses didn't quite answer the question I was asking, I really appreciate both of them! The things you were talking about (I admit to being more than a little over my head in these topics, but I'll study up) could explain why the quadtree was great, but not as great performance-wise as I had hoped. Once I deal with the current issue I'll definitely dig into the whole cache thing to optimize my quadtree code. Thanks again! – MindSeeker Aug 17 '13 at 23:48
• @MindSeeker "optimise code for the cache" - wow, where to begin? It's complicated. Use contiguous memory, don't dynamically (de)alloc, use memory in a predictable fashion. Here's a video (jump to ~18:30 for a more relevant example): channel9.msdn.com/Events/Build/2013/4-329 – congusbongus Aug 22 '13 at 4:46