Sign in to follow this  
DickieChen

TERRAIN COLLISION DETECTION

Recommended Posts

Hi everyone,
Well,I just got this book <Programming.Role.Playing.Games.with.DirectX.2nd.Edition> by Jim Adams,and I tried these code from the book to detect terrain collision .

<code>
float GetClosestHeight(ID3DXMesh* pMesh,float XPos,float YPos,float ZPos)
{

D3DXVECTOR3 vecPos=D3DXVECTOR3(XPos,YPos,ZPos);

float YBelow,float YAbove;
YBelow=GetHeightBelow(pMesh,
vecPos.x,vecPos.y,vecPos.z);
YAbove=GetHeightAbove(pMesh,
vecPos.x,vecPos.y,vecPos.z);

if(fabs(YBelow-vecPos.y)<fabs(YAbove-vecPos.y))
return YBelow;
return YAbove;
}
float GetHeightBelow(ID3DXMesh* pMesh,float XPos,float YPos,float ZPos)
{
BOOL hit;
float u,v,dist;
DWORD faceIndex;

D3DXIntersect(pMesh,
&D3DXVECTOR3(XPos,YPos,ZPos),
&D3DXVECTOR3(0.0f,-1.0f,0.0f),
&hit,&faceIndex,&u,&v,&dist,NULL,NULL);

if(hit==TRUE)
return YPos-dist;
return YPos;
}
float GetHeightAbove(ID3DXMesh* pMesh,float XPos,float YPos,float ZPos)
{
BOOL hit;
float u,v,dist;
DWORD faceIndex;

D3DXIntersect(pMesh,
&D3DXVECTOR3(XPos,YPos,ZPos),
&D3DXVECTOR3(0.0f,1.0f,0.0f),
&hit,&faceIndex,&u,&v,&dist,NULL,NULL);

if(hit==TRUE)
return YPos+dist;
return YPos;
}

</code>

and I wrote:

<code>
if(KEYDown..){
float YPOS=GetClosestHeight(role.x,role,y,role.z);
role->setYpos(YPOS);
</code>
and it didn't have a clue!!!I tried different scenes,and it just didn't work.
Does anyone know what's wrong with the code? Or just give me some code that works...
Thank you.

Share this post


Link to post
Share on other sites
Please don't use all caps thread titles. Is your thread really more important than the threads of other posters?

Please read the FAQ.

Please use the link that says "Show Preview" before posting.

Also, "not working" is not a good description of your problem. Describe what is your target and what is your diagnosis. And, what have you tried so far?

Share this post


Link to post
Share on other sites

Hi,

I found some reference here

D3DXIntersect assumes that the incoming ray and position are in the same space as the mesh to be tested.

So, if you use world transform for the mesh to set it's position and/or orientation, you'll need to transform the incoming ray and it's starting position for D3DXIntersect with the inverse of the world transform matrix used for the mesh.

Good luck!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this