[Solved] 3rd person camera with Bullet

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.
Post Reply
Nyx Erebos
Posts: 33
Joined: Fri Mar 01, 2013 1:26 am

[Solved] 3rd person camera with Bullet

Post by Nyx Erebos »

After weeks of trying to write a basic 3rd person camera I need some help and an aspirin. I have succeeded in making a camera which follows an Irrlicht node but now I can't get my camera to work with a Bullet body. My math skills are a bit rusty so don't be mad at me if I make an obvious mistake :) . I can't really find what I'm doing wrong :

Code: Select all

 
btRigidBody* body; //which is the body I want to follow, let's say it's already set
scene::ICameraSceneNode* cam; //the camera, already set too
 
core::vector3df offset(0,0,-150); //where I want the camera to be from the body
 
const btQuaternion& TQuat = body->getOrientation();
core::quaternion q(TQuat.getX(), TQuat.getY(), TQuat.getZ(), TQuat.getW());
core::vector3df rotation;
q.toEuler(rotation);
rotation *= core::RADTODEG;
 
core::vector3df direction = rotation.rotationToDirection().normalize();
 
btVector3 bodyPos = body->getCenterOfMassPosition();
core::vector3df position = core::vector3df(bodyPos.x(),bodyPos.y(),bodyPos.z());
 
cam->bindTargetAndRotation(true);
cam->setPosition(position+offset*direction);
cam->setTarget(position);
 
When the body rotates, the camera, instead of rotating with it, goes forward over the z-axis and when the rotation reaches 90° over x or y the camera goes through the center of the body and turn back (to keep looking to the body which is the target). First I thought that I didn't understant how rotationToDirection worked but I use it to say in which direction the body should move and it works. Honestly I have no idea what is going on :cry: . Obviously I tried numerous workaround (with the rotateXYBy methods for example) but nothing worked.
Last edited by Nyx Erebos on Sun Dec 01, 2013 2:27 pm, edited 1 time in total.
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: 3rd person camera with Bullet

Post by hendu »

You are multiplying the direction vector with another vector. That's not what you want, you want to multiply the direction with a scalar.

Ie, your "offset" should be a float, not a vector.
Nyx Erebos
Posts: 33
Joined: Fri Mar 01, 2013 1:26 am

Re: 3rd person camera with Bullet

Post by Nyx Erebos »

Thanks, now that I think about it, it makes perfect sense. Making stupid mistakes like that and spending time over it might be the only thing I dislike about programming.
Post Reply