Godot Version
Godot v4.2.2
Question
I am trying to implement a jump trajectory prediction for my 2D platformer game in Godot. I want it to draw the predicted path of the player’s jump while the jump button is being held down or charging the jump. However, the predicted trajectory appears off centred and does not start from the player’s position.
here’s the function that calculate the trajectory and draws it ():
PS: i’m new to program so i used the help of chatgpt to help me with the math
func predict_jump_trajectory(initial_velocity: Vector2, steps: int, step_time: float) -> PackedVector2Array:
var points = PackedVector2Array()
var position = marker.get_global_position()
var velocity = initial_velocity
print("Starting position: ", position) # Debug: Print starting position
for i in range(steps):
position += velocity * step_time
velocity.y += GRAVITY * step_time
# Snap position to the pixel grid
position = position.floor()
points.append(position)
print("Point ", i, ": ", position) # Debug: Print each trajectory point
return points
# Draw the predicted jump trajectory
func _draw():
if is_charging:
var jump_strength = lerp(0.0, JUMP_FORCE, charge_time / MAX_CHARGE_TIME)
var initial_velocity = Vector2()
initial_velocity.y = -jump_strength
initial_velocity.x = -JUMP_HORIZONTAL_FORCE if sprite.flip_h else JUMP_HORIZONTAL_FORCE
var points = predict_jump_trajectory(initial_velocity, 30, 0.1) # Adjust steps and step_time as needed
for i in range(points.size() - 1):
var start_point = points[i] - global_position
var end_point = points[i + 1] - global_position
# Snap points to the pixel grid
start_point = start_point.floor()
end_point = end_point.floor()
draw_line(start_point, end_point, Color(1, 0, 0), 2)