3D Camera - Pitch not working?

Hi everybody. I’m new to this board and could use some help :frowning:
I am using LWJGL and tried implementing a 3D Camera using this tutorial: http://www.lloydgoodall.com/tutorials/first-person-camera-control-with-lwjgl/
In the walkForward-Method I want to implement moving on the Y-axis too, because I want to achieve a “freelook” kind of movement. I am using tan to calculate this movement:


position.x -= distance * Math.sin(Math.toRadians(yaw));
position.y += distance * Math.tan(Math.toRadians(pitch));
position.z += distance * Math.cos(Math.toRadians(yaw));

The problem is, that when I look straight up or down, the position gets increased by thousands and I get “teleported” away instantly.
Can anyone help me with that or tell me why this is happening?

Cheers
womb34

It’s a little more complex than that. Look here:

https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSr0U3xKEIT61RP5dPZPVz759r8S4m6rGwgHGckbtertQ79P4-Q

So for pitch, yaw and roll you need to to use two triangles perpendicular. The hypotenuse of the first becomes the adjacent of the second. It’s a little hard to get clear in your head so you can try or just take my word for it and use this formula:

x = distance * sin(pitch);
z = distance * cos(pitch) * cos(yaw);
y = distance * cos(pitch) * sin(yaw);

and a little proof that the overall magnitude is the same (because I am always unsure of this)

Length^2 = x^2 + y^2 + z^2
Length^2 = d^2 * sin^2(pitch) + d^2 * cos^2(pitch) * sin^2(yaw) + d^2 * cos^2(pitch) * cos^2(yaw)
Length^2 = d^2 ( sin^2(pitch) + cos^2(pitch) * sin^2(yaw) + cos^2(pitch) * cos^2(yaw) )
Length^2 = d^2 ( sin^2(pitch) + cos^2(pitch) ( sin^2(yaw) + cos^2(yaw) ) )
Length^2 = d^2 ( sin^2(pitch) + cos^2(pitch) (1) )
Length^2 = d^2 ( 1 )
Length = distance

Mhm I’m still unable to get it working. Maybe my implementation of the entire Camera is wrong? http://pastebin.com/FTaYXc8K The problematic functions are in the lines 67 and 76.
Do you, by any chance, have a link to read more about 3D First-Person-Cameras? :slight_smile:

I don’t remember any good links and I don’t seem to have any bookmarks to any either. But I think I know what your problem is. At the end of the tick() method, you actually pass the transformations along to OpenGL but you rotate to face the right direction before translating to the right position. That means that the translation is in the terms of the rotated coordinate axis and translates in the wrong direction. Try translating before rotating.

I just tried it and it made things even worse. The camera moves, depending on the view direction, moves in a completely wrong direction and sometimes “teleports” away :frowning: