• Advertisement
Sign in to follow this  

Terrain and Cameras DirectX

This topic is 4050 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! I've been busy putting together my first 3D engine, but have stumbled into a problem. First of all what is the best way to make terrain for DirectX? At the moment I've made some basic terrain in 3D studio max and exported it as a .x file. The main problem however is I don't know how to make it so that the camera will follow the terrain and not just go straight through it. Any help will be much appreciated!

Share this post


Link to post
Share on other sites
Advertisement
You'll need some sort of collision detection. First you'll probably want to split the terrain into various sections to reduce the amount of testing, use an octree or something similar. Then cast a ray down from the cameras position and do a ray-triangle test, just above the intersection point is where the camera should stop.

Share this post


Link to post
Share on other sites
I was hoping there was maybe a simpler way to do it, like retrieving the coordinates of the camera and the coordinates of the terrain and making sure that a there is a certain distance between the Y coordinates or something like that. The problem is, I haven't done collision detection before and the terrain I'm using at the moment is just a place holder to get things working.

Share this post


Link to post
Share on other sites
If you want to use the Y coordinate of your camera and do some SIMPLE collision detection then using .x file is not the way to go.

You should take a look at some HEIGHTMAP examples. This is much easier to do and to use then .x files.

When using a HEIGHTMAP then you already calculated the height of every point. So you only have to use that Y height (from multiple triangles) to set the Y of the camera.

Google for examples, they are easy to find.

www.nextdawn.nl

Share this post


Link to post
Share on other sites
Yes, generally for terrain, a heightmap is the way to go. If you were looking to have the camera stuck to the terrain you could use the 4 different y values in the heightmap that surround you and interpolate them depending on your x and z position. For an example I can post some code if you'd like.

Share this post


Link to post
Share on other sites
Thanks for the help guys, I'm going to have a look at height maps, some sample code would be great as well thanks!

Share this post


Link to post
Share on other sites
So a heightmap as you'll find is an array of float, int or unsigned char values usually. So here:

float heightmap[64*64];

each value holds a height that is used in collision and mesh rendering but I won't get into that b/c it can be long. Here is the code that interpolates the heights. It assumes that each grid unit is 1x1:

float GetY(float x, float z, int cell)
{
float scaledX = (x/2 + (float)(columns+1)/2);
float scaledZ = (z/2 + (float)(rows+1)/2);

float x0 = (int)scaledX;
if (scaledX < 0)
--x0;
float z0 = (int)scaledZ;
if (scaledZ < 0)
--z0;
int p1, p2, p3;

p0 = (z0 * (columns+1) +x0);
p1 = (z0 * (columns+1) + x0 + 1);
p2 = ((z0 + 1) * (rows+1) + x0);
p3 = ((z0 + 1) * (rows+1) + x0 + 1);

float fracX = scaledX - x0;
float fracZ = scaledZ - z0;

float xInterp0 = (heightmap[cell][p0]/3) + fracX * ((heightmap[cell][p1]/3) - (heightmap[cell][p0]/3));
float xInterp1 = (heightmap[cell][p2]/3) + fracX * ((heightmap[cell][p3]/3) - (heightmap[cell][p2]/3));

return (xInterp0 + fracZ * (xInterp1 - xInterp0))+8.0f;
}



This code may be a bit sloppy but you should be able to see what I am getting at.

Share this post


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

  • Advertisement