← Pixel Dreamer Studio

A trajectory line that matches where the ball lands

Step gravity and the project's default damping, one physics tick at a time.

Build the line by stepping through the same physics the ball uses: add gravity, apply linear damping, move, once per physics tick. A new Godot project damps every RigidBody3D by 0.1 (default_linear_damp), and the parabola formula leaves that out. In our Godot 4.7 test the formula put the ball down 12.37 m away, the real ball came down at 11.15 m, and the script below said 11.16 m.

A ball thrown over a dark green field, shown as a row of orange dots along a white line. A thin red line rises a little higher and lands further away
Godot 4.7. Orange: the real RigidBody3D, every frame for its first second. White: the script's line. Red: the parabola formula.

The code

Add this node to your level and call show_path(start, velocity) every frame while the player aims, with the same velocity you will give the ball. landing holds the first point the path hits, for a target marker. If the ball is already at the start point, pass its RID in exclude so the line does not stop on the ball itself.

extends Node3D
## Draws the path a thrown or kicked ball will take, and where it lands, before you throw it.
## It steps through the same physics as a RigidBody3D, gravity and linear damping,
## one physics tick at a time, so the line and the real ball agree.
## Call show_path(start, velocity) while aiming. Pass the ball's RID in "exclude" so the line does not hit the ball.

@export var max_time := 3.0  ## seconds of flight to look ahead
@export var color := Color(1.0, 1.0, 1.0, 0.85)

var landing := Vector3.INF  ## where the path first meets something, INF if it does not
var _mesh := ImmediateMesh.new()


func _ready() -> void:
	var line := MeshInstance3D.new()
	line.mesh = _mesh
	line.top_level = true  # the points are in world space
	line.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
	var m := StandardMaterial3D.new()
	m.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED
	m.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA
	m.albedo_color = color
	line.material_override = m
	add_child(line)


func predict(start: Vector3, velocity: Vector3, ball_damp := 0.0, exclude: Array[RID] = []) -> PackedVector3Array:
	var dt := 1.0 / Engine.physics_ticks_per_second
	var gravity: Vector3 = ProjectSettings.get_setting("physics/3d/default_gravity") * ProjectSettings.get_setting("physics/3d/default_gravity_vector")
	# the project's default damping (0.1 in a new project) is added to the ball's own linear_damp
	var damp: float = ProjectSettings.get_setting("physics/3d/default_linear_damp") + ball_damp
	var space := get_world_3d().direct_space_state
	var pts := PackedVector3Array([start])
	var p := start
	var v := velocity
	landing = Vector3.INF
	for i in int(max_time / dt):
		v += gravity * dt
		v *= maxf(0.0, 1.0 - damp * dt)
		var q := PhysicsRayQueryParameters3D.create(p, p + v * dt)
		q.exclude = exclude
		var hit := space.intersect_ray(q)
		if hit:
			landing = hit.position
			pts.append(landing)
			return pts
		p += v * dt
		pts.append(p)
	return pts


func show_path(start: Vector3, velocity: Vector3, ball_damp := 0.0, exclude: Array[RID] = []) -> void:
	var pts := predict(start, velocity, ball_damp, exclude)
	_mesh.clear_surfaces()
	if pts.size() < 2:
		return
	_mesh.surface_begin(Mesh.PRIMITIVE_LINE_STRIP)
	for pt in pts:
		_mesh.surface_add_vertex(pt)
	_mesh.surface_end()

What we measured

A ball of radius 0.11 m thrown from 1 m up at 8 m/s forward and 7 m/s up, over a flat floor, at 60 physics ticks per second. We compared how far it had gone when its centre came down to 0.11 m above the floor, where the ball touches.

Two runs gave the same numbers.

Why the formula misses

Most of the gap is damping, not the stepping: steps without damping still landed 1.1 m too far. physics/3d/default_linear_damp is 0.1 in a new project and slows every moving body a little each tick, and a body's own linear_damp is added to it. The script reads both from the same settings, so it stays right if you change them.

What to change

Ready-made ball effects: Sports & Ball Game VFX for Godot 4 has 32 presets in Stylized and Retro PSX looks, including a Trajectory Preview, ball trails, charge shots, kick impacts and a goal net ripple.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Sports & Ball Game VFX for Godot 4 has 32 ready-made sports effects in Stylized and Retro PSX looks, including a Trajectory Preview, ball trails, charge shots, kick impacts and a goal net ripple.

More how-tos: Projectiles and aiming