3rd Person Camera for the Complete Idiot!

A forum to store posts deemed exceptionally wise and useful
Post Reply
dejai
Posts: 522
Joined: Sat Apr 21, 2007 9:00 am

3rd Person Camera for the Complete Idiot!

Post by dejai »

Firstly I should say this was made by an idiot "Me" and it uses some formulas I have got from around the forum over the last several months. I hope to achieve a really simple 3rd person camera in this tutorial that makes the event receiver even easier to use. Its aimed at real beginners, I am not saying this is the right way to code its more of a "quick patch" then anything else but hey it works! (most of the time.. lol)

Ok lets get into it, Outside main.cpp (Globals, or In a Header file, Classes etc..)

Code: Select all

         int speed; // HOW FAST YOU GO
         ICameraSceneNode* camera;
            
          vector3df playerpos; // PLAYERS POSITION
          vector3df playerrot; // PLAYERS ROTATION
    
          bool keys[irr::KEY_KEY_CODES_COUNT]; // THE KEYS!!!
         

           virtual bool OnEvent(const SEvent& event) 
           {
           if(event.EventType == irr::EET_KEY_INPUT_EVENT) 
           {
           keys[event.KeyInput.Key] = event.KeyInput.PressedDown;
           
           return false;
           }

           return false;
           }


In Main.cpp

Code: Select all


             keys[KEY_ESCAPE] = false; 
             keys[KEY_KEY_W] = false; 
             keys[KEY_KEY_S] = false; 
             keys[KEY_KEY_A] = false;
             keys[KEY_KEY_D] = false; 

Now in the while loop, Where Sonic_node is your player node...


Code: Select all

       
     if(keys[KEY_ESCAPE]) 
     {
     device->closeDevice();
     keys[KEY_ESCAPE] = false; 
    }

    if (keys[KEY_KEY_W])
    {
    playerpos.X += speed * cos((playerrot.Y) * 3.14/ 180);
    playerpos.Z -= speed * sin((playerrot.Y) * 3.14/180);
    keys[KEY_KEY_W] = false; 
    }

    if (keys[KEY_KEY_S])
    {
    playerpos.X -= speed * cos((playerrot.Y) * 3.14/180);
    playerpos.Z += speed * sin((playerrot.Y) * 3.14/180);
    keys[KEY_KEY_S] = false; 
    }
    if (keys[KEY_KEY_A])
    {
    playerrot.Y -= 3;
    keys[KEY_KEY_A] = false; 

    }
    if (keys[KEY_KEY_D])
    {
    playerrot.Y += 3;
    keys[KEY_KEY_D] = false; 

    }


    sonic_node->setPosition(playerpos);
    
    sonic_node->setRotation(vector3df(playerrot.X, (playerrot.Y-90), playerrot.Z));
    
    

   sonic_node->addChild(camera);
   vector3df cameratarg = playerpos; // Look at this I declare the variable  every time in the loop! 
   cameratarg.Y += 200; // Needs to be Tinkered With
   camera->setTarget(cameratarg);
  
  
 
        


And thats a cheap hack for the complete idiot, by a complete Idiot. It is easier to understand though.

(I am wearing anti-flame gear thx)
Programming Blog: http://www.uberwolf.com
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Cool.
A little advice, replace all 3.14/180 with
const f32 irr::core::DEGTORAD = PI / 180.0f

32bit Constant for converting from degrees to radians
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Post Reply