I am trying to implement a drawn line that predicts the players jumping trajectory but the line is not centerd on player

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)

when you initialize “points”, put the player position as the first point:

var points: PackedVector2Array = [player.global_position]
1 Like

the start point variable already starts at the player’s global position and the points variable need to be a vector.
the problem was in step time simply i needed to make the time shorter like 0.01:

var points = predict_jump_trajectory(initial_velocity, 90, 0.01)

i still had some miss calculation for some reason so I tweaked the initial_velocity:

initial_velocity.y = -jump_strength + 60
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.