2
\$\begingroup\$

I'm making a camera that can currently rotate freely from the back to the front of a target object by giving it an angle and a distance.

enter image description here

I do the above with the following code

directionVector = new Vector3(0, Mathf.Sin (angle*Mathf.Deg2Rad), Mathf.Cos(Mathf.Deg2Rad * angle));
normalizedDirectionVector = (directionVector).normalized;

transform.position = (normalizedDirectionVector*cameraDistance) + target.transform.position;

In the above I take the direction from the angle, normalize it and then add it to the target position. And the camera sets nicely to the wanted angle. But how can I add the sideways movement(x-component) so that I could move the camera freely around the target?

enter image description here

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

The manpage for gluLookAt might be of use. That is deprecated now days in OpenGL itself, but glm has an implementation. Then you just use that plus basic trigonometry for working out the X/Y coordinates of the camera.

\$\endgroup\$
1
\$\begingroup\$

You could use engine features to avoid trigonometry entirely. Another way to achieve this is to do the following steps:

  1. Set the camera's position equal to the target position.
  2. Rotate the camera such that it points in the direction you want.
  3. Add the desired displacement to the local translation of the camera. This will automatically move the camera away from the target. (Note: depending on the axes you are using, this might mean adding <0, 0, orbitRadius> or <0, 0, -orbitRadius>.)

It looks like you're using Unity, which has the tools to make this extremely simple.

\$\endgroup\$
1
\$\begingroup\$

Google Euler angle conversion for more info. To get a direction vector from a pitch and a yaw (pitch is what you have now and yaw is around the vertical axis) you want this:

x = cos(yaw)*cos(pitch)
y = sin(yaw)*cos(pitch)
z = sin(pitch)
\$\endgroup\$
1
  • \$\begingroup\$ I tried this approach it worked fantastically when e.g editing yaw with pitch as zero. But when both had values the rotation became limited. \$\endgroup\$ – Esa Oct 11 '13 at 5:28
0
\$\begingroup\$

I solved this by using the original angle vector and then using the equation that @nykwil provided, but only using the pitch component. Then I combined the y-component of the angle vector to the pitch and I got the result I wanted.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.