+2 votes

Hello!
I've been trying to make a first person camera.

var camera = get_node("Camera");
var pitch = rad2deg(camera.get_rotation().x);
var plus_pitch = SENSITIVITY * mouse.relative_y;
var plus_yaw = fmod(SENSITIVITY * mouse.relative_x,360);
camera.global_rotate(Vector3(0,1,0), deg2rad(plus_yaw));
camera.rotate(Vector3(1,0,0),deg2rad(plus_pitch));

This works correctly, I can watch around freely. However the problem arises when I try to limit the camera like so:

plus_pitch = min(plus_pitch, 45 - pitch);
plus_pitch = max(plus_pitch, -45 - pitch);

The problem comes from the fact that apparently, plus_pitch is a negative value. Yet, when I use it with rotate, it causes a positive rotation. Why is this so?
For now, I've just switched its sign to do calculations:

plus_pitch *= -1;
plus_pitch = min(plus_pitch, 45 - pitch);
plus_pitch = max(plus_pitch, -45 - pitch);
plus_pitch *= -1;

This effectively limits the camera to a 90° degree angle, but it causes another problem: if i rotate my camera around (to the left and to the right) it starts spinning for no reason at all, as if I was the yaw actually influenced the pitch. I used global_rotate instead of rotate when using the yaw to be sure I didn't rotate the camera in its space, but rather in world space, so I'm not sure what the problem could be.

TL;DR why does a negative angle cause a positive rotation in rotate()?
why does global_rotate() seem to affect the pitch instead of being, well, global?

in Engine by (14 points)

I'm not 100% sure I understand the question but I have a strong feeling that your problems might be a result from a fact that by default camera is facing a negative z axis (while I'm assuming you were writing this for positive z).

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