# Terrain Following

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

## 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->UnlockIndexBuffer();

D3DXVECTOR3 *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 on other sites
http://www.toymaker.info/Games/html/terrain_follow.html

##### 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 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!

bp

##### 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.

1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5

• 26
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633694
• Total Posts
3013378
×