13

What I'm trying to do is cast a ray from my camera. I know the camera's x, y and z coordinates, as well as its pitch and yaw. I need to calculate its direction vector so I can pass it to my raytracing algorithm.

The camera's up vector is (0, 1, 0). "Pitch", from the perspective of the camera, is looking up and down.

(I would prefer to not use matrices, but I will if I have to)

genpfault
  • 51,148
  • 11
  • 85
  • 139
RunasSudo
  • 483
  • 2
  • 4
  • 13
  • Why the objection to matrices? That seems like the most logical way to do it for me. Too confusing, too expensive? If you don't do matrices you'll just have to devise it using a lot of trigonometry, which will end up pretty much doing the same thing. – Tim May 13 '12 at 06:06

1 Answers1

14

Assuming that your coordinate system is set up such that the following conditions are met:

(pitch, yaw)  -> (x, y, z)
(0,     0)    -> (1, 0, 0)
(pi/2,  0)    -> (0, 1, 0)
(0,    -pi/2) -> (0, 0, 1)

This will calculate (x, y, z):

xzLen = cos(pitch)
x = xzLen * cos(yaw)
y = sin(pitch)
z = xzLen * sin(-yaw)
Neil Forrester
  • 5,101
  • 29
  • 32
  • Awesome, this helped me out a lot :D – CyanPrime Jun 08 '12 at 17:18
  • @Neil Forrester, what dose xzlen mean ? where can I find math derivation of the formula ? – wangdq May 10 '16 at 12:18
  • `xzLen` is the length of the unit vector in the desired direction after projecting it into the plane containing the x and z axes. The formula follows directly from the definitions of sin() and cos(). – Neil Forrester May 10 '16 at 21:29