Dead Easy 3rd Person Camera

A forum to store posts deemed exceptionally wise and useful
N/A

Dead Easy 3rd Person Camera

Post 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.
Guest

Post 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
Guest

great, but..

Post 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.
N/A

...

Post 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
Guest

Post 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
N/A

oh

Post 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.
Guest

perfect!!!

Post 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
N/A

no problem

Post 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 :).
skajake
Posts: 9
Joined: Sun Sep 12, 2004 6:26 pm

Post by skajake »

I got it all working except for the jittery camera problem. Anyone else figure this out?
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

Post by keless »

Just wanted to say thanks, I used this code as the basis for my camera in IrrRPG just now.
a screen cap is worth 0x100000 DWORDS
Guest

Post by Guest »

im pretty sure that its not the camera but the player movement that causes the jitteryness, skajake
Guest

Post 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.
xterminhate
Posts: 206
Joined: Thu Sep 01, 2005 9:26 pm
Location: France

Post 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.
N/A

Post 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...
Halan
Posts: 447
Joined: Tue Oct 04, 2005 8:17 pm
Location: Germany, Freak City
Contact:

Re: Dead Easy 3rd Person Camera

Post 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
Post Reply