Jon
Jon

Reputation: 505

Height Detection - OpenGL

I am using openGL ES. I have loaded a ball, skybox, and a small patch of terrain into the program.

The terrain changes heights at certain points. The ball rolls around the terrain.

My question is: is there any way I can tell what the height of the terrain is at the position of the ball? (The position of the ball is stored in a variable that can be used). Is there any sort of OpenGL ES command, or even an OpenGL command I can base some research of this into?

Upvotes: 1

Views: 808

Answers (2)

Christian Rau
Christian Rau

Reputation: 45948

OpenGL is a rendering API, it just draws things onto the screen, nothing more. So you won't get around doing such things like terrain collision detection and the like yourself.

If you got a heightmap for the texture (e.g. you draw a x-z regular grid with the y taken from a heightmap). You can just search for the cell your ball is in (or better the triangle the ball is in of the two triangles) and interpolate the corresponding height values to get the height of the terrain at the ball's position.

Upvotes: 1

Sid
Sid

Reputation: 129

Are you using a height map to make the terrain? If you are, this should help:

int Height(BYTE *pHeightMap, int X, int Y)          // This Returns The Height From A Height Map Index
{
    int x = X % MAP_SIZE;                   // Error Check Our x Value
    int y = Y % MAP_SIZE;                   // Error Check Our y Value

    if(!pHeightMap) return 0;               // Make Sure Our Data Is Valid
    return pHeightMap[x + (y * MAP_SIZE)];  // Index Into Our Height Array And Return The Height

}

We need to treat the single array like a 2D array. We can use the equation: index = (x + (y * arrayWidth) ). This is assuming we are visualizing it like: pHeightMap[x][y], otherwise it's the opposite: (y + (x * arrayWidth) ).

Now that we have the correct index, we will return the height at that index (data at x, y in our array).

.. http://nehe.gamedev.net/tutorial/beautiful_landscapes_by_means_of_height_mapping/16006/

Upvotes: 0

Related Questions