Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | MisterAcoustic |
I made an attempt at controlling the orientation of a ship in space in 3d according to the relative mouse motion. The camera is attached to the ship, so parts of the ship appear fixed, and the background panorama sky (star background) seems to move.
The motion I achieve is really jumpy - instead of moving smoothly, each relative change of the mouse causes a largish change in the movement of the stars.
How can I make this smoother?
I didn’t post queryMouseMoveEvent - it just checks if the relative mouse motion would cause the motion being passed in. And the hard coded numbers 1 and 0 represent the x axis and the z axis.
var lastMousePosition = Vector2(0,0)
func _input(event):
if event is InputEventMouseMotion:
lastMousePosition = event.relative
func _process(delta):
if queryMouseMoveEvent("YawLeft"):
ship.rotateShip(1, deg2rad(lastMousePosition.x), false, MouseSensitivity, delta)
if queryMouseMoveEvent("YawRight"):
ship.rotateShip(1, deg2rad(lastMousePosition.x), true, MouseSensitivity, delta)
if queryMouseMoveEvent("PitchUp"):
ship.rotateShip(0, deg2rad(lastMousePosition.y), true, MouseSensitivity, delta)
if queryMouseMoveEvent("PitchDown"):
ship.rotateShip(0, deg2rad(lastMousePosition.y), false, MouseSensitivity, delta)
lastMousePosition = Vector2(0,0)
And finally, inside the ship script:
func rotateShip(axis, radians, negate, sensitivity, delta):
var basis = self.transform.basis
var amount = sensitivity
if negate:
amount = -amount
var xaxis = basis.x
var yaxis = basis.y
var zaxis = basis.z
var deltaAmount = amount * delta
# select axis
var chosenAxis
match axis:
0:
chosenAxis = xaxis
1:
chosenAxis = yaxis
2:
chosenAxis = zaxis
rotate(chosenAxis, deltaAmount)
orthonormalize()
Doh! I love it when I post stupid code on a public forum!
The gist of the problem is that I totally failed to actually use the radians variable. As a result I was only using the mouse sensitivity.
There are a few other things that need tweaking, but that’s the main thing.
MisterAcoustic | 2020-05-19 02:50