I realize that a lot of stuff I post about here is really basic level stuff for people who have an easier time remembering things that they read, do, or really anything at all, but sometimes for me everything is an up-hill battle. The benefit of this shortcoming is that every step forward seems like an accomplishment, so I write it down so that don’t have to re-invent the wheel the next time I revisit.

I got my spaceship working, which is cool, and I got my infinite parallax background working as well. Everything is pretty “off the shelf” so no nominations for awards in the originality categories, please and thank you. The main issue right now, though, is that the ship moves like a flippin’ light cycle from Tron: It starts and stops on a dime.

Now, my design choice is that the player won’t have to deal with frictionless inertia because I personally hate that in games that I play. I don’t care how air-quote realistic air-quote you think it is, since we’re talking about space truckers with laser cannons and missiles and that’s about as realistic as elves with jetpacks, so I won’t be adding that kind of movement here; assume that technology has advanced to the point where spaceships can reverse thrust hard enough to stop themselves from getting all Fast and Furious in the depths of space. Still, I don’t want the ship to just accelerate from 0 to max as soon as the command is given because even I have limits.

Taking the base movement script I have, I amended it to add a bit of acceleration and, not by accident but also not entirely intentionally, deceleration.

extends KinematicBody2D

var speed = 0
export (int) var max_speed = 800
export (float) var rotation_speed = 1.5
export (float) var acceleration = 1200

var velocity = Vector2()
var rotation_dir = 0

func get_input(delta):
	rotation_dir = 0
	velocity = Vector2()
	if Input.is_action_pressed("move_e"):
		rotation_dir += 1
	if Input.is_action_pressed("move_w"):
		rotation_dir -= 1
	if Input.is_action_pressed("move_n"):
		speed += acceleration * delta
		if speed > max_speed:
			speed = max_speed
		velocity = Vector2(0, -speed).rotated(rotation)
	else:
		speed -= acceleration * delta
		if speed < 0:
			speed = 0
		velocity = Vector2(0, -speed).rotated(rotation)
	
func _physics_process(delta):
	get_input(delta)
	rotation += rotation_dir * rotation_speed * delta
	velocity = move_and_slide(velocity)

Now, the ship will slowly make its way towards its maximum speed, and when the player lets off the controls, will slowly decelerate back towards 0.

Ideally the idea would be that acceleration and deceleration would be decoupled, and the values that govern both would be factors of things like hull size, engine size, and cargo weight. While I don’t want to allow the ship to drift forever, I also don’t want the crew to slam against the bulkhead every time the ship moves or stops. Managing the acceleration and deceleration variables would be an interesting way to make different ships feel different, and would give the player something cool to juggle.

Scopique

Owner and author.

2 Comments

  • Tipa

    December 17, 2022 - 11:08 AM

    I dunnnnooooo having drag in space means you have to constantly thrust to keep moving. Uses a lot of fuel.

    Check out today’s XKCD 🙂

  • Nimgimli

    December 17, 2022 - 11:45 AM

    Hoping for elves with jetpacks in the finished game…

Leave a Reply

Your email address will not be published. Required fields are marked *