0 votes

I am working in 3D. Trying to get my camera to move with the mouse motion. The code below , I just copied and pasted from another demo. I am not a programmer but trying to learn..

currently with code below, the camera only moves up and down. It does not move left and right.
Image > http://tinypic.com/r/11ch5ch/9

So ...
Questions 1. What can i do to make my camera move left and right with my mouse.

Question 2. Can someone explain me in detail what does this part of the code mean ? What is it doing ?

 yaw = fmod(yaw - event.relative_x * view_sensitivity, 360) 
 pitch = max(min(pitch - event.relative_y * view_sensitivity, 90),-90)
 set_rotation(Vector3(0,deg2rad(yaw), 0))

Question 3. what does yaw , pitch, fmod mean? any example of when I would need to use this math function ?
I tried to look it up in the help section etc. But couldn't understand it.

var yaw = 0
var pitch = 0
var view_sensitivity = .3

func _ready():


func _input(event):
    if event.type == InputEvent.MOUSE_MOTION:
        yaw = fmod(yaw - event.relative_x * view_sensitivity, 360) 
        pitch = max(min(pitch - event.relative_y * view_sensitivity, 90),-90)
        set_rotation(Vector3(0,deg2rad(yaw), 0))
in Engine by (37 points)
edited by

1 Answer

+2 votes
Best answer

There's at least one obvious mistake with the code above:

 set_rotation(Vector3(0,deg2rad(yaw), 0))

The second line is overwriting the rotation, so no matter what you set as the yaw it's reset to 0 in the second line. Replace that line with

 set_rotation(Vector3(deg2rad(pitch), deg2rad(yaw), 0))

When you want to store the orientation of something in 3d you have mainly three options:
1. You can store how many degrees something was rotated along which axis. That's very handy if you want to store one rotation, but if an object is rotated several times it's not easy to calculate that into one combined set of values.
2. You can store the orientation as a "quaternion", which is basically a more fancy way of doing the same as in option 1.
3. You can store the orientation as a combination of rotations around the x-, y- and z-axis. That solution has its own problems (the "gimbal lock" is the main one), but it's a lot easier to understand, especially if you want to combine several rotations.
That method is called "using Euler angles", and the three values aren't called x-rotation, y-rotation and z-rotation (unfortunately), they are called "yaw", "pitch" and "roll".
Afaik those words are taken from ships or airplanes. (http://www.aerospaceweb.org/question/dynamics/yaw/controls.jpg)

fmod is a function that is the floating point equivalent of the mathematical modulo function.
When you divide 5 by 2 you get 2.5. If you do so using integer numbers you get 5/2=2, the rest of 0.5 is lost since it can't be stored without using floating point numbers. In school you would say that 5 divided by 2 is 2 "with a rest of 1". Modulo returns that rest, so 5 modulo 2 is 1, since 5 is 1 more than the next lower number that can be divided by 2.
Modulo is often used to create sequences of numbers that stay in a certain range. In this case for example the calculated angle is restricted to numbers between 0 and 360, since an angle of e.g. 410 would be identical to an angle of 50 (since 360 degrees is a full turn and there's no difference between turning 50 degrees and turning 50 degrees + a full turn).

by (1,120 points)
selected by

But yet continue rotating roll angle

how is possible block this for FPS game?

Something funky happens with this script. When my character turned 180 to one side, the pitch works in the opposite way. :|


set_rotation(Vector3(deg2rad(pitch), deg2rad(yaw), 0))

doesn't work in that case as camera will only work as intended at one angle and 90° away from that working angle, the camera will start rolling.

From another example i found the solution to the problem:

set_rotation(Vector3(0, deg2rad(yaw), 0))

Question/Answer where i found it: https://godotengine.org/qa/8120/block-only-roll-coordenate-camera-rotate-many-things-sucess
Code: https://github.com/Zylann/godot_terrain_plugin/blob/master/addons/zylann.terrain/demos/debug_camera.gd#L94

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.