Camera != below terrain.

This topic is 4544 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi, Like the title says Im trying to get my camera to always be on the terrain and not getting below it when moving up and down. The things I have is. CameraPos. upDownRotation. Is there any easy and fast smart ways to do this? I have try something like this. float hight = pTerrain.GetHight( x, z ); if( cameraPos.y <= hight ){ upDownRotation += rotationSensitivity; } or maybe something like this: upDownRotation=(180.0/M_PI)*atan(cameraPos.y/sqrt((cameraPos.x*cameraPos.x)+ (cameraPos.z*cameraPos.z))); but I dont get it working. I know there is alot better ways to do this. Please help =) Thanks all.

Share on other sites
It's not clear what you are trying to do. Why do you rotate the camera if it is below the ground? If you want the camera to always be on the ground, wouldn't you just write this?
    float hight = pTerrain.GetHight( x, z );    cameraPos.y = hight;
Or if you just want to prevent the camera from going below the ground, would this work?
    float hight = pTerrain.GetHight( x, z );    if ( cameraPos.y < hight )         cameraPos.y = hight;
I guess there is a problem if the camera is very close to the ground -- the ground can cut through the front view plane. In that case you can keep the camera at a minimum height above the ground, or you can rotate the camera up so that the ground does not cut through the front plane. The latter is very difficult to do.

Share on other sites
Is it a 3rd person camera that you want to always be on the top of the terrain and also have the player model in line of sight? Otherwise just move the camera's y position to the terrain level + some delta value.

And it's spelled "height" :P

Share on other sites
Well, the problem is I cant just do like JohnBolton means because the camera uses an matrix for the view and the camera is behind the character.

well it looks like this.

static MATRIX4X4 tempMatrix;
glPushMatrix();
glTranslatef( LookAt.x, LookAt.y, LookAt.z );
glRotated( -LeftRightRotation, 0.0f, 1.0f, 0.0f );
glRotated( -upDownRotation, 1.0f, 0.0f, 0.0f );
glTranslatef( 0.0f, 0.0f, rZoom );

Now I get the modelview matrix;

glGetFloatv( GL_MODELVIEW_MATRIX, tempMatrix );

And the position of the camera will be like this;

Vector3 position = tempMatrix.GetColumn( 3 );

Column = matrix 12,13,14,15.

Now I can get the postion were the camera exacly is when I have it zoomed away from the character.

Maybe I can do like this?

if( AngleOfUpRotation < m_terrain.getHeight( position.x, position.z ) + 5.0f )
{
upDownRotation += rotateUpDownSpeed+0.01f;
}

so when I rotate the camera below the terrain it rotate it up again, but I dont know exalt how to calculate the rotation angle for the AngleOfUpRotation and also check if the rotation collide with the terrain or something.. I dont realy know.

if you know any to do this please tell me :)

I hope you guys know what I mean :/ Im realy bad to explain things...

Well Thanks and cheers!

ps: Im not so good at english sry.

Share on other sites
Before rotating by -upDownRotation, you need to see if it will put the camera below the ground and then limit the rotation if it does.

The lowest allowed value of -upDownRotation would be asin( ( pTerrain.GetHight( CameraPos.x, CameraPos.z ) - lookAt.y ) / rZoom ). However, this is complicated by the fact that CameraPos.x and CameraPos.z (and thus pTerrain.GetHight()) depend on the angle. That implies an iterative approach where if the camera is below the ground you limit the rotation and then check again.

One more thing. When using OpenGL, it is usually better to do the math yourself, rather than using glRotate, glTranslate, and glGetFloatv( GL_MODELVIEW_MATRIX, ...). Even though it results in more code, it usually ends up being simpler (as in this case), and it may even be faster.

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 28
• 16
• 10
• 10
• 11
• Forum Statistics

• Total Topics
634113
• Total Posts
3015570
×