Once you calculate the new up, you'll want the lerp from old to new to take a few seconds. Additionally, if CameraToObject
and PlanetToObject
are coincident, a single plane cannot be defined. If the object is already selected, your vertical constraint will prevent CameraToObject
and PlanetToObject
from becoming coincident, however, if the two are already coincident when you click the object, or are made coincident by clicking the object, the algorithm will not work. So, if you go to calculate and detect that they are coincident, bump one a tiny bit; this will find an up vector, which allows the camera to begin lerping, which allows the vertical constraint to work.
The cross-product of the normalized PlanetToObject
and CameraToObject
directions is perpendicular to both vectors and is, by definition, the normal of a plane that contains both. The cross product of the plane's normal (perpendicular to the plane) and CameraToObject
(a vector in the plane) is a second vector in the same plane, perpendicular to the first; the net-result is a rotation of CameraToObject
90 degrees around the face normal.
Note: I may or may not have reversed the order of the cross-products, but you only need to worry about it at the very end; the yellow vector should point to the "right of the camera"; invert it if it doesn't.
This was one of the hardest screenshots I've tried to diagram over; I had given up on making it look right but will post it anyway, since I think you'll still get it.
Regarding quats:
Consider the specific wording of your question. "I want to rotate the image counter-clockwise". That's also saying that you want to rotate the camera clock-wise around CameraToObject
. In that case, CameraToObject
is the Quaternion's vector component, and "90 degrees" is the scalar component. Quaternions twist inputs around their vector component by the angle stored in their scalar component.
A few quaternions youYou didn't know youyou've already knew aboutbeen using quaternions for matrix math? With unit-axis vector components, the quaternion equation reduces to the appropriate axis-rotation matrices: