Jump to content
  • Advertisement
Sign in to follow this  
denuo_

irregular terrain collisions nightmare

This topic is 4875 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! Im new to direct3d and I really need some help. In my projection I created a mesh from a ".x" file to use it as a terrain. The problem is that I cant retrieve the Y coordinate from the mesh where my eye position is. I tried to use the pDist value from the D3DXIntersect function but I dont really understand the returned value. D3DXIntersect(m_lpMesh /* this my terrain */, (D3DXVECTOR3*) &vRayPos /* X: EyeXPos, Y: 100.0f, Z: EyeZPos */, (D3DXVECTOR3*) &vRayDir /* X: EyeXPos, y: -100.0f, Z: EyeZPos */, &iHit, &ulFaceIndex /* do I need this? how can i use it?*/, &fU, &fV /* barycentric coordinates????? */, &fDist /* ???????? */, &lpAllHits, &ulNumberOfHits); Im truily lost and any help is welcome. Thanks in advance and please forgive my english spelling.

Share this post


Link to post
Share on other sites
Advertisement
using directx functions is sometimes a bastard.
i couldnt help you with what youve got but
heres another intersect triangle that you can use.

t is the distance from the source of the ray to the point.
u and v are the texture coordinates of the point (you dont use these)
add the vector direction*t to the vector origin and youll get the position
of the collision.

youll have to retrieve the vertex and index buffer manually from the mesh
and loop through it yourself being the only problem.

if you dont want it dont use it, just trying to help.

[source "c++"]
bool intersect_triangle( const D3DXVECTOR3& orig,
const D3DXVECTOR3& dir, D3DXVECTOR3& v0,
D3DXVECTOR3& v1, D3DXVECTOR3& v2,
FLOAT* t, FLOAT* u, FLOAT* v )
{
// Find vectors for two edges sharing vert0
D3DXVECTOR3 edge1 = v1 - v0;
D3DXVECTOR3 edge2 = v2 - v0;

// Begin calculating determinant - also used to calculate U parameter
D3DXVECTOR3 pvec;
D3DXVec3Cross( &pvec, &dir, &edge2 );

// If determinant is near zero, ray lies in plane of triangle
FLOAT det = D3DXVec3Dot( &edge1, &pvec );
if( det < 0.0001f )
return FALSE;

// Calculate distance from vert0 to ray origin
D3DXVECTOR3 tvec = orig - v0;

// Calculate U parameter and test bounds
*u = D3DXVec3Dot( &tvec, &pvec );
if( *u < 0.0f || *u > det )
return FALSE;

// Prepare to test V parameter
D3DXVECTOR3 qvec;
D3DXVec3Cross( &qvec, &tvec, &edge1 );

// Calculate V parameter and test bounds
*v = D3DXVec3Dot( &dir, &qvec );
if( *v < 0.0f || *u + *v > det )
return FALSE;

// Calculate t, scale parameters, ray intersects triangle
*t = D3DXVec3Dot( &edge2, &qvec );
FLOAT fInvDet = 1.0f / det;
*t *= fInvDet;
*u *= fInvDet;
*v *= fInvDet;

return TRUE;
}


Share this post


Link to post
Share on other sites
D3DXIntersect could facilitate your work a lot (ou no velho e bom português: essa função facilitaria muito o seu trabalho [smile]).

The following code shows an example: character walking on an irregular terrain (no gravity):


D3DXVECTOR3 vRayDir ( 0.0f, -1.0f, 0.0f ); // Raio: verticamente para baixo

D3DXVECTOR3 vRayPos; // Pode ser o centro do personagem, por exemplo.
// Deve sempre estar acima do terreno.

BOOL Inters; // retorna TRUE se o raio interceptar o terreno
FLOAT pDist; // distância de vRayPos até o terreno (na direção de vRayDir)

D3DXIntersect( mesh, // malha do terreno
&vRayPos,
&vRayDir,
&Inters,
NULL,
NULL,
NULL,
&pDist,
NULL,
NULL );

if ( Inters )
{
// Suponha que você não tenha gravidade no seu cenário, e que o seu personagem esteja
// posicionado sobre o terreno de forma que o seu centro esteja a 100 unidades
// acima do terreno. Para que o personagem acompanhe o relevo do terreno, basta fazer:

centroPerson.y = centroPerson.y + (100 - pDist);
}



Good luck!

Share this post


Link to post
Share on other sites
the Intersect is a pretty decent function. A lot of people have trouble understanding the concept behind the inverse world transforms necessary to make it work however.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!