Advertisement Jump to content
Sign in to follow this  

Walking on my Terrain

This topic is 4882 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

I have my Terrain Engine setup and running Im using a FirstPersonCamera in the DX sample Framework now to simply Fly around .. Soon I will implement Frustum culling and create my own camera class but for now im stumped at how to find the triangle that im currently above so that I can get the Y value to place the bottom of my Model's Mesh (his feet) onto the terrain and have it correctly (for now slide around) move about. How Do I do this .. I have googled my heart out and still can not find any tutorials about placing my model on my terrain and setting up gravity so that i can create jumping movement ect. Any Help I would GREATLY Apreciate it thanks guys.

Share this post

Link to post
Share on other sites
if all you want is to slide the character about the terrain mesh (no jumping, no physics) all you need is a ray-triangle test, test the ray with source at the character position, and with direction (0,0,-1) if Z is your up vector, (0.-1,0) if Y is your up vector instead against all triangles (or the collection of posible triangles, the triangles around your char), and then change the Z (or Y) position accordingly.

If you want physics and jumping around, it gets complicated look for collision detection algorithms here is a tutorial on how to do it the way Quake 3 does it.

Share this post

Link to post
Share on other sites
Please explain details about your terrain. Are the polys aligned on a grid? Doing so would really improve performance and make your job easier. Basically, just set a grid size for your terrain and place two triangles in each grid slot. Doing so allows you to jump instantly to a specific set of (or even single) triangles in the largest terrain you can cook up.

Share this post

Link to post
Share on other sites
Find your players xz position, and then find the corresponding height value on the terrain for that xz position, taking into account any scaling/translation you have done on the terrain. For a simple example, imagine a 2d side scrolling game with some stairs. The player moves forward towards the stairs. Get his x-position, and determine which stair he is standing "in". Then find out the y value of that stair, and move him up.

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
Yes i agree with IgnisDeus, this is the way i do that stuff.
By saving your heightmap and some how maps between your application and the map. say if your heightmap is 256*256 and the scale of your is times ten 2560 * 2560 simple use the x-z coords of your character to lookup the y value in the heightmap.

Another way and a little more tricky is to implement your terrain in a octree and use this to get collision detection, plenty of good tut's on that topic octtree

last i using novodex physics engine to calculate all my collisions and response, i tell you it is great fun. and fairly easy to integrate in you engine. You can acctually load a whole heightmap and then let it calculate all you stuff

Good luck

Share this post

Link to post
Share on other sites
hi there...

I've created a "walking on terrain app" some days ago...The way I used to do it is working correctly.

First, I put the camera at its 'xz' position and then I find y as you are doing. But before this, I find the triangle the camera is on using the dot product.To do this, I loop through all the triangles of the terrain.

I proceed this way(you must work with 2D to do this :)): I get the x and z from the camera and from the 3 vertices of the Triangle(for loop) and keep each ones into D3DXVECTOR2s. Then, I create more 3 D3DXVECTOR2s.Into these I will add the normalized subtraction of the camera position and the triangles vertices(3 vectors).Calculating the dot product of these 3 normalized vectors, we get the cosine of the angle between them.Well, If you have understood it, you should know that for the camera be inside the triangle, the sum of the arc cos of the 3 dot products, must be 360 degrees.If it be lower, the camera is outside the triangle...hmm...I'm not sure you understood it, I'm a bad teacher :(...heh.Maybe some code will help:

//loop through all terrain triangles to find which the camera is on
for(DWORD i = 0; i<NumTriangles; i++)
D3DXVECTOR2 Cam2D = D3DXVECTOR2(camera.position.x, camera.position.z);
D3DXVECTOR2 v0 = D3DXVECTOR2(Triangle.v0.x, Triangle.v0.z);//triangle vertex 0
D3DXVECTOR2 v1 = D3DXVECTOR2(Triangle.v1.x, Triangle.v1.z);//triangle vertex 1
D3DXVECTOR2 v2 = D3DXVECTOR2(Triangle.v2.x, Triangle.v2.z);//triangle vertex 2
D3DXVECTOR2 d0, d1, d2;//vectors to use in the dot product calculation
D3DXVec2Subtract(&d0, &Cam2D, &v0);
D3DXVec2Subtract(&d1, &Cam2D, &v1);
D3DXVec2Subtract(&d2, &Cam2D, &v2);
//normalizing vectors...
D3DXVec3Normalize(&d0, &d0);
D3DXVec3Normalize(&d1, &d1);
D3DXVec3Normalize(&d2, &d2);
//get the cosine of the angles between the vectors through the dot
float a = D3DXVec2Dot(&d0, &d1);
float b = D3DXVec2Dot(&d1, &d2);
float c = D3DXVec2Dot(&d0, &d2);
//now, using the acos(), lets find the angles between these vectors
a = float(acos(a));
b = float(acos(b));
c = float(acos(c));
//as I said before, if a+b+c == 360 degrees, the camera is on this triangle
//360 degrees = 2pi radians.but,since we are working with float, if we set this at the next if 'a+b+c == 2*D3DX_PI', we will get some errors.So lets use a value a bit lower than pi.After some tests, I find that 6.28318f is good :) so:
if(a+b+c > 6.28318f)//the camera is on the triangle
TriangleIndex = i;//use a var like this to store the index of the tringle the camera is on and then use Triangle[TriangleIndex] to find the camera.position.y
//so we are done

Now, its just find the y...:)

To help you more, I will provide you one application I created when I learnt the above method:

Cya man....

[Edited by - xissburg on September 10, 2005 6:36:31 PM]

Share this post

Link to post
Share on other sites
Thanks man very much for the code, makes it much easier to understand when I can interpret thru that. My wife just gave birth to our second child on thursday (a little baby girl :0) so I have not yet tried this .. I will try it out very shortly and I really hope that it works :0 thanks again ill let ya know.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!