Godot Version
4.3
Question
so as apart of the 10 game challenge im making jetpack joyride and I want to make the gravity more accurate to whats in the actual game cause my guy just starts falling immediately I tried making it lower the y vel a little every second but that just breaks things what can I do?
here is my code btw
extends CharacterBody2D
const SPEED = 300
const JUMP_VELOCITY = -400.0
const gravity = 500
var direction = 1
#469 from players position
func _physics_process(delta: float) -> void:
Global.player_position = global_position
if !is_on_floor():
$"Character Sprite".play("Flying")
else:
$"Character Sprite".play("idle")
# Handle jump.
if Input.is_action_just_pressed("ui_accept"):
velocity.y = JUMP_VELOCITY
if Input.is_action_just_released("ui_accept"):
pass
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
Gravity on Earth causes downward acceleration of 9.81 meters every second. So the distance fallen is:
- 9.81 meters in the first second.
- 19.62 meters in the second second.
- 29.43 meters in the third second.
- etc…
How you calculate this in your game can be done in several ways. One way is to maintain a variable that indicates your current fall rate, and initialize this to 0.
The actual distance fallen is fall_rate * delta
.
Increment fall_rate
by 9.81 * delta
on every frame until terminal velocity is reached.
If someone finds an error in my math (which is likely), feel free to correct me.
not what I ment but this will prolly work
Well since you asked for corrections…
Acceleration modifies the velocity, not the distance fallen. So it the acceleration would be 9.81 meters per second per second. And after one second, the velocity would be 9.81 meters per second if starting from 0. The distance fallen is not linear so you’ll need to use one of those grade school physics formulas to find the distance.
The current fall rate is already stored in the built in variable velocity
. You’re right that acceleration should be multiplied by delta. However, given that this is a 2D game, the units are in pixels and 9.8 pixels per second per second is too small to be believable as gravity. A number around like 800 or so might be better.
My bad. It’s been a while since I last saw Jetpack Joyride, and assumed (for some reason) it was 3D. My calculations don’t apply to pixel-based games.
1 Like