Skip to main content
gamedev.net
🔒 Locked

BoundingSphere

Started by Xcool 11 replies 1.2k views
Original Post
Xcool
Xcool
I was tring to get bounding spheres around my meshs for collision detection purposes but for some reason when the code is initlized it crashes my game so i wanted to know if any of u guys could tell me whats wrong with my code here it is: BYTE* Ptr,Ptr2,Ptr3;; LPDIRECT3DVERTEXBUFFER9 pVB,pVB2,pVB3; g_pMesh->GetVertexBuffer(&pVB); pVB->Lock(0,0,(void**)&Ptr,0); D3DXComputeBoundingSphere((D3DXVECTOR3*)Ptr, g_pMesh>GetNumVertices(), g_pMesh->GetFVF(), &D3DXVECTOR3(0.0f,0.0f,0.0f), &InvaderYR); g_pMesh->UnlockVertexBuffer(); pVB->Unlock(); pVB->Release(); g_pMesh->GetVertexBuffer(&pVB2); pVB->Lock(0,0,(void**)&Ptr2,0); D3DXComputeBoundingSphere((D3DXVECTOR3*)Ptr2, g_pMesh2->GetNumVertices(),g_pMesh2->GetFVF(), &D3DXVECTOR3( 0.0f,0.0f,0.0f ), &JetR); g_pMesh2->UnlockVertexBuffer(); pVB2->Unlock(); pVB2->Release(); g_pMesh->GetVertexBuffer(&pVB3); pVB->Lock(0,0,(void**)&Ptr3,0); D3DXComputeBoundingSphere((D3DXVECTOR3*)Ptr3, g_pLaser->GetNumVertices(),g_pLaser->GetFVF(), &D3DXVECTOR3(0.0f,0.0f,0.0f), &LaserR); pVB3->Unlock(); pVB3->Release(); return TRUE;
S1CA
S1CA
It''s difficult to tell what could/couldn''t be wrong without more context such as the flags used to create the meshes etc.

A much better plan would be for you to ask Direct3D/D3DX itself if it knows what the problem is!


1) Check the return codes from the D3D and D3DX calls you make to see if any are failing and to get a rough idea of the failure type.

2) Use the DEBUG DirectX runtimes, turn the output level for Direct3D up high, run your code, if any of the D3D calls fails check the output stream - the problem will be described to you! See the FAQ for more details on the debug runtimes.

3) Link with the DEBUG version of the D3DX library. Use D3DX9D.LIB instead of D3DX9.LIB. This will give you extra validation and debug output for D3D functions in a similar way to the debug D3D runtimes.



Simon O''Connor
Game Programmer &
Microsoft DirectX MVP
Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site
Xcool
Xcool
The odd thing when i try to debug none of my mesh,textures,music files get loaded up and it fails no matter what
also i'm not sure on how to debug ether


[edited by - Xcool on May 15, 2004 11:59:28 AM]
Etnu
Etnu
That''s usually caused by specifying weird paths for your media files. Try copying the media files into your debug folder.
---------------------------Hello, and Welcome to some arbitrary temporal location in the space-time continuum.
Xcool
Xcool
so now i splited up the 3 shpere codes so I could found out which one is messed up and it seems that i have no problems with my 1st sphere but my 2nd and 3rd are causing the crushing but the odd thing is they are the same as the 1st just with
differnt meshs see
Update:After messing with the code i found out its the Mesh that is causing the problem if i use g_pMesh in code 2 it works

This is the code for g_pMesh:
HRESULT InitGeometry()
{
LPD3DXBUFFER pD3DXMtrlBuffer;

if( FAILED( D3DXLoadMeshFromX( "InvaderY.x", D3DXMESH_SYSTEMMEM,
g_pd3dDevice, NULL,
&pD3DXMtrlBuffer, NULL, &g_dwNumMaterials,
&g_pMesh ) ) )
{

if( FAILED( D3DXLoadMeshFromX( "..\\InvaderY.x", D3DXMESH_SYSTEMMEM,
g_pd3dDevice, NULL,
&pD3DXMtrlBuffer, NULL, &g_dwNumMaterials,
&g_pMesh ) ) )
{
MessageBox(NULL, "Could not find IY.x", "Meshes.exe", MB_OK);
return E_FAIL;
}
}


D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
g_pMeshMaterials = new D3DMATERIAL9[g_dwNumMaterials];
g_pMeshTextures = new LPDIRECT3DTEXTURE9[g_dwNumMaterials];

for( DWORD i=0; i<g_dwNumMaterials; i++ )
{

g_pMeshMaterials[i] = d3dxMaterials[i].MatD3D;


g_pMeshMaterials[i].Ambient = g_pMeshMaterials[i].Diffuse;

g_pMeshTextures[i] = NULL;
if( d3dxMaterials[i].pTextureFilename != NULL &&
lstrlen(d3dxMaterials[i].pTextureFilename) > 0 )
{

if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice,
d3dxMaterials[i].pTextureFilename,
&g_pMeshTextures[i] ) ) )
{

const TCHAR* strPrefix = TEXT("..\\");
const int lenPrefix = lstrlen( strPrefix );
TCHAR strTexture[MAX_PATH];
lstrcpyn( strTexture, strPrefix, MAX_PATH );
lstrcpyn( strTexture + lenPrefix, d3dxMaterials[i].pTextureFilename, MAX_PATH - lenPrefix );

if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice,
strTexture,
&g_pMeshTextures[i] ) ) )
{
MessageBox(NULL, "Could not find texture map", "Meshes.exe", MB_OK);
}

}
}
}


