# Bounding sphere

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

## Recommended Posts

Hi all, I was wondering how I could visually place a bounding sphere around an object in my game, I know the D3DXComputeBoundingSphere gives you back the center vector and radius of the sphere, maybe using this radius I could call D3DXCreateSphere function ? Any help is much appreciated. Steve

##### Share on other sites
Yea why don't you try that first, should be easy. And then render it semi-transparent. Another solution would be to render a two-dimensional circle always facing the camera.

##### Share on other sites
Thanks Lantz,

I found this code below, think this should be fine :

int DrawSphere( D3DXVECTOR3 pos, const float &radius ){LPD3DXMESH meshSphere;D3DXMATRIX matLocation;D3DXMatrixIdentity( &matLocation );if( !D3DXCreateSphere( m_pd3dDevice, radius, 16, 16, &meshSphere, NULL ) )return 0;D3DXMatrixTranslation( &matLocation, pos.x, pos.y, pos.z );SetWorldMatrix( matLocation );D3DMATERIAL9 mtrlSphere;ZeroMemory( &mtrlSphere, sizeof( mtrlSphere ) );mtrlSphere.Diffuse.r = mtrlSphere.Ambient.r = 1.0;mtrlSphere.Diffuse.g = mtrlSphere.Ambient.g = 1.0;mtrlSphere.Diffuse.b = mtrlSphere.Ambient.b = 1.0;mtrlSphere.Diffuse.a = mtrlSphere.Ambient.a = 1.0;m_pd3dDevice->SetMaterial( &mtrlSphere );if( !meshSphere->DrawSubset( 0 ) )return 0;return 1;}

pos being the position in world space of my object.

How would I draw a 2d circle, would the formula below work :

I could use maybe a simple for loop for the angle and store x,y in vertex buffer ?

Keeping it facing the camera, forgot how to do this!

Best regards,
Steve

##### Share on other sites
Yea that function will probably work, though it generates immense memory leaks (and it will perform terrible) since it's recreating (and not destroying) a mesh object every time its called.

And yep, that's the basic formula for a circle. This is one way of doing it:
- find the vector C from the circle position to the camera
- find any vector X perpendicular to C
- calculate a new vector Y = X cross C
- make sure all vectors are normalized
- now use X and Y with the formula you wrote to generate the circle, eitehr by using lines or points, thats up to you

It will look something like this
for( angle 0 to 2pi )
point = center + X * cos(angle) * radius + Y * sin(angle) * radius

You could optimize a bit by premultiplying X and Y with the radius.

##### Share on other sites
Thanks Lantz,

I'm a little confused though over keeping the circle facing the camera. So from the circle position vector( object position )Cir(x,y,z) to the camera position vector Cam(x,y,z) thus :

C = Cam - Cir

now X is find any vector perpendicular to C ( this just a vector that is 90 degs to C ? )

then, Y is cross product of X C.

I can't seem to picture what is going on here in my head ?

Best regards,
Steve

##### Share on other sites
Well, hmm, since the circle function is 2-dimensional, we need two axises (x & y). Normally we would use X and Y, but in this case that wouldn't give us a circle that is facing the camera. So what we're trying to do is to calculate two new axises ('bases' in linear algebra) to use with the circle function.

Quote:
 now X is find any vector perpendicular to C ( this just a vector that is 90 degs to C ? )

Yep, this will be the first axis.
Quote:
 then, Y is cross product of X C.

The cross product produces a new vector which is perpendicular to both operands. So this gives us our second axis. You can then use these axises to generate the circle.

One thing though, I was thinking about this a little earlier and remembered that this method is not very accurate, since the camera is not a point in space but a plane. So unless the object is right in front of the camera / in the center of the screen, or very far away, the circle will not be entirely correct.

A better and accurate (IIRC, someone correct me if I'm wrong please) would be to extract the axises from the current view matrix. This isn't very hard and in OpenGL it can be done like this (shouldnt be hard to convert to D3D):
// from my matrix classVector3 forward(){ return Vector3( -m31, -m32, -m33 ); };Vector3 up(){ return Vector3( m21, m22, m23 ); };Vector3 right(){ return Vector3( m11, m12, m13 ); };

So if you do it this way, which you probably should to get good results, use the up and right vectors as the axises for the circle.

##### Share on other sites
Thanks Lantz,

This is just the same as billboarding I suspect ?

Yup.

• 18
• 18
• 11
• 21
• 16
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!