Page 1 of 2

Dead Easy 3rd Person Camera

Posted: Mon Jul 25, 2005 5:55 pm
by N/A
Hi,

Taking a look around at all the 3rd person camera classes, I have decided to make an easier way to implement a 3rd person camera, just by changing some code, and adding some in. Firstly, change the camera to a normal camera, like this:

Code: Select all

camera = sm->addCameraSceneNode(0, core::vector3df(0.0f,0.0f,0.0f) , core::vector3df(0.0f,0.0f,0.0f), -1);
Next, go to the game's main header file (i.e. CGame.h), and add in the following code somewhere:

Code: Select all

float direction;
float zdirection;
Next, in the main file (i.e. main.cpp), put this where you are "initing" the main class, ie where you put: CGame::CGame( ... ) : variables... :

Code: Select all

direction(0), zdirection(0)
Now that we have the direction variables set up, we add in a new function. Go back to the main header file, and put:

Code: Select all

void moveCameraControl();
Next, go back to the main cpp file, and put in somewhere (THIS IS THE 3RD PERSON CAMERA PART):

Code: Select all

void CGame::moveCameraControl()
{
     core::position2d<f32> cursorPos = device->getCursorControl()->getRelativePosition();
     scene::ICameraSceneNode* camera = device->getSceneManager()->getActiveCamera();
     core::vector3df cameraPos = camera->getAbsolutePosition();
     
     float change_x = ( cursorPos.X - 0.5 ) * 256.0f;
     float change_y = ( cursorPos.Y - 0.5 ) * 256.0f;
     direction += change_x;
     zdirection -= change_y;
     if( zdirection <- 90 )
         zdirection = -90;
     else
     if( zdirection > 90 )
         zdirection = 90;
     device->getCursorControl()->setPosition( 0.5f, 0.5f );
     
     core::vector3df playerPos = [PLAYERNODE]->getPosition();
     
     float xf = playerPos.X - cos( direction * PI / 180.0f ) * 64.0f;
     float yf = playerPos.Y - sin( zdirection * PI / 180.0f ) * 64.0f;
     float zf = playerPos.Z + sin( direction * PI / 180.0f ) * 64.0f;
     
     camera->setPosition( core::vector3df( xf, yf, zf ) );
     camera->setTarget( core::vector3df( playerPos.X, playerPos.Y+25.0f, playerPos.Z ) );
     [PLAYERNODE]->setRotation( core::vector3df( 0, direction, 0 ) );
}
Then in the main loop put:

Code: Select all

moveCameraControl();
That's it! Now, if done correctly, you should have a third person camera in! Enjoy!

NOTE: Here i use "zdirection" instead of "ydirection" because I got this code from a GameMaker 6.1 Game I wrote, and I forgot to change it :P

Any questions, just ask.

Posted: Thu Jul 28, 2005 4:45 pm
by Guest
this is amazing! thx! its very smooth, a proper third person camera i compared it to bloodrayne and i prefer this, except the fact that you can't look all the way up

just one thing: the camera sinks into the walls when it moves about. is there any way i can stop this?

ps i am new to irrlicht

great, but..

Posted: Sun Jul 31, 2005 7:36 am
by Guest
this works, but it lacks something ..

for the that I understood there is not orientation, the movement it follows the absolute coordinates and for that when I rotate my person for the mouse input in 180 degrees and I walk to the front, he walks back, It needs to be something that when a rotation is applied in the node, the coordinates rotate together .

I am right?

anybody to help?

Thank you.

...

Posted: Sun Jul 31, 2005 2:53 pm
by N/A
i dont think i understand you correctly, but you're saying that when you rotate the node, the camera doesn't move and/or turn accordingly. if this is what you are saying, then you have to rotate the camera using "direction" and "zdirection", and the node will rotate with the camera. the camera doesn't rotate with the node, it rotates the node

Posted: Sun Jul 31, 2005 5:28 pm
by Guest
No, no.. it is not the camera the problem, they are the relative coordinates of the node, in which think doesn't exist or it is not used .
Besides the absolute coordinates it should have relative coordinates that rotate in the same direction of the node when that rotate, that would make possible the node to move in any direction, not only in the direction XYZ as it is happening.

I wait it was clearer

oh

Posted: Sun Jul 31, 2005 6:46 pm
by N/A
oh I see. I agree, but I will probably not implement this, as this code works fine, and I can move the node in any direction I want (i think :P). but anyway, you may add that in if you wish, but I am not.

if you want the movement code I use (note this is not mine) then it is this:

Code: Select all

irr::core::vector3df facing( sin( ( _node->getRotation().Y + 90.0f ) * PI/180.0f ), 0, cos( ( _node->getRotation().Y + 90.0f ) * PI/180.0f ) ); 
facing.normalize(); 
irr::core::vector3df newPos = (facing*4.0f) + _node->getPosition(); 
_node->setPosition( newPos );
i'm sorry if this does not help, but right now my mind isn't functioning properly.

perfect!!!

Posted: Sun Jul 31, 2005 9:05 pm
by Guest
That is perfect! this turns now possible to move the node and to move according to the orientation, 8) cool!!! 8) .
I believe that few people in that community don't know as implementing this, but this will help many here, as it helped to me ..Thank you. :D

no problem

Posted: Mon Aug 01, 2005 8:04 am
by N/A
thanks for your replies. to make the camera look higher (or lower), just change this part:

Code: Select all

float yf = playerPos.Y - sin( zdirection * PI / 180.0f ) * 64.0f;
to:

Code: Select all

float yf = playerPos.Y - sin( zdirection * PI / 180.0f ) * 128.0f;
or whatever have you.

I'm glad this has helped some people :).

Posted: Wed Aug 24, 2005 4:51 pm
by skajake
I got it all working except for the jittery camera problem. Anyone else figure this out?

Posted: Thu Sep 08, 2005 10:45 pm
by keless
Just wanted to say thanks, I used this code as the basis for my camera in IrrRPG just now.

Posted: Thu Sep 29, 2005 12:09 pm
by Guest
im pretty sure that its not the camera but the player movement that causes the jitteryness, skajake

Posted: Sat Oct 01, 2005 6:36 pm
by Guest
This third party cam is really nice. Source code is clear. :D

I did a smgr->createCollisionResponseAnimator() with the camera node, so that the camera collides with the map mesh.

Thanks.

Xterm-in'Hate.

Posted: Sat Oct 01, 2005 9:35 pm
by xterminhate
The code can be slightly improved. Indeed, xf and zf calculation must take in account zdirection.

So the camera can be placed just above the player node (I couldn't see the breast of my character without this correction 8) ).

Code: Select all

     float xf = playerPos.X - cos( zdirection * irr::core::PI / 180.0f ) * cos( _direction * irr::core::PI / 180.0f ) * 128.0f;
     float zf = playerPos.Z + cos( zdirection * irr::core::PI / 180.0f ) * sin( _direction * irr::core::PI / 180.0f ) * 128.0f;


Thanks,
Xterm-in'Hate.

Posted: Thu Oct 06, 2005 12:31 pm
by N/A
Yeah, your correction does improve it. I haven't been active with Irrlicht recently, so I haven't had the time to make corrections etc...

Re: Dead Easy 3rd Person Camera

Posted: Sat Dec 24, 2005 8:56 pm
by Halan
N/A wrote:Next, in the main file (i.e. main.cpp), put this where you are "initing" the main class, ie where you put: CGame::CGame( ... ) : variables... :

Code: Select all

direction(0), zdirection(0)
sry i dont get it :S