Last active
July 23, 2024 07:06
-
-
Save sjvnnings/5f02d2f2fc417f3804e967daa73cccfd to your computer and use it in GitHub Desktop.
An easy to work with jump in Godot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends KinematicBody2D | |
export var move_speed = 200.0 | |
var velocity := Vector2.ZERO | |
export var jump_height : float | |
export var jump_time_to_peak : float | |
export var jump_time_to_descent : float | |
onready var jump_velocity : float = ((2.0 * jump_height) / jump_time_to_peak) * -1.0 | |
onready var jump_gravity : float = ((-2.0 * jump_height) / (jump_time_to_peak * jump_time_to_peak)) * -1.0 | |
onready var fall_gravity : float = ((-2.0 * jump_height) / (jump_time_to_descent * jump_time_to_descent)) * -1.0 | |
func _physics_process(delta): | |
velocity.y += get_gravity() * delta | |
velocity.x = get_input_velocity() * move_speed | |
if Input.is_action_just_pressed("jump") and is_on_floor(): | |
jump() | |
velocity = move_and_slide(velocity, Vector2.UP) | |
func get_gravity() -> float: | |
return jump_gravity if velocity.y < 0.0 else fall_gravity | |
func jump(): | |
velocity.y = jump_velocity | |
func get_input_velocity() -> float: | |
var horizontal := 0.0 | |
if Input.is_action_pressed("left"): | |
horizontal -= 1.0 | |
if Input.is_action_pressed("right"): | |
horizontal += 1.0 | |
return horizontal |
Figure out I changed project settings for the ticks, if I unlock fps by turning off Vsync it still occur even when I have more FPS, I think there is a problem with the math or Godot 4
Physics framerate and rendering framerate are different in Godot. You can render the scene at a higher FPS with Vsync off, but Godot will still update the physics at a fixed interval depending on the project settings
Ik that Godot run at a fixed interval, anyways thanks for replying I will check out the RK method
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Intrivus Unfortunately, what you're experiencing is an unavoidable side effect of updating the character movement using these formulas of delta time. This kind of updating is called Euler Integration and it's prone to over or underestimation in physics approximation. Generally, Euler integration behaves more physically accurate at higher framerates, as you noticed. If high precision is important, you should probably just run your project at the higher framerate as it's the easiest solution. Otherwise, you can try using a different integration method, like Runge-Kutta. I will warn that different integration methods tend to be much more complex than Euler and may be difficult to implement if you don't have a good grasp on physics and calculus.