Sailing Ship movement

Attention Topic was automatically imported from the old Question2Answer platform.

I am new to coding. I have a top-down ship object whose movement I am trying to make work. I have it moving at a constant speed the moment the game starts, and I have mapped it to move left when the left arrow on the keyboard is pressed, and right when the right arrow is pressed.
The ship has a large sail and a small sail. I would like to try to make it feel like the rotation of the sail is turning the ship, and the arrow keys are turning the sails.
Right now the ship and the sails turn at the same time. I would like to make it so that the sails rotate up to a certain degree relative to the ship, and then stop (since ship’s sails don’t rotate 360 degrees in real life.). Perhaps at 90 degrees relative to the ship.
I would like to make it so the more the sails rotate the faster the ship rotates.
When the sails get rotated back to zero the rotation of the ship slows until the sails hit zero degrees, at which point the ship is no longer rotating.
Here is the current code:

``````func control(delta):
var rot_dir = 0
if Input.is_action_pressed('turn_right'):
\$LargeSail.rotate(0.005)
\$SmallSail.rotate(0.005)
rot_dir += 1
if Input.is_action_pressed('turn_left'):
\$LargeSail.rotate(-0.005)
\$SmallSail.rotate(-0.005)
rot_dir -= 1
rotation += rotation_speed * rot_dir * delta
velocity = Vector2(speed, 0).rotated(rotation)
``````

Any ideas on where I can start on this?

This is what I have come up with so far. I decided a 45 degree stop would be more realistic than a 90 degree. Please let me know if you see a more efficient way to write this:

``````func control(delta):
var rot_dir = 0
if Input.is_action_pressed('turn_right'):
\$LargeSail.rotate(0.01)
\$SmallSail.rotate(0.01)
if Input.is_action_pressed('turn_left'):
\$LargeSail.rotate(-0.005)
\$SmallSail.rotate(-0.005)
if Input.is_action_just_pressed('shoot_right'):
shoot_right()
if Input.is_action_just_pressed('shoot_left'):
shoot_left()
if \$LargeSail.rotation_degrees > -45:
\$LargeSail.rotation_degrees = -45
\$SmallSail.rotation_degrees = -45
if \$LargeSail.rotation_degrees < -135:
\$LargeSail.rotation_degrees = -135
\$SmallSail.rotation_degrees = -135
if \$LargeSail.rotation_degrees > -85:
rot_dir += .05
if \$LargeSail.rotation_degrees > -81:
rot_dir += .10
if \$LargeSail.rotation_degrees > -72:
rot_dir += .15
if \$LargeSail.rotation_degrees > -63:
rot_dir += .20
if \$LargeSail.rotation_degrees > -54:
rot_dir += .25
if \$LargeSail.rotation_degrees < -95:
rot_dir += -.05
if \$LargeSail.rotation_degrees < -99:
rot_dir += -.10
if \$LargeSail.rotation_degrees < -108:
rot_dir += -.15
if \$LargeSail.rotation_degrees < -117:
rot_dir += -.20
if \$LargeSail.rotation_degrees < -126:
rot_dir += -.25
rotation += rotation_speed * rot_dir * delta
velocity = Vector2(speed, 0).rotated(rotation)
``````

wildBcat | 2020-02-17 01:31

Theirs probably some super simple trigonometry that’ll fix all of this but I don’t know.

Merlin1846 | 2020-02-17 15:30