Hey!
I have been racking my brain for about a day and a half now. I know the answer has to be something simple, but for the life of me I can't deduce the simple equation/algorithym.
I built a randomly generated heightmap class. I have a working camera class that allows you to walk around in 3d space on this map, but I am having a heck of a time trying to figure out how keep the camera on the terrain at all times. The heightmap is declared as follows.
Code:
struct Vector3{
Vector3();
Vector3(float x, float y, float z);
float X;
float Y;
float Z;
};
struct Triangle{
Vector3 VertexA;
Vector3 VertexB;
Vector3 VertexC;
};
class HeightMap{
private:
int Length, Width, MaxHeight, MinHeight, Scale, CollisionIndex;
std::vector <int> Heights;
Triangle CollisionTriangleA;
Triangle CollisionTriangleB;
Vector3 Origin;
public:
HeightMap(int L, int W, int MaxH, int MinH, int scale, Vector3 origin);
void Generate();
void Draw();
float GetHeightOf(int index);
float GetHeight(float x, float z);
Triangle GetColTriA();
Triangle GetColTriB();
};
And the map is drawn like:
Code:
void HeightMap::Draw(){
int HeightIndex = 0;
glTranslatef(Origin.X, Origin.Y, Origin.Z);
glBegin(GL_TRIANGLES);
for(int w = 0; w < Width-1; w++){
for(int l = 0; l < Length-1; l++){
if(CollisionIndex == HeightIndex){
glEnd(); // this draws the two triangles as wirefram so that I can see the collision detection is working properly
glPolygonMode(GL_FRONT, GL_LINE);
glPolygonMode(GL_BACK, GL_LINE);
// save the Triangles Coordinates for debugging the collision algorithym
CollisionTriangleA.VertexA = Vector3(float(l*Scale), float(Heights.at(HeightIndex)), float(w*Scale));
CollisionTriangleA.VertexB = Vector3(float((l+1)*Scale), float(Heights.at(HeightIndex+1)), float(w*Scale));
CollisionTriangleA.VertexC = Vector3(float(l*Scale), float(Heights.at(HeightIndex+Width)), float((w+1)*Scale));
CollisionTriangleB.VertexA = Vector3(float(l*Scale), float(Heights.at(HeightIndex+Width)), float((w+1)*Scale));
CollisionTriangleB.VertexB = Vector3(float((l+1)*Scale), float(Heights.at(HeightIndex+1)), float(w*Scale));
CollisionTriangleB.VertexC = Vector3(float((l+1)*Scale), float(Heights.at(HeightIndex+Width+1)), float((w+1)*Scale));
glBegin(GL_TRIANGLES);
}
glTexCoord2f(0.0f, 2.0f); glVertex3f(float(l*Scale), float(Heights.at(HeightIndex)), float(w*Scale));
glTexCoord2f(2.0f, 2.0f); glVertex3f(float((l+1)*Scale), float(Heights.at(HeightIndex+1)), float(w*Scale));
glTexCoord2f(0.0f, 0.0f); glVertex3f(float(l*Scale), float(Heights.at(HeightIndex+Width)), float((w+1)*Scale));
glTexCoord2f(0.0f, 0.0f); glVertex3f(float(l*Scale), float(Heights.at(HeightIndex+Width)), float((w+1)*Scale));
glTexCoord2f(2.0f, 2.0f); glVertex3f(float((l+1)*Scale), float(Heights.at(HeightIndex+1)), float(w*Scale));
glTexCoord2d(2.0f, 0.0f); glVertex3f(float((l+1)*Scale), float(Heights.at(HeightIndex+Width+1)), float((w+1)*Scale));
if(CollisionIndex == HeightIndex){
glEnd();
glPolygonMode(GL_FRONT, GL_FILL);
glPolygonMode(GL_BACK, GL_FILL);
glBegin(GL_TRIANGLES);
}
HeightIndex++;
}
HeightIndex++;
}
glEnd();
}
The above draws the heigh map based off a int value in a vector called "heights" and the correct triangles that I am "standing" on are drawn in a wire frame.
So at this point I have the value of all the vertices stored in the CollisionTriangleA and CollisionTriangleB and I have the X and Z coordinate of the camera, but I can't figure out how to calculate the Camera Y coordinate based off this.
I hope I explained this clearly enough. If you can drop a link or tip I'd really appreciate it ;o).
Thanks!
- John
Here is a video of it as well you can see the the triangles below the camera are rendered in wireframe and the coordinates are displayed in the debug window to the left. Sorry about the background noise...I recorded it in a cafe.
https://www.youtube.com/watch?v=qYBTyEkm9e8