3rd Person view

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Daniel

3rd Person view

Post by Daniel »

Can someone please show me how to add/use a 3rd person camera view on a character?
after-life
Posts: 69
Joined: Wed Mar 30, 2005 8:16 am
Location: Keerbergen, Belgium

Post by after-life »

1) use setTarget to set your player as the target the camera is looking at
2) then: use sin,cos stuff to set the height, ...
3) make it so that in the event receiver it can rotate the camera and zoom in/out
4) update the camera location every time the player moves or the camera is rotated (do 1 and 2)


AND CHECK THE FORUM
mrweet
Posts: 5
Joined: Fri Jun 17, 2005 3:13 am

Post by mrweet »

virtual void irr::scene::ICameraSceneNode::setTarget ( const core::vector3df & pos ) [pure virtual]

It says your supposed to replace the pos with what you want the camera to look at. What would you put in there though?
after-life
Posts: 69
Joined: Wed Mar 30, 2005 8:16 am
Location: Keerbergen, Belgium

Post by after-life »

here is some code:

Code: Select all

position2d<f32> RelMousePos = device->getCursorControl()->getRelativePosition();         
if(RelMousePos.X >= 0.8)
{
    float rotation = (RelMousePos.X-0.8)*10;
    fAngleX += rotation;
    if(fAngleX >= 360)
         fAngleX = 0;
}
if(RelMousePos.X <= 0.2)
{             
float rotation = (0.2-RelMousePos.X)*10;
fAngleX -= rotation;
if(fAngleX <= 0)
    fAngleX = 360;
}
if(RelMousePos.Y >= 0.8)
{
    float rotation = (RelMousePos.Y-0.8)*10;
    fAngleY -= rotation;
    if(fAngleY <= 20)
        fAngleY = 20;
}
if(RelMousePos.Y <= 0.2)
{
    float rotation = (0.2-RelMousePos.Y)*10;
    fAngleY += rotation;
    if(fAngleY >= 75)
         fAngleY = 75;
}

vector3df CameraPos;
ICameraSceneNode* camera = smgr->getActiveCamera();
float fXZDistance = CameraDistance*cos(PI*(fAngleY/180));
CameraPos.Y = playerpos.Y + CameraDistance*sin(PI*(fAngleY/180));
CameraPos.X = playerpos.X + cos(PI*(fAngleX/180))*fXZDistance;
CameraPos.Z = playerpos.Z + sin(PI*(fAngleX/180))*fXZDistance;
camera->setPosition(CameraPos);
camera->setTarget(vector3df(playerpos.X,playerpos.Y,playerpos.Z));
CameraDistance is a float which shoul be adjustable by scrolling (im not going to explain that part).
All the rest should be obvious.
You can adjust a few things (the rotation and stuff) for example: the camera is now facing the feet of the player, you can adjust it by doing:

camera->setTarget(vector3df(playerpos.X,playerpos.Y+20,playerpos.Z));
Post Reply