All Questions

Community

B
CJ Boduma
posted to: Adding gravity

Why doesn't gravity continue to increase velocity. y?

It is probably obvious(but not to me).

if new_velocity.y += gravity * get_physics_process_delta_time() why doesn't gravity continue to increase velocity.y ??

PS Thanks for your excellent tutorials.



  • Nathan Lovato replied

    This is not obvious, and that's a good question.

    The reason gravity doesn't accumulate is because we store the value returned by move_and_slide:

    velocity = move_and_slide(...)
    

    When your kinematic body collides with the floor, this function resets the Y velocity to 0.

    And we apply the gravity every frame:

    1. To keep our code simple. Gravity always applies.
    2. So our character may collide with the floor every frame, allowing the KinematicBody2D class to detect that the character is on the floor. In turn, when we call KinematicBody2D.is_on_floor(), it can return true every frame that we are on the floor. This isn't the case if there is no gravity, or downwards velocity.
  • B
    CJ Boduma replied

    Thanks Nathan, Makes sense..... I never would have worked that out by myself.