• Advertisement

Archived

This topic is now archived and is closed to further replies.

Is spherical collision faster than bounding box?

This topic is 5489 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm currently adding some advanced collision detection to my 2D engine, and have come across an interesting . . well, observation. And I was wondering if this makes sense. I have a sprite object. Some variables are x and y location, scale, and allowance (this is for collision). My bounding box collision is very basic. An 4 part if statement testing the 4 sides to see if the two boxes intersect. I've also written a collision function that tests a point to an oval. What was interesting is I'm testing the performance of the functions. I created 100,000 objects, and then one controllable object. Then I test this object against the 100,000 other objects, and time the function. Using the oval collision, each frame takes about 150 ms. I figured that since my oval function looks like it takes longer (creates 7 floats, does subtraction and division right off the bat) than the bounding box, that I'd call the bounding box function first to see if the oval function is even worth it. With the 4 if's, on average, the bounding box function will only go through two comparisons before quitting, whereas the oval function does a bunch of creation and divison of floats. So in the oval function, I simply called the bounding box function first, and if the bounding box function fails, then obviously the object is not close enough to touch the oval, so I just quit the oval function. Well, doing this, my frames took 5 ms LONGER. Not a big increase, but the whole point of it was to decrease my frametime. Are if statements and function calls really that slow? Mainly, I'm focusing on the function calls, because 2 if statements couldn't be slower than all that I'm doing in the oval collision functions. I can post my code if necessary, but it just seems really awkward to me that putting the bounding box "safeguard" would lower performance. Any thoughts? --Vic-- [edited by - Roof Top Pew Wee on February 9, 2003 3:02:49 PM]

Share this post


Link to post
Share on other sites
Advertisement
hm not sure i think it depends on how you use it finding the shorted distance between a poly and the sphere could be quite a bit complex though

Share this post


Link to post
Share on other sites
I figured it out. It was that when I''d do that test in the oval function, I was calling the bounding box function from inside, which in effect doubled the number of function calls I was making.

I actually copied the contents of the bounding box function into the oval function, so that I had the same number of function calls, and I got my frame time down to 130.

So it appears what was happening is that the bounding box function gave me an improvement of 20 ms, but the extra function call was giving me a decreased improvement of 25 ms.

--Vic--

Share this post


Link to post
Share on other sites

  • Advertisement