10
\$\begingroup\$

I have a 3rd person camera that doesn't look directly at the player but somewhere in front of him.

When the user enter shooting mode, I want the camera to turn around the player to face the target.

example of what I'm talking about

In the image above. "O" is the player (Origin), "L" is the lookat, "C" is the camera position and "T" is the target. I want to rotate the lookat line C->L so that it passes by T (C'->L'->T') around the Origin ("O").

Basically I need to find the angle alpha that I put in red in the picture.

I store my camera position in a structure like such :

struct CameraTarget {
     Quaternion absoluteRotation;
     Vec3 absolutePosition;
     Vec3 cameraOffset;
     Vec3 lookatOffset;
     float FOV;
}

So if I could find the angle I'm looking for I could do something like :

cam->absoluteRotation = cam->absoluteRotation * alpha;

To get the player to always look at the target.

If the lookat was passing by the Origin I could simply do

Vec3 origDir = cam->lookAtOffset - cam->absolutePosition;
origDir.normalize();
Vec3 newDir = cam->target - cam->absolutePosition;
newDir.normalize();
Quaternion q(origDir, newDir); // from->to
cam->absoluteRotation = cam->absoluteRotation * q;

However in the diagram above this doesn't quite work since the rotation is offset from the Origin.

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Answering to myself if it helps someone else one day :

SFDKT has the right idea about projecting the target point into the current camera look direction.

However, my biggest problem was finding this point P. Turned out that a bit of trigonometry managed to solve it.

Considering the triangle formed by the three points CPO :

  1. Since I know the lengths LO, OC and CL I can calculate the angle at C.

  2. Now I know the angle C and the length of OC and PO. Using the law of sines you can find the angle at O. (I found this website very useful for finding the missing angles)

  3. Then I can find the last missing angle P and use again the law of Sines to find the length CP.

  4. Take the look direction posC + normalized(CL) * lengthCP gives me the position of P.

  5. Once I have P I can calculate the shortest rotation between OP and OT which gives me the quaternion I need to rotate my camera.

I did have a bit of trouble with unwanted roll in 3D but I just solved the problem with Z = 0 then calculated the missing pitch rotation to keep the camera's up vector straight.

\$\endgroup\$
0
\$\begingroup\$

this picture adds the symmetry needed to solve the question easier.

enter image description here

Just project L along the vector CL so that |P-O|=|T-O|.

Now it's obviously just two identical triangles (CPO and C'TO) rotated by your desired angle. I.E. ang(OT)-ang(OP)

C' and L' are both the same as C and L after rotating, about O, by the same amount.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .