GD script, 3D camera movement

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Ninku
:warning: Old Version Published before Godot 3 was released.

Hi,
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))
 set_rotation(Vector3(deg2rad(pitch),0,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():
	set_fixed_process(true)
	set_process_input(true)

	pass

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))
		set_rotation(Vector3(deg2rad(pitch),0,0))
:bust_in_silhouette: Reply From: Warlaan

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

 set_rotation(Vector3(0,deg2rad(yaw), 0))
 set_rotation(Vector3(deg2rad(pitch),0,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).

But yet continue rotating roll angle

how is possible block this for FPS game?

linuxfree | 2016-09-18 19:41

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

rredesigns | 2017-03-23 04:09

Actually

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))
rotate_x(deg2rad(pitch))

Question/Answer where i found it: https://forum.godotengine.org/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

FinalplayerRyu | 2017-08-11 21:24