Height map collision

Morning

I started with my first attempt to create and render randomly genarated height maps with LWJGL.
The heightmap is rendered using triangle strips from BufferedImage genarated from Perlin noise.
I can fly over the terrain and parse through it, but now I want to implement collision detection
so that the camera always stays at a fixed height above the ground.

My camera holds a vector3 (x,y,z) for the position in 3D space and my height map stores a 2D array of height values. When i render the map i have a nested for loop that places a vertices where the need to be.


for (int z = 0; z < values.length - 1; z++) {
     GL11.glBegin(GL_TRIANGLE_STRIP);
     for (int x = 0; x < values[z].length; x++) {
          GL11.glVertex3f(x, values[z][x], z);
          GL11.glVertex3f(x, values[z + 1][x], z + 1);
     }
     GL11.glEnd();
     GL11.glFlush();
}

the way is thought of doing is having a boolean method that returns whether I point is below the height map and there just translate the camera until its position is no longer below the map. (hope this makes sense)


// like this or somthing similar
while (map.isBelow(camera.getPosition())) {
     camera.translate(0, 1, 0);
}

is there any way to do this, assuming this is the best way and if not what is the best way?

thanks in advance,
siD

I resolved the same issue for myself a few months ago by interpolating like so:


public float interpolateHeight(float posX, float posZ) {
    try {
        int x = (int) Math.floor(posX);
        int z = (int) Math.floor(posZ);
        float x0 = posX - x;
        float z0 = posZ - z;
        return ((heights[x][z] * (1.0f - x0) + heights[x + 1][z] * x0) * (1.0f - z0))
                + (z0 * (heights[x][z + 1] * (1.0f - x0) + heights[x + 1][z + 1] * x0));
    } catch (Exception e) {
        return 0;
    }
}

This will return the surface height of the terrain.

So try this:


cam.y = interpolateHeight(cam.x, cam,z);

thanks! works great! ;D