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.