The objective is to lock the rotation of the object to the camera, so it stays top-right in from of the camera wherever the camera is moved including if it inverts.
As its a 3D object, I can't used the 2D screen overlay.
It seems more complicated that it should be, using getHorizontalAngle or quaternions the logo node can be rotated to stay oriented to the camera, but if the camera goes upsidedown, so does the logo node, as if it was lacking the tilt correction.
I've no idea how to correct for the tilt. If the camera stays upwards, the logo is locked to the top right of the monitor, so it works apart from the tilt.
It seems getHorizontalAngle does not account for tilt, which would cause the problem, but then does the tilt have to be added to the rotated Z-axis of the node by the look-at vector, not just the .z of the rotation...?
Code: Select all
// Rotate node so that it always faces the camera (node is also position offset locked to the camera)
vector3df direction = pCamera->getTarget() - pCamera->getAbsolutePosition();
vector3df requiredRotation = direction.getHorizontalAngle(); // Calculate node XY rotation to direction of camera
vector3df CamUp = pObjectToFace->pCamera->getUpVector();
bool bOk = 1;
f32 ar = CamUp.dotProduct(vector3df(0,1,0); // Angle between default up and camera up
if (fabs(ar) > 1.0f)
bOk = 0;
f32 z = acos(ar) * RADTODEG;
requiredRotation.Z = z; // Add camera tilt to rotation
node->setRotation(requiredRotation + adjRot); // Set rotation with tilt so it tracks camera even if upsidedown