Page 1 of 1

position calculation problem

Posted: Sun Aug 19, 2007 7:40 pm
by gammaray
Hello, it's me again:)

I'm trying to implement a sort of chase cam, that stays sticky to it's target (effect: the target looks still, only the world rotates). I use the following code to calculate the camera position:
/* fix main cam stuff */
matrix4 mat = getSceneNode()->getRelativeTransformation();
matrix4 fixmat;
fixmat.setRotationDegrees(mat.getRotationDegrees());

// calculate cam position and target
vector3df lookat(0,0,(getSceneNode()->getBoundingBox().getExtent().Z + 1));
mat.transformVect(lookat);

// calculate cameras up vector
vector3df up(0,1,0);
fixmat.transformVect(up);
up.normalize();

vector3df campos(0,2,-3);
mat.transformVect(campos);

cams[E_CAMERA_FRONT]->setUpVector(up);
cams[E_CAMERA_FRONT]->setPosition(campos);
cams[E_CAMERA_FRONT]->setTarget(lookat);
Now in my app, the position and target get updated correctly, but there seem to be problems with the up vector - camera rotation doesn't match the rotation of the target.
What's strange, I use the same code in my other test app and it works like it should.
The main difference between both apps is that the first (not working) updates the camera from within a class method, the second one (working) updates the cam from within irrlicht main loop, without use of class methods - can it have any influence?

Any ideas what am I doing wrong?

Posted: Sun Aug 19, 2007 9:20 pm
by Acki
It should not matter if you do this in a class or in the main loop...
Sometimes I had problems with a camera when I changed the position and/or target of it and forgot to call updateAbsolutePosition on the camera afterwards, maybe this is your peoblem too...

Re:

Posted: Sun Aug 19, 2007 9:37 pm
by gammaray
The position itself seems to be ok, only the up vector gets screwed somehow.
I've checked the output of camera->getUpVector() and it seems to be updated correctly, but the cam behaves almost the same as with a non-transformed (0,1,0). And whats weird in the other app it behaves like it should while the only difference is the classes layout;/ Am' totally confused...

solved:)

Posted: Mon Aug 20, 2007 5:31 pm
by gammaray
/* fix main cam stuff */
matrix4 mat;

mat.setTranslation(getSceneNode()->getAbsolutePosition());
mat.setRotationDegrees(getSceneNode()->getRotation());

matrix4 fixmat;
fixmat.setRotationDegrees(mat.getRotationDegrees());
Slight change in the code and it works now. I was convinced that mat = SceneNode->getAbsolute/RelativeTransformation should produce the same effect as the solution above? What's the difference? I'd be grateful for an explanation:) Cheers!

Posted: Mon Aug 20, 2007 6:10 pm
by Nextor
Wouldn't be simpler to create an empty node at/near the position of target and parented to it, and then, parent the camera to the empty node? So, your camera always follows the target, and if you want to rotate the camera around the target, you only have to rotate the empty node.
That's the way I did it in one of my projects.