Bounding sphere

Started by
6 comments, last by Lantz 19 years, 6 months ago
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

If it isn't working, take a bath, have a think and try again...

Advertisement
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.
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 :

x=cos(angle)*radius, y=sin(angle)*radius

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

If it isn't working, take a bath, have a think and try again...

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.
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

If it isn't working, take a bath, have a think and try again...

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.

Thanks Lantz,

This is just the same as billboarding I suspect ?

If it isn't working, take a bath, have a think and try again...

Yup.

This topic is closed to new replies.

Advertisement