Need help predicting trajectory

Godot Version 4.6.3 stable

Question

For more context, read my repo here

I’m trying to predict the trajectory of an object that you can throw in the game so you can see how far it will go before you actually throw it. But, I’m having trouble actually putting this into play. Here’s an example of the function I use to try and predict its path:

func predictTrajectory(delta: float):
	deleteDots()
	var rightStickX: float = Input.get_joy_axis(joystickDevice, JOY_AXIS_RIGHT_X)
	var rightStickY: float = Input.get_joy_axis(joystickDevice, JOY_AXIS_RIGHT_Y)
	var input: Vector2 = Vector2(rightStickX, rightStickY)
	var inputNormal: Vector2 = Vector2(rightStickX, rightStickY).normalized()

	var numDots: int = 6
	if abs(input.x) > deadzone or abs(input.y) > deadzone:
		for x in numDots:
			#createDot(rightStickX * spaceBetweenDots * (x + .5), input.y * spaceBetweenDots* (x + .5))
			#createDot(x*10, x*x)
			x= x * 10
			var myVelY = worldGravity * delta
			myVelY -= inputNormal * myMaxSpeed * x * delta
			var myPos = position - myVelY
			print("Me ", myPos)
			
			createDot(myPos.x, myPos.y)

Currently, the line produced is linear. I need something that looks like a parabola that actually reflects what the throw will look like. Can you help me with implementation?

What you want is a quadratic equation. If you google quadratic formula for projectile motion, you’ll find exactly what you need.

Compute the line’s points the same way you would compute the ball’s trajectory.

var trajectory: Array[Vector2] = [position]
var projected_position: Vector2 = position
var projected_velocity: Vector2 = velocity
for x in 60: 

  # Run the same physics the real object gets,
  # but apply them to the fake position instead
  projected_velocity += ..........
  projected_position += projected_velocity + .........

  trajectory.append(projected_position)
for x in 6:
  var sample: Vector2 = trajectory[x*10]
  createDot(sample.x, sample.y)

This should simulate 60 frames of your object’s trajectory and place dots at 10-frame intervals (every 160ms if your physics_process runs at 60Hz). I could suggest only running this when player input changes the trajectory but the CPU time used is cheaper than a discord ping sound.