Jump to content
  • Advertisement
Sign in to follow this  
BrakePad

Terrain Following

This topic is 3443 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 all, I've loaded a mesh into my scene and am trying to implement terrain following on it (ie being able to walk along the surface of the terrain in first person).. The implementation I've used works to an extent, but is pretty ugly - when I walk up slopes the camera jutters and jerks, up and down as it moves from one poly to another - I've read about interpolation (i've been stuck on this for days now!!), but not sure how to implement this. Here's some code - triangle is a variable that is already set as the index of the triangle in the trianglelist:
//Get contents of meshes vertex and index buffers...
WORD* pIndices;
pMesh->LockIndexBuffer(D3DLOCK_DISCARD, (void**)&pIndices);
pMesh->UnlockIndexBuffer();


D3DXVECTOR3 *verts;
pMesh->LockVertexBuffer(D3DLOCK_DISCARD, (void**)&verts)
pMesh->UnlockVertexBuffer();

//get 2 vectors to calculate the normal
D3DXVECTOR3 V0, V1;
V0 = verts[pIndices[-2 + (triangle * 3)]]
    - verts[pIndices[-3 + (triangle * 3)]];
V1 = verts[pIndices[-1 + (triangle * 3)]]
    - verts[pIndices[-3 + (triangle * 3)]];

//get the face normal of the poly
D3DXVECTOR3 faceNormal = cross(V0, V1);

D3DXVECTOR3 planeOrigin = verts[pIndices[-3 + (triangle * 3)]];

D3DXPLANE plane;
D3DXPlaneFromPointNormal(&plane, &planeOrigin, &faceNormal); 

D3DXVECTOR3 endPoint(cameraPosition);
endPoint.y = -100; //End point 100 units vertically below the camera
D3DXVECTOR3 result;
D3DXPlaneIntersectLine(&result, &plane, &cameraPosition, &endPoint);


The code is returning a vector in 'result' that is close to the polygon, but not exactly - and like I say it jumps up and down on slopes... Any help would be greatly appreciated, bp

Share this post


Link to post
Share on other sites
Advertisement
http://www.toymaker.info/Games/html/terrain_follow.html

This link should solve your problem.

Share this post


Link to post
Share on other sites
Unfortunately I have already used this method to no avail, it returns the same height value as using D3DXPlaneFromPointNormal, and creates the same effect...

Any other suggestions?

Share this post


Link to post
Share on other sites
Problem Fixed!

Turns out there was nothing wrong with my calculation of the height, apart from my calculation of the cross product to get the plane normal! I guess that's what happens if you stay up til 4 in the morning trying to get something working!

Cheers for your help,

bp

Share this post


Link to post
Share on other sites
Hmm arent you getting multiple intersections with :
D3DXPlaneIntersectLine(&result, &plane, &cameraPosition, &endPoint);

I use a polygon line intersection instead of plane intersection and if the terrain is a heightmap I only get 1 intersection that might be your problem.

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!