0 votes

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.

in Engine by (271 points)

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

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

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

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?

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 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;


        #scroll the camera in and out of the battle
            radius = max(min(radius - 0.2, max_radius_offset), 1.0);

            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));

About the fact Z is backwards, it comes from OpenGL convention: https://godotengine.org/qa/10322/problem-in-using-transform

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

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.

Please log in or register to answer this question.

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 Frequently asked questions and 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.