Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

i wanna walk to terrain... first i created the terrain

void desenha_terreno(float px, float pz){


for (int z = 0; z < iwidth-1; z++)
{
    glBegin(GL_TRIANGLE_STRIP);

    for (int x = 0; x < iheight-1; x++)
    {
        // for each vertex, we calculate the grayscale shade color, 
        // we set the texture coordinate, and we draw the vertex.
        /*
           the vertices are drawn in this order:

           0  ---> 1
                  /
                /
             |/
           2  ---> 3
        */

        // draw vertex 0
        glColor3f(terreno[x][z][1]/255.0f, terreno[x][z][1]/255.0f, terreno[x][z][1]/255.0f);
        glTexCoord2f(0.0f, 0.0f);
        glVertex3f(terreno[x][z][0], terreno[x][z][1], terreno[x][z][2]);

        // draw vertex 1
        glTexCoord2f(1.0f, 0.0f);
        glColor3f(terreno[x+1][z][1]/255.0f, terreno[x+1][z][1]/255.0f, terreno[x+1][z][1]/255.0f);
        glVertex3f(terreno[x+1][z][0], terreno[x+1][z][1], terreno[x+1][z][2]);

        // draw vertex 2
        glTexCoord2f(0.0f, 1.0f);
        glColor3f(terreno[x][z+1][1]/255.0f, terreno[x][z+1][1]/255.0f, terreno[x][z+1][1]/255.0f);
        glVertex3f(terreno[x][z+1][0], terreno[x][z+1][1], terreno[x][z+1][2]);

        // draw vertex 3
        glColor3f(terreno[x+1][z+1][1]/255.0f, terreno[x+1][z+1][1]/255.0f, terreno[x+1][z+1][1]/255.0f);
        glTexCoord2f(1.0f, 1.0f);
        glVertex3f(terreno[x+1][z+1][0], terreno[x+1][z+1][1], terreno[x+1][z+1][2]);


    }
    glEnd();
}
}

I can move the camera with the functions:

void keyboard (unsigned char key, int x, int y) {
if (key=='w')
{
    float xrotrad, yrotrad;
    yrotrad = (yrot / 180 * 3.141592654f);
    xrotrad = (xrot / 180 * 3.141592654f); 
    xpos += float(sin(yrotrad))*5;
    zpos -= float(cos(yrotrad))*5;
    ypos -= float(sin(xrotrad))*5;

}

if (key=='s')
{
    float xrotrad, yrotrad;
    yrotrad = (yrot / 180 * 3.141592654f);
    xrotrad = (xrot / 180 * 3.141592654f); 
    xpos -= float(sin(yrotrad))*5;
    zpos += float(cos(yrotrad))*5;
    ypos += float(sin(xrotrad))*5;
}

if (key=='d')
{
    float yrotrad;
    yrotrad = (yrot / 180 * 3.141592654f);
    xpos += float(cos(yrotrad))*5;
    zpos += float(sin(yrotrad))*5;
}

if (key=='a')
{
    float yrotrad;
    yrotrad = (yrot / 180 * 3.141592654f);
    xpos -= float(cos(yrotrad))*5;
    zpos -= float(sin(yrotrad))*5;
}

if (key=='q')
{
    yrot += (float) -0.5*5;    //set the xrot to yrot with the addition of the difference in the x position
}

if (key=='e')
{
    yrot += (float) 0.5*5;    //set the xrot to yrot with the addition of the difference in the x position
}

if (key=='r')
{
    xrot += (float) -0.5*5;    //set the xrot to yrot with the addition of the difference in the x position
}

if (key=='f')
{
    xrot += (float) 0.5*5;    //set the xrot to yrot with the addition of the difference in the x position
}

if (key==27)
{
    exit(0);
}

glutPostRedisplay();
`

and, i change the position with the values of x,y,z positions

    // clear screen and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);     
glLoadIdentity();

//glTranslatef(0.0f, 0.0f, -cRadius);
glRotatef(xrot,1.0,0.0,0.0);
glRotatef(yrot,0.0,1.0,0.0);  //rotate our camera on the    y-axis (up and down)

glTranslated(-xpos,-ypos,-zpos); //translate the screen to the position of our camera


// set the camera position
//gluLookAt(cameraX, cameraY, cameraZ, lookX, lookY, lookZ, 0.0, 1.0, 0.0);

// set the current texture to the land texture
//glBindTexture(GL_TEXTURE_2D, land);


    desenha_terreno(xpos,ypos);
    glEnable(GL_BLEND);

// enable read-only depth buffer
glDepthMask(GL_FALSE);

// set the blend function to what we use for transparency
glBlendFunc(GL_SRC_ALPHA, GL_ONE);

// set back to normal depth buffer mode (writable)
glDepthMask(GL_TRUE);

// disable blending
glDisable(GL_BLEND);

    glFlush();


    glutSwapBuffers();

    angle++; //increase the angle
   }

well... i need to walk to the terrain, above the terrain, help me please!

share|improve this question

migrated from stackoverflow.com Aug 12 '11 at 18:02

This question came from our site for professional and enthusiast programmers.

1  
Jeez, wall of code much? What have you tried? What do you know about what you're trying to accomplish? –  Jonathan Grynspan Jun 3 '11 at 20:39
7  
Are you talking about simulating walking on terrain as if the player is the camera? It isn't difficult to adjust your Z value as you move by simply querying your terrain and getting a height to which you'd apply your offset to. books.google.com/… –  Robb Jun 3 '11 at 21:00
    
yes, simulating walking as a player... thanks Robb, i will see the link! –  Gabriel Jun 3 '11 at 21:10
4  
Honestly, who upvotes such questions? –  pmr Aug 12 '11 at 16:34

3 Answers 3

The answer is very simple.

When you render terrain you supply 3 values, x,y,z. Lets assume that z value is height. So when walking you just need to keep camera above z value! You get camera x,y query height in that spot and place camera above z value.When there is no z value for this spot interpolate from near by points.

Thats all.

You also may want to keep camera some constant above terrain itself (to give illusion of being physical character).

You also may want to put all height in one array. And assume that x and y change by const interval, to simplify your terrain generation. And you get height-map!

share|improve this answer

You may have some luck with using gluLookAt() to manipulate the camera. It'd probably be easier to move the camera position and the point that it's looking at instead of rotating the camera yourself. This would also simplify doing things like "looking around" from a stationary point.

share|improve this answer

The best way to make sure you're above a triangle is to use barycentric coordinates. In short, this is a coordinate system local to a triangle that allows you to get the height of a point somewhere in the interior of the triangle.

So your algorithm would go something like:

  • Get the camera/player position
  • Figure out what triangle that player is on (assuming you have a regular grid of triangles in the (x,y) plane this should be trivial).
  • Using the 3 points of that triangle convert the camera point to triangle space and use the barycentric coordinates to solve for the height.
  • Unproject it from triangle space back to world space and you'll have the point on the triangle where the camera should be. You'll probably want to move this up so that it appears the camera is on the terrain proper.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.