How is gravity calculated?

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

For example:

const JUMP_SPEED = -325
const GRAVITY = 1000
if jump and is_on_floor:
    velocity.y = JUMP_SPEED

velocity.y = GRAVITY * delta

How exactly does Godot calculate this? I’m asking because I’m getting ready to add water physics to my game. It’d be a great help if I had a solid idea of how this works during runtime.

:bust_in_silhouette: Reply From: kidscancode

First of all, gravity is an acceleration, which means it should increase your y velocity every frame. The code you posted sets the y velocity to a constant value. Instead, you want to use something like:

velocity.y += GRAVITY * delta

This would make your y velocity increase each frame so that you fall faster and faster.

I’m not sure what you mean by “How exactly does Godot calculate this?” because in this case Godot is not calculating anything, you are doing it yourself in the script.

If you want to use Godot’s built-in physics for gravity, you would need to use the RigidBody node, which uses the physics engine to calculate movement. From the code snippet above, I’m assuming you’re using a KinematicBody2D, which requires you to handle all movement via your own code.