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
@Ramontique
Copy link

In Godot 4.0.1 I have set jump_height to 66 but I can't jump on top of tiles that are 64 pixels high. I have to set jump_height to 67 to make the jump. Is this behavior to be expected or is something wrong with my code?

@sjvnnings
Copy link
Author

I haven't tested this code with Godot 4.0 yet, but I can't see why it would be different, unless CharacterBody3D has substantially changed. Could you send a screenshot of your set up (i.e. the tiles you want to jump, the character, and the collision shapes you're using)?

@Ramontique
Copy link

Just made a minimalist project using only your code converted to Godot 4.0.2 and it seems to work without any issue.

@Ramontique
Copy link

Ramontique commented Apr 10, 2023

Hm I did notice something weird after all: jump_height 64, 63, 62 all jump over 64 pixels. Might be something wrong with Godot 4 physics after all. /edit: same behavior in 3.5.

@sjvnnings
Copy link
Author

Hmmm, that's strange, I haven't noticed any problems in Godot 3.x yet, and this code should be mathematically pure as it's derived from real-world physics equations. Could you send a screenshot of your project or a minimal reproduction project?

@Ramontique
Copy link

https://www.dropbox.com/s/x2fk6yg589q10gw/Jump%20height%20test%203.5.zip?dl=0

Here's a link to a minimal reproduction project in Godot 3.5. As you can see I have jump_height set to 62 and the player can jump over 64 height of blocks.

@default1200
Copy link

I tried this but it doesn't work for some reason. I get Division by zero in operator '/' error
also an error at function: _ready reffering to the onready var

@Frodo-tech
Copy link

I tried this but it doesn't work for some reason. I get Division by zero in operator '/' error also an error at function: _ready referring to the onready var

Are you sure you initialized all the related variables? if either time_to_descend or time_to_peak are 0 when the node is ready (whence why the error occurs at the _ready() function) you'll get a division by zero error.

@Jazztache
Copy link

Would there be a way to get this working in Godot 4.X?

@KyleRConway
Copy link

KyleRConway commented Aug 16, 2023

Would there be a way to get this working in Godot 4.X?

This works on my end. It's the base movement template from Godot 4.0 with these concepts built in.


extends CharacterBody2D

@onready var screen_size = get_viewport_rect().size

const SPEED = 600.0
#const JUMP_VELOCITY = -600.0

@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

# Get the gravity from the project settings to be synced with RigidBody nodes.
#var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
#var gravity = 980

func _physics_process(delta):
	print(jump_gravity,fall_gravity)
	# Add the gravity.
	if not is_on_floor():
		velocity.y += get_gravity() * delta

	# Handle Jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = jump_velocity
		print("JUMP")

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var direction = Input.get_axis("ui_left", "ui_right")
	if direction:
		velocity.x = direction * SPEED
		slow_friction(delta)
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
	print(direction)
	
	# Screenwrap	
	#position.x = wrapf(position.x, 0, screen_size.x)
	#position.y = wrapf(position.y, 0, screen_size.y)

	move_and_slide()

func slow_friction(d):
	velocity.x -= .02 * d
	print(velocity)
	print("SLOW")

func get_gravity() -> float:
	return jump_gravity if velocity.y < 0.0 else fall_gravity

@kwhitejr
Copy link

kwhitejr commented Oct 20, 2023

@Jazztache

Would there be a way to get this working in Godot 4.X?

Here is a minimalist implementation for Godot 4.x.x. I also included air jumps because reasons.

extends CharacterBody2D

@export var move_speed : float = 200.0
@export var air_jumps_total : int = 1
var air_jumps_current : int = air_jumps_total

@export var jump_height : float = 30
@export var jump_time_to_peak : float = 0.5
@export var jump_time_to_descent : float = 0.25

@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_horizontal_velocity() * move_speed
	
	if Input.is_action_just_pressed("jump"):
		if is_on_floor():
			jump()
		if air_jumps_current > 0 and not is_on_floor():
			air_jump()

	var direction = Input.get_axis("move_left", "move_right")
	if direction:
		velocity.x = direction * move_speed
	else:
		velocity.x = move_toward(velocity.x, 0, move_speed)

	move_and_slide()

func get_gravity() -> float:
	return jump_gravity if velocity.y < 0.0 else fall_gravity

func jump():
	air_jumps_current = air_jumps_total
	velocity.y = jump_velocity
	
func air_jump():
	air_jumps_current -= 1
	velocity.y = jump_velocity

func get_horizontal_velocity() -> float:
	var horizontal := 0.0
	
	if Input.is_action_pressed("move_left"):
		horizontal -= 1.0
	if Input.is_action_pressed("move_right"):
		horizontal += 1.0
	
	return horizontal

@Intrivus
Copy link

I found out that physics frame rates affects the code, which makes the script now really inconsistent. When I set the jump to 256 (twice the height of the character's body) it jump roughly 9 pixels higher above the platform that is 3 times my body, looking somewhat like this:
image
(this is with 60fps)

And if I set the game settings to 120 fps, it jumps normally (probably would affect higher jumps)

@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