Skip to content

Instantly share code, notes, and snippets.

@sjvnnings
Last active July 23, 2024 07:06
Show Gist options
  • Save sjvnnings/5f02d2f2fc417f3804e967daa73cccfd to your computer and use it in GitHub Desktop.
Save sjvnnings/5f02d2f2fc417f3804e967daa73cccfd to your computer and use it in GitHub Desktop.
An easy to work with jump in Godot
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
@cabezayunke
Copy link

This works for me with variable height based on how long the button is pressed:

extends CharacterBody2D


@export_group("Jump")
@export var jump_height_px = 30.0
@export var jump_time_up = 0.4
@export var jump_time_down = 0.1

@onready var jump_velocity = -(2.0 * jump_height_px) / jump_time_up
@onready var jump_gravity = (2.0 * jump_height_px) / (jump_time_up * jump_time_up)
@onready var fall_gravity = (2.0 * jump_height_px) / (jump_time_down * jump_time_down)

var _gravity: float:
	get:
		return jump_gravity if velocity.y < 0.0 else fall_gravity 

@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
@onready var jump_timer: Timer = $JumpTimer


func _handle_variable_height_jump() -> void:
	# check initial input
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		jump_timer.start()
		velocity.y = jump_velocity
		sprite.play("jump")
	
	# keep checking input while timer is running
	if not jump_timer.is_stopped() and Input.is_action_pressed("ui_accept"):
		velocity.y = jump_velocity


func _physics_process(delta: float) -> void:
	# Add the gravity.
	if not is_on_floor():
		velocity.y += _gravity * delta
	
	velocity.x = get_horizontal_velocity() * move_speed
	
	# Handle jump.
	_handle_variable_height_jump(delta)
	
	move_and_slide()

@40-4
Copy link

40-4 commented Jul 19, 2024

Hi, trying to get this code to work in 4.2.2, but for some reason math isn't working correctly, can't figure out why. Changing "time_to_peak" and "time_to_fall" also seems to affect the jump height, which wasn't supposed to be the case.

Here's my code (with coyote time and jump buffering already implemented, you can ignore those)

extends CharacterBody2D

var speed : float = 200

var jump_height : float = 100
var time_to_peak : float = 0.5
var time_to_fall : float = 0.3

var _coyote_time : float = 0.0
var _jump_buffer : float = 0.0
var _jumped : bool = false

var _jump_velocity : float = ((2.0 * jump_height) / time_to_peak)


func _get_gravity() -> float:
	if velocity.y >= 0 and Input.is_action_pressed("MOVE_JUMP"):
		return ((2.0 * jump_height) / (time_to_peak ** 2))
	else:
		pass
		_jumped = false
		return ((2.0 * jump_height) / (time_to_fall ** 2))

func _jump() -> void:
	velocity.y = -_jump_velocity
	_jumped = true

func _physics_process(delta) -> void:
	if is_on_floor():
		_coyote_time = 0.1
		if Input.is_action_just_pressed("MOVE_JUMP") or _jump_buffer > 0.0:
			_jump()
	else:
		velocity.y += _get_gravity() * delta
		if _coyote_time > 0 and Input.is_action_just_pressed("MOVE_JUMP"):
			_jump()
		elif Input.is_action_just_pressed("MOVE_JUMP"):
			_jump_buffer = 0.2
			
		_coyote_time -= delta
		_jump_buffer -= delta
		
	
	velocity.x = clamp(Input.get_axis("MOVE_LEFT", "MOVE_RIGHT")*100, -1.0, 1.0) * speed
	
	move_and_slide()
	print(velocity)

Thanks in advance

@40-4
Copy link

40-4 commented Jul 21, 2024

Hi, trying to get this code to work in 4.2.2, but for some reason math isn't working correctly, can't figure out why. Changing "time_to_peak" and "time_to_fall" also seems to affect the jump height, which wasn't supposed to be the case.

Here's my code (with coyote time and jump buffering already implemented, you can ignore those)

extends CharacterBody2D

var speed : float = 200

var jump_height : float = 100
var time_to_peak : float = 0.5
var time_to_fall : float = 0.3

var _coyote_time : float = 0.0
var _jump_buffer : float = 0.0
var _jumped : bool = false

var _jump_velocity : float = ((2.0 * jump_height) / time_to_peak)


func _get_gravity() -> float:
	if velocity.y >= 0 and Input.is_action_pressed("MOVE_JUMP"):
		return ((2.0 * jump_height) / (time_to_peak ** 2))
	else:
		pass
		_jumped = false
		return ((2.0 * jump_height) / (time_to_fall ** 2))

func _jump() -> void:
	velocity.y = -_jump_velocity
	_jumped = true

func _physics_process(delta) -> void:
	if is_on_floor():
		_coyote_time = 0.1
		if Input.is_action_just_pressed("MOVE_JUMP") or _jump_buffer > 0.0:
			_jump()
	else:
		velocity.y += _get_gravity() * delta
		if _coyote_time > 0 and Input.is_action_just_pressed("MOVE_JUMP"):
			_jump()
		elif Input.is_action_just_pressed("MOVE_JUMP"):
			_jump_buffer = 0.2
			
		_coyote_time -= delta
		_jump_buffer -= delta
		
	
	velocity.x = clamp(Input.get_axis("MOVE_LEFT", "MOVE_RIGHT")*100, -1.0, 1.0) * speed
	
	move_and_slide()
	print(velocity)

Thanks in advance

welp, turns out my whole problem was >= instead of <=...

@sjvnnings
Copy link
Author

@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.

@Intrivus
Copy link

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

@sjvnnings
Copy link
Author

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

@Intrivus
Copy link

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