CODE: position camera on terrain w/o glitches nor physics

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
catron
Posts: 158
Joined: Mon Feb 19, 2007 1:54 am

CODE: position camera on terrain w/o glitches nor physics

Post by catron »

this is how to position a camera above a terrain without expensive physics nor the glitchyness of irrlicht's collision. heres the code:

Code: Select all

        //Heres the function to place in a header or main.cpp      
        
bool placeCam()  {

core::vector3df campos, setcam;
float Y1, Y2, Y3, Y;
float lift = 10; //may need to change depending on speed of cam/object
campos = camera->getPosition();

Y1 = terrain->getHeight(campos.X , campos.Z);

Y2 = terrain->getHeight(campos.X+3.1 , campos.Z+3.1);

Y3 = terrain->getHeight(campos.X-.1 , campos.Z-.1);

Y = ((Y1 + Y2 + Y3)+lift)/3;

setcam = core::vector3df(campos.X, Y+5.0, campos.Z); //dont change 5.0, change lift variable

camera->setPosition(setcam);

return true;
}

//in your game loop put
placeCam();
there ya go! please post any questions and/or improvements

EDIT: you need to remove your collision detector with the terrain
Last edited by catron on Sat Jul 14, 2007 6:11 pm, edited 1 time in total.
Katsankat
Posts: 178
Joined: Sun Mar 12, 2006 4:15 am
Contact:

Post by Katsankat »

The code is nice for anything that does not jump: cam is always fixed to the same height above ground, like for a heavy tank. Since tanks can jump too here's some improvement: gravity.

Code: Select all

   f32 HeightAboveGround=10.f;
   f32 Gravity = 0.8f;
   // In main loop
       if (Gravity)
       {
         float Y = terrain->getHeight(pos.X, pos.Z);
         if (Y != -999999.9f) // if no ground under, do nothing
         {
          vector3df pos = camera->getPosition();
          pos.Y -= Gravity;
          Y += HeightAboveGround;
          if (pos.Y > Y) camera->setPosition(pos);
          else camera->setPosition(vector3df(pos.X, Y, pos.Z));
         }
       }
Post Reply