Through reverse engineering, I've found an instruction to inject on which allows me to obtain the XYZ coordinates of the camera, as well as pitch and yaw. Thus, I have the following:

  • camX (Plane) -- Value is a float ranging from negative to positive
  • camY (Plane) -- Value is a float ranging from negative to positive
  • camZ (Up/Down) -- Value is a float ranging from negative to positive
  • camH (Horizontal/Yaw) -- Value is a float in degrees from +180 to -180
  • camV (Vertical/Pitch) -- Value is a float in degrees clamped to +89 to -89

I've written a script in Lua which allows me to bind directions (forward, backward, left, right, up, and down) to keys (W, A, S, D, Q, and E); however, those simply add/subtract to/from XYZ values along with a speed modifier, meaning if I'm facing forward and moving forward, everything is fine. Turning 180 degress with the mouse, though, effectively means back is forward, left is right, etc.

What I'm looking to achieve is having those directional keys apply to wherever I'm aiming the mouse pointer. So, forward is always where I'm facing, etc.

I've been piecing together the necessity of sin/cos for calculations related to this; however, I'm apparently in over my head with understanding everything I'm reading/watching. I've found a number of calculations for this based on degrees, radians, converting degrees to radians, etc., but I'm just completely lost as nothing I've tried has panned out (even though I think I understand what I'm reading).

I've ascertained that, where applicable, respective games have sin/cos values stored somewhere in memory which I could use in lieu of calculating those values myself, but I'm honestly not sure what those values look like to seek out in the first place...or exactly what I need to do to make the calculations myself. They're probably staring me right in the face in nearby memory or on the stack as I trace through code, but I simply don't know.

Are there multiple ways to calculate the appropriate angles via sin/cos? Can I use some combination of the 5 values I listed above, along with a speed modifier and Lua's math.* methods (math.rad, math.sin, math.cos), to get this flycam to work properly?

Thank you for any guidance you can provide!


1 Answer 1


Let's assume your Y axis points forward (Yaw = 0), your X axis points right (Yaw = -90 degrees), and your Z axis pints up (a right-handed coordinate system).

Then you can construct a unit vector in spherical coordinates that points in the direction your camera is looking:

x = -sin(yaw) * cos(pitch)
y =  cos(yaw) * cos(pitch)
z =  sin(pitch)

Multiply this vector by your desired speed (and delta time step) to get the displacement to move your camera forward.

Run the same formulas with yaw - 90 to get the camera's right vector.


You must log in to answer this question.

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