D3DXComputeBoundingSphere issue

Started by
12 comments, last by TAlchemist 18 years, 9 months ago
Ok i have like 7 models that are loaded into a model list and then the game spawns objects that link to the model. Everything works fine for drawing and moving and having lots of objects. But now i am trying to do some basic object interactions like selecting a target object and basic collision work and i found an error. I have code included that uses D3DXComputeBoundingSphere to give me the center of the object and its radius for ray to sphere intersection testing before i check ray to triangle of the model. I have an error with some models and not others. So to debug i added output information durring the loading of models and instancing of creatures. It apears that the tiny.x model and my sky.x and ground.x models all load and compute proper bounding spheres but my rattling.x and tree3.x all have radius 0 and center 0,0,0. I even have it checking for HRESULT failures in all parts of the bounding sphere creation and get no failures just get zero's for the results. It thinks it is working but computes nothing. Any ideas why? could it be bad models, bad code, or bad DX8.1?
Advertisement
I'm not quite sure why you would compute a bounding sphere on a ground or sky model, but because it works with those and not world "objects", I would suspect something in the difference between how you load a sky or ground model versus how you load the "world object" models. Can you show the general flow of the code or some snippets? Do you use the exact same functions for loading sky, ground, and the other models?

Chris
Chris ByersMicrosoft DirectX MVP - 2005
i dont really want it for sky or ground it is just in all objects loading < all x files are loaded the same > as a test for now.

i load the object from a file.
m_pMesh

clone it to a specific FVF format for my terrain following < which follows but wont render and the regular object renders but wont follow...go figgure >
m_pColMesh

then i loop through the textures and materials of the x model.

then load 2 more versions of the model low res and med res.
m_pMeshLow
m_pMeshMid

then i compute the sphere for the m_pMesh

everything works fine for the sky.x, ground.x, and tiny.x. i found that my building2.x seems to work too but its radius is only 1 and should be much more 1. but the models tree3.x and rattling.x all have 0 radius and 0,0,0 for center
i found that even the default tiger mesh wont compute a bounding radius or center just a radius of 1 and a center of 0,0,0
not to hijack your thread, but I was having a few issues with computing bounding box's myself, which is about the same as the bounding spheres (fucntion wise).

I was wondering how you do you get the D3DVECTOR3* to the first position (the first parameter). I thought I could just use the vertex buffer for this, but am running into issues (its blowing up when I call D3DXComputeBoundingBox()).

Here's what I was trying...
// pMesh is an ID3DXMesh *			// Calculate the bounding volume of the object.  result = D3DXComputeBoundingBox( (D3DXVECTOR3*)pBuffer,                                pMesh->GetNumVertices( ),		   		pMesh->GetNumBytesPerVertex( ),   				&tempMin, &tempMax );           // D3DXVECTOR3's


If you could point me in the right direction, I'd appreciate it.
Well i read this from the Toymaker.info and have used it to make my bounding box code pretier but still no go.

BYTE* pVertices=NULL;

hr=m_mesh->LockVertexBuffer(D3DLOCK_READONLY, (LPVOID*)&pVertices);
if (FAILED(hr))
return FALSE;

D3DXVECTOR3 minBounds,maxBounds;

D3DXComputeBoundingBox((D3DXVECTOR3*)pVertices, m_mesh->GetNumVertices(), D3DXGetFVFVertexSize(m_mesh->GetFVF()), &minBounds, &maxBounds);

m_mesh->UnlockVertexBuffer();


also on a side note any of my meshes will load in his bounding demo and will collide correctly.
Awesome, thank you much. That fixed my problem. :)
well since i cant help my self im glad i helped you.

i dont know why some of my meshes will load fine and compute bounding. maybe i can try using boxes instead...i dont know.
Or you could make your own function to calculate it, it's not that hard.

Anyways, the only problems I can think of are:
- Maybe you're not pointing to the position item in the pFirstPosition argument.
- Maybe you're not passing the right number of vertices to the function (i.e. passing the number of vertices of the low poly model)
- Are you sure you're passing the right stride into the function?
well i assume that i am passing the correct stride. i have tried cloning the meshes to a specific FVF and using that but never seems to work correctly. I will look into refining the model loading code and check that out first. i think i might have fixed another issue i was having with loading models for colllision tests and can remove my collisionModel from the memory and just act on the models.

Also i read in an article how alot of games use the lowest res model for collision testing since it will be alot faster to check half the vert's and faces.

This topic is closed to new replies.

Advertisement