![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | threenuc |
I’m creating a simple camera look script that moves the camera based on mouse movement.
It works like this (the following is called after an InputEventMouseMotion event)
- Take mouse position from last frame
- Take current mouse position
- Rotate the camera by the difference between mouse positions
- Save current mouse position so it can be reused as “mouse position from last frame” in the next frame.
The problem is that when the cursor is on the edge of the screen, the difference of mouse positions between two frames will be the same. The camera wont rotate.
What I’ve tried is adding another step to the above algorithm:
- Reset the mouse cursor to the center of the screen
(using get_viewport.warp_mouse(Vector2(windowWidth/2, windowHeight/2))
The problem is: a warp_mouse() call triggers the InputEventMouseMotion event.
Here’s an example frame by frame, assuming we start from the center of the screen:
- Move mouse 1 pixel to the left (MouseMotion event triggers)
- Last mousepos is center of the screen → (screenwidth/2, screenheight/2)
- Turn camera 1 degree left
- Current mousepos is 1 pixel to the left from center
- Warp mouse back to center with warp_mouse() (MouseMotion event still triggers)
- Last mousepos is 1 pixel to the left from center
- Current mousepos is center
- Turn camera 1 degree right
So it will always turn the camera back to center after 1 frame (which is visible after I limit Godot FPS to 2 also).
Here’s the code:
var lastFrameMouseposX=0;
var lastFrameMouseposY=0;
func _input(event):
if event is InputEventMouseMotion:
_moveCameraWithMouse(event);
pass
func _moveCameraWithMouse(event):
var mousepos = event.position;
var sensitivity = 0.01;
#var lastFrameMouseposX; these are global variables defined at the beginning of this script
#var lastFrameMouseposY;
var moveCameraOnX=(lastFrameMouseposX - mousepos.x) * sensitivity;
var moveCameraOnY=(lastFrameMouseposY - mousepos.y) * sensitivity;
var potentialCameraXAngleOnNextFrame = rad2deg(rotation.x+moveCameraOnY);
if (_isBetween(potentialCameraXAngleOnNextFrame, -90, 90)):
rotation += Vector3(moveCameraOnY, moveCameraOnX,0);
lastFrameMouseposX=mousepos.x;
lastFrameMouseposY=mousepos.y;
var windowsize=get_viewport().size;
get_viewport().warp_mouse(Vector2(windowsize.x/2, windowsize.y/2));
pass
The fact that warp_mouse triggers a mousemove is pretty weird in itself. I’m just learning but isn’t it something one could file an github issue for (to create a ignore flag for warp_mouse)? Obviously I’m asking here to make sure I’m not making a mistake here.
What do I do to make edge look work?