pD3DXMtrlBuffer->Release();
return S_OK;
}


This is the code for g_pMesh2:
HRESULT InitMesh()
{
LPD3DXBUFFER pD3DXMtrlBuffer;

if( FAILED( D3DXLoadMeshFromX( "Jet.x", D3DXMESH_SYSTEMMEM,
g_pd3dDevice, NULL,
&pD3DXMtrlBuffer, NULL, &g_dwNumMaterials2,
&g_pMesh2 ) ) )
{
// If model is not in current folder, try parent folder

if( FAILED( D3DXLoadMeshFromX( "..\\Jet.x", D3DXMESH_SYSTEMMEM,
g_pd3dDevice, NULL,
&pD3DXMtrlBuffer, NULL, &g_dwNumMaterials2,
&g_pMesh2 ) ) )
{
MessageBox(NULL, "Could not find Jet.x", "Meshes.exe", MB_OK);
return E_FAIL;
}
}


D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
g_pMeshMaterials2 = new D3DMATERIAL9[g_dwNumMaterials];
g_pMeshTextures2 = new LPDIRECT3DTEXTURE9[g_dwNumMaterials];

for( DWORD i=0; i<g_dwNumMaterials2; i++ )
{

g_pMeshMaterials2[i] = d3dxMaterials[i].MatD3D;



g_pMeshMaterials2[i].Ambient = g_pMeshMaterials2[i].Diffuse;

g_pMeshTextures2[i] = NULL;
if( d3dxMaterials[i].pTextureFilename != NULL &&
lstrlen(d3dxMaterials[i].pTextureFilename) > 0 )
{

if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice,
d3dxMaterials[i].pTextureFilename,
&g_pMeshTextures2[i] ) ) )
{

const TCHAR* strPrefix = TEXT("..\\");
const int lenPrefix = lstrlen( strPrefix );
TCHAR strTexture[MAX_PATH];
lstrcpyn( strTexture, strPrefix, MAX_PATH );
lstrcpyn( strTexture + lenPrefix, d3dxMaterials[i].pTextureFilename, MAX_PATH - lenPrefix );
// If texture is not in current folder, try parent folder

if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice,
strTexture,
&g_pMeshTextures2[i] ) ) )
{
MessageBox(NULL, "Could not find texture map", "Meshes.exe", MB_OK);
}

}
}
}


pD3DXMtrlBuffer->Release();
return S_OK;
}


Code for Sphere:

HRESULT IntersectSphere()
{


BYTE* Ptr;
LPDIRECT3DVERTEXBUFFER9 pVB;
g_pMesh->GetVertexBuffer(&pVB);
pVB->Lock(0,0,(void**)&Ptr,0);


D3DXComputeBoundingSphere((D3DXVECTOR3*)Ptr,
g_pMesh->GetNumVertices(),g_pMesh->GetFVF(),
&D3DXVECTOR3(0.0f,0.0f,0.0f), &InvaderYR);


pVB->Unlock();
pVB->Release();




return TRUE;
}

HRESULT IntersectSphere2()
{
BYTE* Ptr;
LPDIRECT3DVERTEXBUFFER9 pVB;
g_pMesh2->GetVertexBuffer(&pVB);
pVB->Lock(0,0,(void**)&Ptr,0);


D3DXComputeBoundingSphere((D3DXVECTOR3*)Ptr,
g_pMesh2->GetNumVertices(),g_pMesh2->GetFVF(),
&D3DXVECTOR3(0.0f,0.0f,0.0f), &JetR);

pVB->Unlock();
pVB->Release();

return TRUE;
}

HRESULT IntersectSphere3()
{
BYTE* Ptr;
LPDIRECT3DVERTEXBUFFER9 pVB;
g_pLaser->GetVertexBuffer(&pVB);
pVB->Lock(0,0,(void**)&Ptr,0);

D3DXComputeBoundingSphere((D3DXVECTOR3*)Ptr,
g_pLaser->GetNumVertices(),g_pLaser->GetFVF(),
&D3DXVECTOR3(0.0f,0.0f,0.0f), &LaserR);
pVB->Unlock();
pVB->Release();

return TRUE;
}


[edited by - Xcool on May 15, 2004 12:58:55 PM]

[edited by - Xcool on May 15, 2004 1:19:42 PM]

[edited by - Xcool on May 15, 2004 1:25:37 PM]

[edited by - Xcool on May 15, 2004 1:41:24 PM]

[edited by - Xcool on May 15, 2004 1:42:44 PM]

