Advertisement

Bounding Spheres?

Started by November 01, 2002 11:08 AM
9 comments, last by aker_jus 22 years, 4 months ago
I have implemented a frustum calculation method in my engine and test for sphere. What I need now is an algorithm for creating a bounding sphere from a mesh (I use ms3d meshes with my on renderer). So basically, is there a way to make a bounding sphere (center and radius) from the mesh''s vertices? I have searched google and flipcode, but I didnt get good results.
GraphicsWare|RenderTechhttp://www.graphicsware.com3D Graphics & Solutions
Actualy there has been COTD on flipcode about generation minimal bouding spheres with some kind recursion algorithem.

You should never let your fears become the boundaries of your dreams.
You should never let your fears become the boundaries of your dreams.
Advertisement
Creating bounding spheres is quite simple really. You need to first find the center the object, and then test the distance between that and every vertex in the object to get the max. distance. Save that and the radius. To test for collision between two objects, test the sum of their radiuses and against the distance between the two center vertexes. If that distance is greater, the two objects are not colliding.

How do I find the center and the maximum vertex? Well, for the center, you need to create a bounding box and find the midpoint between two diagomally opposite vertexes. But how do you compute the box?! Still easy! You need to find the minimum and maximum x, y, and z values in the entire object. Just iterate through and test each one against the currently stored values.

I don't have much more time, so I can't give you all the algorithms and such. Just search "bounding sphere collision detection" on google.com. You _should_ find something, I've tried it before. At worst you may need to invest in a book. The first of the Gems series explains this very well (that's where I learned it ).

[edited by - aggregate on November 1, 2002 12:39:24 PM]
Some of my code to do a bounding box, I hope this helps:


  //in the headertypedef struct{  float min_x;  float min_y;  float min_z;  float max_x;  float max_y;  float max_z;}box;//whatever function		//clear the bounding box		box.min_x=1200.0f;		box.min_y=1200.0f;		box.min_z=1200.0f;		box.max_x=-1200.0f;		box.max_y=-1200.0f;		box.max_z=-1200.0f;		for(k=0;k<numVertices;k++)			{//get x,y,z, from your mesh				//do the bounding box				if(box.min_x>x)box.min_x=x;				if(box.max_x<x)box.max_x=x;				if(box.min_y>y)box.min_y=y;				if(box.max_y<y)box.max_y=y;				if(box.min_z>z)box.min_z=z;				if(box.max_z<z)box.max_z=z;}   


[edited by - Raduprv on November 1, 2002 4:50:05 PM]
Ok thanks for the information!
It helped.
GraphicsWare|RenderTechhttp://www.graphicsware.com3D Graphics & Solutions
BTW, i think it would be better to addjust your frustrum testing, to use boxes too, not only spheres.
Advertisement
Yes, it supports boxes too.
GraphicsWare|RenderTechhttp://www.graphicsware.com3D Graphics & Solutions
Then use the boxes, they are better than spheres especially for long/flat/tall objects.
Arent spheres faster?
GraphicsWare|RenderTechhttp://www.graphicsware.com3D Graphics & Solutions
Use both! First do a quick sphere test then only if sphere is partly inside do bouding box test.

You should never let your fears become the boundaries of your dreams.
You should never let your fears become the boundaries of your dreams.

This topic is closed to new replies.

Advertisement