Easiest 3rd Person Camera
Posted: Fri Dec 30, 2005 3:00 pm
I decided that making the camera a child of the playernode was not the best idea because it restricts what you can do with the camera. Keeping that in mind I wrote a little bit of code to make the camera follow a character, position itselft and rotate appropriately based on the platernodes position and roatation. As an added bonus I'll also include code on how to modify the player node to move appropriatley with rotation.
To use the trigonometric funtions like sin and cos you need to include the standard math headers by putting the following at the top of you code.
Fist off, this code goes into the render loop before the draw functions are called.
What it did: It set the camera target to the position of the playernode. Then it used trigonetry to place the camera relatively behind the playernode using the playernodes rototation as a factor and put the camera at a distance of cZoom.
Next, wherever you handle key events declare a position and rotation variable based on the player node. Also define some speed that the playernode will move with.
Then you would put the following code in its appropriate conditional.
What it will do: It will move the playernode about the X,Z axis while repecting the nodes rotation. Meaning that if a character is rotated x degrees to the right or left and movement is called then the node will move while keeping the persoective of the node. It will move forward in the direction the node is facing basically.
Forward:
Reverse:
Strafe Left:
Strafe Right:
Turn Left:
Turn Right:
----------------
I hope that helped some people; especially those who have either needed a third person camera and couldn't get one to work and those who aren't comfortable with trigonometry.
To use the trigonometric funtions like sin and cos you need to include the standard math headers by putting the following at the top of you code.
Code: Select all
#include <math.h>
Code: Select all
int cOffset = 0; // change this to rotate the camera around the character
int cZoom = 200; // radius.. decrease to zoom in, increase to zoom out
core::vector3df r = playernode->getRotation();
core::vector3df p = playernode->getPosition();
camera->setTarget( core::vector3df( playernode->getPosition() ) );
camera->setPosition(
core::vector3df(
( -1 * cZoom * cos( -1 * ( r.Y + cOffset ) * 3.14159265/180 ) ) + p.X,
p.Y+50,
( -1 * cZoom * sin( -1 * ( r.Y + cOffset ) * 3.14159265/180 ) ) + p.Z
)
);
Next, wherever you handle key events declare a position and rotation variable based on the player node. Also define some speed that the playernode will move with.
Code: Select all
float speed = 4.0;
core::vector3df r = playernode->getRotation();
core::vector3df v = playernode->getPosition();
What it will do: It will move the playernode about the X,Z axis while repecting the nodes rotation. Meaning that if a character is rotated x degrees to the right or left and movement is called then the node will move while keeping the persoective of the node. It will move forward in the direction the node is facing basically.
Forward:
Code: Select all
v.X = v.X + ( cos(r.Y * 3.14159265/180)*speed );
v.Z = v.Z - ( sin(r.Y * 3.14159265/180)*speed );
playernode->setPosition(v);
Code: Select all
v.X = v.X - ( cos(r.Y * 3.14159265/180)*speed );
v.Z = v.Z + ( sin(r.Y * 3.14159265/180)*speed );
playernode->setPosition(v);
Code: Select all
v.X = v.X + ( cos((r.Y-90) * 3.14159265/180)*speed );
v.Z = v.Z - ( sin((r.Y-90) * 3.14159265/180)*speed );
playernode->setPosition(v);
Code: Select all
v.X = v.X + ( cos((r.Y+90) * 3.14159265/180)*speed );
v.Z = v.Z - ( sin((r.Y+90) * 3.14159265/180)*speed );
playernode->setPosition(v);
Code: Select all
r.Y = r.Y - 4.0f;
playernode->setRotation(r);
Code: Select all
r.Y = r.Y + 4.0f;
playernode->setRotation(r);
I hope that helped some people; especially those who have either needed a third person camera and couldn't get one to work and those who aren't comfortable with trigonometry.