W/S angle · A/D aim · Q/E power · Space fire · T auto-aim · G air · drag to orbit · Esc exits

Premium · Level 3 Three.js Tutorial

Three.js Projectile Trajectory Tutorial:
Build an Artillery Aim Arc

By Shane Brumback · Updated · Three.js tutorial

Premium three.js projectile trajectory tutorial: a cannon, a dotted aim arc that predicts the landing spot, drag, wind, auto-aim and crates that explode. Live demo.

Three.js Physics Ballistics Game Development
See it live

LIVE DEMO

The ballistics range behind this page is the finished file running live, with the cannon aiming and firing by itself. Launch the full-screen demo to take over: set the angle and power, watch the dotted aim arc bend with drag and wind, fire, and knock the crates over.

Free: sign in free or play as a guest. Keyboard or the on-screen buttons aim and fire; drag or one finger orbits; Esc exits.

What Is a Three.js Projectile Trajectory? (Quick Definition)

A three.js projectile trajectory is the path your code moves a shot along, one small time step at a time: gravity pulls it down, air drag slows it in proportion to its speed squared, and wind changes the air it moves through. An aim arc is that same path predicted ahead of time, and it is only accurate when it runs the same step function with the same fixed time step as the real shot.

Introduction

In this tutorial you build a small artillery range in one HTML file. A cannon sits on a mound at sunset. As you change the angle and power, a gold dotted arc shows exactly where the shot will land, and a blue arc shows where the same shot would land with no air at all. Press fire and a glowing cannonball follows the gold dots into a stack of crates, which blow apart.

The point of the lesson is the promise the arc makes: the preview never lies. Both the arc and the ball run the same physics step, with drag that grows with speed squared and wind that changes the air the ball flies through. Once prediction is honest, the cannon can even aim itself, which is how it hits the targets behind this page.

Want the ideas first? The free article Three.js Projectile Trajectory: Why Game Shots Curve explains gravity, drag, wind and fixed time steps in plain words.

What You'll Learn

  • Move a projectile with semi-implicit Euler integration: gravity, quadratic air drag and wind in one small function
  • Predict a dotted aim arc and a landing ring with the exact same step, drawn with one InstancedMesh
  • Run the real shot on a fixed time step with an accumulator so it matches the preview at any frame rate
  • Auto-aim by searching for the right power (bisection) and correcting for crosswind
  • Add trails, explosions, a flash of light and crates that fly, with keyboard and touch controls

Prerequisites

Facts

  • Gravity in the demo is 9.81 m/s² straight down, and every shot, real or predicted, moves in fixed steps of 1/60 of a second.
  • Air drag is quadratic: DRAG_K = 0.016 per metre, which gives the cannonball a terminal fall speed of about 24.8 m/s (the square root of 9.81 / 0.016).
  • At 41° and 30 m/s, a shot in a vacuum would land about 99 m away. With drag and no wind it lands about 52 m away, and in the demo's starting wind (4 m/s down the range, 1.5 m/s across) about 60 m away and 2 m to the side.
  • A 4 m/s headwind with the same shot cuts the range to about 43 m. Drag is worked out from the ball's speed through the air (its velocity minus the wind), not its speed over the ground.
  • The dotted aim arc and the real cannonball call the same stepBody() function with the same fixed step, so the landing ring is exactly where the ball comes down.
  • Controls: W/S or up/down angle, A/D or left/right aim, Q/E power, Space fire, T auto-aim, G air on/off, N new wind, V vacuum arc, R reset crates, plus on-screen buttons; drag or one finger orbits, scroll or pinch zooms, Esc exits.
  • One HTML file, three.js r160 from unpkg, OrbitControls, no build step and no physics engine.

How the Three.js Projectile Demo Works

  1. Imports, settings and renderer. we load three.js and OrbitControls and put every physics number in one place: gravity, the drag constant, the fixed time step, the ball size and the wind.
  2. Sunset sky, lights and the firing range. a big inside-out sphere with a simple gradient shader paints the sunset, a warm sun casts shadows, and a green ground plane gets a pale stripe every 10 metres so you can read distances by eye..
  3. Cannon, wind sock, crate stacks and the bullseye. the cannon is a group on a dirt mound with a barrel inside a pivot, so turning the group aims left and right and tilting the pivot sets the angle.
  4. The physics step: gravity, drag and wind. this is the heart of the lesson.
  5. Aim arc: predict the shot with the SAME step. predictPath() starts a pretend ball at the muzzle and calls the exact same stepBody() with the exact same FIXED_DT until it reaches the ground, saving every point.
  6. Auto-aim: solve for the power that lands on a target. because prediction is honest, aiming is just searching.
  7. Firing: cannonballs and glowing trails. fire() makes a glowing ball at the muzzle with the same launch velocity the arc used, plus a 40-point line for its trail, a puff of muzzle smoke and a little barrel recoil.
  8. Impacts: explosions, flying crates and the score. hitTest() checks the ball against every crate box and the bullseye disc.
  9. HUD, keys and touch buttons. the code builds its own panel and buttons, so the full file needs no extra HTML.
  10. Camera: auto-orbit behind the page, drag or finger orbit in the demo. behind the page text the camera slowly circles the range and looks a little to the left of it, so the action sits on the right behind the title, while an autopilot auto-aims and fires every 2.6 seconds.
  11. Main loop, resize and Live Demo hooks. every frame we read the held keys, run the autopilot, rebuild the arcs if needed, step the shots, update the explosions, turn the wind sock, move the camera, refresh the panel and draw.

Code Breakdown

The detailed code breakdown for all 11 steps and the full copy-paste file are below. Members unlock them. Every step is an exact excerpt of the full file, and the full file is exactly what runs in the Live Demo and the Launch button.

How To Use It

  • Turn air off (G) and the gold and blue arcs become one perfect parabola. Turn it back on and watch how much shorter and steeper the real shot is.
  • Press N a few times for new winds. A tailwind stretches the arc, a headwind shrinks it and a crosswind bends the landing ring sideways. Then press T and let the cannon find the power for you.
  • Raise DRAG_K to 0.04 for a heavy, draggy ball, or lower it to 0.005 for a nearly vacuum one. The arc and the shot still agree.
  • To drop the trick into your own game, copy stepBody(), predictPath() and the accumulator loop in updateShots().

Frequently Asked Questions

How do I make a three.js projectile trajectory with an accurate aim arc?

Move the projectile with one step function that applies gravity, drag and wind, using a fixed time step. Draw the aim arc by running that same function forward from the muzzle until the ball reaches the ground, and move the real shot with the same function and the same fixed step through an accumulator.

Is the full source free?

No. This is a premium Level 3 tutorial. The live demo is free: sign in free or play as a guest to launch it. Members unlock the step-by-step code, the full file and the Launch button.

Does this need a physics engine like Rapier or cannon-es?

No. A cannonball is one point with a velocity, so the whole flight model is a few lines of plain JavaScript. The crates use a tiny bit of hand-written gravity and bounce. Use a physics engine when you need real rigid-body stacking and collisions.

How do I make the drag stronger or weaker?

Change DRAG_K. Bigger numbers mean more drag, shorter shots and a lower terminal velocity (the square root of 9.81 divided by DRAG_K). Set it to 0, or press G in the demo, to see the pure gravity parabola.

Does it work on phones?

Yes. The on-screen buttons set angle, power and direction, fire, auto-aim, toggle air and pick a new wind. Drag with one finger to orbit and pinch to zoom.

Related Tutorials and Articles

READ THE FREE ARTICLE

Want the why before the how? Read Three.js Projectile Trajectory: Why Game Shots Curve, then play a game built on the same ideas.