[edited by - Xcool on May 15, 2004 1:50:39 PM]
Xcool
Xcool
now i was messing with the code and in code 2&3 i changed
GetFVF() to GetNumBytesPerVertex() and now my game dosen't crush but the collision detection dosen't work lol
so i wanted to know if it doesn't work because of what i did above or because the way i'm check for it is wrong

HRESULT CheckIntersect(float Xcenter1, float Ycenter1, float Zcenter1, float Radius1,
float Xcenter2, float Ycenter2, float Zcenter2, float Radius2)
{
float Xdiff, Ydiff, Zdiff, Distance;

Xdiff = (float)fabs(Xcenter2-Xcenter1);
Ydiff = (float)fabs(Ycenter2-Ycenter1);
Zdiff = (float)fabs(Zcenter2-Zcenter1);
Distance = (float)sqrt(Xdiff*Xdiff+Ydiff*Ydiff+Zdiff*Zdiff);

if(Distance <= (Radius1+Radius2)){
Hit = TRUE;}


return TRUE;
}

also is there a reason why GetFVF would work on mesh1 but not on 2&3

[edited by - Xcool on May 15, 2004 12:24:59 AM]
hplus0603
hplus0603
The code you posted has an obvious flaw:

quote:

BYTE* Ptr,Ptr2,Ptr3;



This declares one pointer ("Ptr") and two byte-sized values (Ptr2 and Ptr3). You then take the address of Ptr2 and Ptr3, and cast that to void**, which will assign pointer-sized data to memory that only holds a single byte; likely the assignment of Ptr2 will overwrite Ptr3.

Of course, you would have noticed this yourself if you had just stopped at the beginning of this function in the debugger, and single-stepped through, checking the value of each variable after each line. Or, if you''re l33t, if you put data breakpoints on the pointer values that got corrupted, to see where they got corrupted.
enum Bool { True, False, FileNotFound };
superpig
superpig
quote:
Original post by Xcool
also i''m not sure on how to debug ether


Forum FAQ
Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse
Xcool
Xcool
No matter what i do i can not get debug to work it just will not load up my media files even tho there in the debug folder with my .exe so the game doesn't even start up in Debug let alone give out debug messages

also do u mean instaed of using BYTE* I should some thing else like LPVOID*.

also i'm sorry if i been kind of noobish but i haven't been programing in Direct X for to long
lol i just learned what a bounding sphere was a few days ago.

also here is a pic of my game so far its a 3d version of space invader i'm tring to make it my 1st game

http://server5.uploadit.org/files/watercooler20-Space.JPG








[edited by - Xcool on May 16, 2004 11:03:18 AM]
superpig
superpig
quote:
Original post by Xcool
No matter what i do i can not get debug to work it just will not load up my media files even tho there in the debug folder with my .exe so the game doesn''t even start up in Debug let alone give out debug messages


Change your working directory. Both VC6 and VC7 have ''debugger'' tabs in their project settings dialogs; you just need to point the "working" or "starting" directory at the right folder.
Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse
benstr
benstr
What hplus0603 meant was that:

BYTE* Ptr,Ptr2,Ptr3;

should be:

BYTE* Ptr,*Ptr2,*Ptr3;

- Benny
-Benny-
Etnu
Etnu
a BYTE is 8 bits long. a pointer is 32 bits long.

Trying to cast a pointer to a byte will likely result in stack corruption.
---------------------------Hello, and Welcome to some arbitrary temporal location in the space-time continuum.
Xcool
Xcool
ok so now the debug works thanks to u guys
so the only things that stuck out on the debug was

Direct3D9: :====> ENTER: DLLMAIN(016a3950): Process Attach: 00000b10, tid=00000c28
Direct3D9: (WARN) :Executing processor detection code (benign first-chance exception possible)

Direct3D9: (INFO) :MMX detected

Direct3D9: (INFO) :Pentium Pro CPU features (fcomi, cmov) detected

Direct3D9: (INFO) :dwCPUFamily = 6, dwCPUFeatures = 7

Direct3D9: (INFO) :szCPUString = AuthenticAMD

Direct3D9: :====> EXIT: DLLMAIN(016a3950): Process Attach: 00000b10
Direct3D9: (INFO) :Direct3D9 Debug Runtime selected.
Direct3D9: (INFO) :======================= Hal HWVP device selected

Direct3D9: (INFO) :HalDevice Driver Style 9

Direct3D9: :BackBufferCount not specified, considered default 1
Direct3D9: :DoneExclusiveMode
Direct3D9: (INFO) :Failed to create driver indexbuffer
''Direct3D HAL (HWVP) Device 1'': Attached to debug monitor.
Direct3D9: (WARN) :Ignoring redundant SetRenderState - 7

''Pong.exe'': Loaded ''C:\WINDOWS\system32\d3dxof.dll'', No symbols loaded.
First-chance exception at 0x004c07bb in Pong.exe: 0xC0000005: Access violation reading location 0x09ec30fe.
Unhandled exception at 0x004c07bb in Pong.exe: 0xC0000005: Access violation reading location 0x09ec30fe.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.