# Should acceleration and friction be multiplied by delta?

Attention Topic was automatically imported from the old Question2Answer platform.

## In the following script, should ACCEL (acceleration) and FRIC (friction)be multiplied by delta?

extends KinematicBody2D

# Members
##movement
const SPEED = 200
const ACCEL = 30
const FRIC = 15
var velocity : Vector2
var direction : Vector2
#----- members

func get_input_dir() -> void:
direction.x = float(Input.is_action_pressed("move_right")) - float(Input.is_action_pressed("move_left"))
direction.y = float(Input.is_action_pressed("move_down")) - float(Input.is_action_pressed("move_up"))
direction = direction.normalized()

func _physics_process(delta) -> void:
get_input_dir()
_move()

func _move() -> void:
if direction == Vector2.ZERO:
velocity = velocity.move_toward(Vector2.ZERO, FRIC)
else:
velocity = velocity.move_toward(direction * SPEED, ACCEL)
velocity = move_and_slide(velocity)

I don’t understand why you’re using move_toward() in the velocity variable. Usually, the directions are added or subtracted from velocity (or some Vector2 that involves movement) and that is used in the move_and_slide() function. Btw, delta should not be factored into the calculation for move_and_side(), as noted in the documentation on the function (at least in KinematicBody2D):

move_and_slide() automatically calculates frame-based movement using delta. Do not multiply your velocity vector by delta before passing it to move_and_slide().

Thanks, I was following a tutorial that mentioned using the move_toward() method to accelerate and decelerate. I did some testing by forcing my game down to 10 fps and the acceleration and deceleration were significantly different, so I guess that answers my question.

I ended up changing the function I used, because that one, when applying delta to friction and acceleration, made me have huge acceleration and friction numbers.

These are the key parts of my final code:

const speed : int = 500
const acceleration : float = 3.0
const friction : float = 7.5

## With in the _physics_process function

#For moving
velocity = lerp(velocity, direction * speed, acceleration * delta)

# For stopping
velocity = lerp(velocity, Vector2.ZERO, friction * delta)

# And then...
velocity = move_and_slide(velocity)

Testing out my game in different frame rates showed significant changes in acceleration and friction if delta was not applied. So you do have to apply delta to acceleration and friction if your setup is like mine.

hidemat | 2020-10-17 02:39