Page 1 of 1

[Solved] 3rd person camera with Bullet

Posted: Sat Nov 30, 2013 3:39 pm
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.

Re: 3rd person camera with Bullet

Posted: Sat Nov 30, 2013 6:00 pm
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.

Re: 3rd person camera with Bullet

Posted: Sun Dec 01, 2013 2:26 pm
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.