I'm doing an Asteroids clone, and I got a nice "tank control" from the official Godot tutorials, but I'm stuck trying to add acceleration/deceleration to my ship.


var speed = 250
var rotate_speed = 1.5
var rotate_dir = 0
var velocity = Vector2()

func getinput():
	rotate_dir = 0
	velocity = Vector2()
	
	if Input.is_action_pressed("p1_right"):
		rotate_dir += 1
		
	if Input.is_action_pressed("p1_left"):
		rotate_dir -= 1
		
	if Input.is_action_pressed("p1_up"):
		velocity = Vector2(0, -speed).rotated(rotation)
		
	if Input.is_action_pressed("p1_down"):
		velocity = Vector2(0, speed).rotated(rotation) 

func _physics_process(delta: float) -> void:
	getinput()
	rotation += rotate_dir * rotate_speed * delta
	velocity = move_and_slide(velocity)

Would appreciate some help and ideas in how to implement it.

    pedrohfmg what i would do is make an additional variable to act as your multiplying factor and then have that variable increase when you're moving and drop when you are not. Then you can just plug in that factor into your physics process. Make it small to start out and play with the math until its where you want. You may want to set some constants to for a "max" accelaration level for your logic.
    So add something like:

    export var _maxAcceleration : float = 2.00 ## max rate, limit to acceleration
    export var _accelerationRate : float = 0.05 ## this value is added each physics frame
    var _movingAccelRate : float = 0.00 ## abstraction layer
    var _currentAccelRate : float = 0.00 ## this will hold your "current" value
    var _isMoving : bool = false ## use this to mark when you are moving so not always accelerating

    Then in your physics:

    func _physics_process(delta: float) -> void: ## we start "filling" our acceleration rate
        if _nowMoving:
            var _movingAccelRate = _currentAccelRate + _accelerationRate
    
    
        if _rotate_dir == 0 or !_nowMoving: ## or put whatever movement logic you want here and flip it on inputs
           _movingAccelRate = _currentAccelRate - _accelerationRate
    
        if _movingAccelRate > _maxAcceleration:
            _movingAccelRate = _maxAcceleration
     
    
        _currentAccelRate = _movingAccelRate
        getinput()
        rotation += rotate_dir * rotate_speed * _currentAccelRate * delta
        velocity = move_and_slide(velocity)

    The math will need tuning to get it right, but you get the idea.