Camera Rotation Help

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

I’m trying to rotate the camera in a 360 degree arc. the code I have below works but the camera snaps violently back to 0 when it goes over 360. any way I can smooth out the transition.

func _input(event):
	
   if event.type == InputEvent.MOUSE_MOTION:
	    yaw = fmod(yaw - (event.relative_x * camera_sensitivity), 360);

Also is it possible to get the forward direction of an object.

forward direction in global space: get_global_transform().basis.xform(Vector3(0,1,0))
local space: get_transform().basis.xform(Vector3(0,1,0))

Jatz | 2016-11-29 08:11

is your camera_sensitivity a very small number?
I don’t know for sure, but this might be because you are using degrees when you should be using radians

Jatz | 2016-11-29 08:12

Alternate way of getting forward from any Spatial node (here camera):
var forward = -camera.get_transform().basis.z

Zylann | 2016-11-29 13:24

As Jatz said, you are probably using degrees while the camera expects radians, so snapping to 360 won’t match a U-turn.
How are you using yaw after that to update the camera?

Zylann | 2016-11-29 13:28

Thanks for the response. There are a few things that I can’t seem to understand though. why is the camera forward -/negative? does this mean that backwards is +/positive. is this the same of the other axis’s too? The full Code is below

func _input(event):
	
	if(!lockCamera):
		
		if event.type == InputEvent.MOUSE_MOTION:
			
			pitch = max(min(pitch + (event.relative_y * camera_sensitivity), camera_pitch_minMax.x), camera_pitch_minMax.y);
			yaw = fmod(yaw - (event.relative_x * camera_sensitivity), 360);
	
	if( Input.is_mouse_button_pressed(BUTTON_MIDDLE) && cameraMode == "Battle Cam" ):
		free_movement = !free_movement;
	
	if(free_movement):
		
		#scroll the camera in and out of the battle
		if(Input.is_mouse_button_pressed(BUTTON_WHEEL_UP)):
			radius = max(min(radius - 0.2, max_radius_offset), 1.0);
		
		elif(Input.is_mouse_button_pressed(BUTTON_WHEEL_DOWN)):
			radius = max(min(radius + 0.2, max_radius_offset), 1.0);
		

func CameraMotion(var target, var delta):
	
		#set the camera FOV
		if(get_projection() == Camera.PROJECTION_PERSPECTIVE):
			set_perspective(lerp(get_fov(), camera_fov, camera_smooth_lerp * delta), get_znear(), get_zfar());
		
		current_pitch = lerp(current_pitch, pitch, 10 * delta);
		current_yaw = lerp(current_yaw, yaw, 10 * delta);
		current_radius = lerp(current_radius, radius, 5 * delta)
		
		#get the pivots original position
		camera_pos = target.get_global_transform().origin;
		camera_pos.x += current_radius * sin(deg2rad(current_yaw)) * cos(deg2rad(current_pitch));
		camera_pos.y += current_radius * sin(deg2rad(current_pitch));
		camera_pos.z += current_radius * cos(deg2rad(current_yaw)) * cos(deg2rad(current_pitch));
		#set the position and target to look at
		look_at_from_pos(camera_pos , target.get_global_transform().origin, Vector3(0,1,0));

vonflyhighace2 | 2016-11-30 01:36

About the fact Z is backwards, it comes from OpenGL convention: problem in using transform - Archive - Godot Forum

Also your code isn’t complete, I can’t test it as it is, it seem to rely on a target?

Zylann | 2016-11-30 22:33

var forward = -camera.get_transform().basis.z
is probably the best way, I have really wrapped my head around Matrices yet, but that’s a neat trick to keep in mind.

Jatz | 2016-12-01 10:10