Advertisement

Bounding Spheres: Trouble Optimizing some code

Started by December 22, 2004 11:14 AM
0 comments, last by _DarkWIng_ 19 years, 8 months ago
Hello members, I've got a bounding sphere class where I have a function "Extend" to increase the size of one bounding sphere by another. I need this function to create one bounding sphere out of multiple smaller bounding spheres. However, this function is quite slow at the moment. Every sphere has the two members pos and radius, that represent the position and the radius of the sphere. Here's the code of my function:

void CSphere::Extend( CSphere& _sphere2 )
{
	// TODO: I'm sure this can be optimized
	CV3d vecAB = (_sphere2.pos - pos).GetNorm();
	CV3d maxPos = _sphere2.pos + vecAB * _sphere2.radius;
	CV3d minPos = pos - vecAB * radius;

	pos = (maxPos + minPos) / 2.f;
	radius = pos.Distance(maxPos);
}
Anyone knows if this function could be improved in some way? The are quite a lot of mults and even a sqrt() is in it. It would be nice to do it in a faster way. Lyve
_____________________________________http://www.winmaze.de, a 3D shoot em up in OpenGL, nice graphics, multiplayer, chat rooms, a nice community, worth visiting! ;)http://www.spheretris.tk, an upcoming Tetrisphere clone for windows, a 3D tetris game on a sphere with powerful graphics for Geforce FX and similar graphics cards.
First make sure this is really a bottleneck (by using profiler, not by guessing). If it's not, then don't even bother to do micro-optimizations.

when you calculate normalied AB vector you need to calculate distance... right. You can reuse this for final radius to save one sqrt. Just add both radiuses to distance and divide by 2 to get final radius. And replacing /2.0 with *0.5 might help a bit. If you have copy constructor then "CV3d minPos( ... )" might be a bit better.
You should never let your fears become the boundaries of your dreams.

This topic is closed to new replies.

Advertisement