Jump to content
  • Advertisement
Sign in to follow this  
DickieChen

TERRAIN COLLISION DETECTION

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

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!