Issue with Kinematicbody 2d move and collide function when accelerating a sprite.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By roxer1122

I am trying to understand the basic concepts of KinematicBody2D. Following is my code for movement of a sprite.

extends KinematicBody2D
const acceleration=50
const MAX_SPEED =200
var velocity = Vector2.ZERO

func _ready():
set_physics_process(true)

func _physics_process(delta):

velocity.x += (acceleration * delta)
velocity.x = clamp(velocity.x,-MAX_SPEED,MAX_SPEED)
move_and_collide(velocity)

I don’t understand that when I am using move and collide method for movement, the sprite quickly moves out of the screen. But when using move and slide method(without multiplying acceleration with delta),the movement is slow and looks perfect.
I have also noted that when the velocity is set constant, the sprite movement is same using both method.

:bust_in_silhouette: Reply From: Tim Martin

Hello,

I think you should be multiplying acceleration by delta, and adding it to velocity in both cases.

The difference is whether you then manually multiply the velocity vector by delta when passing it to the movement function.

For move_and_collide, you do move_and_collide(velocity * delta). For the move_and_slide helper function, you do move_and_slide(velocity) as the helper function multiples by delta internally.

:bust_in_silhouette: Reply From: A112Studio

When you use move_and_collide your speed is 200px per frame
With move_and_slidethe speed is 200px per second, this methods also returns a Vector2D which represents your remaining velocity.