0 votes

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()
in Engine by (46 points)

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.

1 Answer

0 votes
Best answer

As I mentioned in my comment above, I failed to use the value that I passed in for how much to rotate. Here is something that works, despite my not having cleaned up the code yet:

func rotateShip(axis, radians, negate, sensitivity, delta):
        var basis = self.transform.basis

        var amount = radians

        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

        if negate:
            deltaAmount = -deltaAmount

        rotate(chosenAxis, deltaAmount * sensitivity)

        orthonormalize()
by (46 points)
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 [email protected] with your username.