IrrNewt - strange rotation

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
xray
Posts: 231
Joined: Fri Feb 02, 2007 1:06 pm
Location: Germany, Munich
Contact:

Post by xray »

We just wanted to do the following. If you could imagine a car driving game. There is a car which you can control with your keyboard through a road cycle. And there is the camera which should be always behind the car. So if you now drive with your car into a curve your mesh will rotate around the y axis. But know you cant grab the Y rotation from this node because if the car drives for example a hill up than it also has a X and/or Z rotation --> and then you get this strange Y rotation numbers if you want to grab it from node->getRotationDegrees() ! And also make the camera a child of the car node isnt really clever as Donner says it looks too stiff.

So here is the routine what Im doing:

Code: Select all

// first create a lookAt vector which will "look" all the time in the
// direction of the character (car, human...)
// but dont give this vector an Y coordinate only X and/or Z coordinates
// for example:

core::vector3df lookAt = core::vector3df(1,0,0);

// now grab the transformation matrix from your character (human,car)

core::matrix4 mat = characterNode->getAbsoluteTransformation();

// and rotate your lookAt vector with rotation part of the matrix
mat.rotateVect(lookAt);

// now ensure no Y coordinates, and maybe normalizing for setting camera
// behind the character
lookAt.Y = 0.f;
lookAt.normalize();

// now you can access to the node y rotation by calling
irr::f32 yAngle = lookAt.getHorizontalAngle().Y;

// or setting directly the camera behind human/car...
cam->setPosition(characterNode->getPosition() - 10.f * lookAt);
cam->setTarget(characterNode->getPosition());

This works for me, very well. And I also can change the length or direction of lookAt vector so that the camera will follow smooth the characterNode.
Post Reply