Free Three.js Article
Three.js Projectile Trajectory:
Why Game Shots Curve
By Shane Brumback · Updated
Three.js projectile trajectory explained: how gravity, air drag and wind bend a game shot, and why the aim arc must use the same physics step as the real shot. The cannon firing behind this page is the real demo.
The Short Answer
A three.js projectile trajectory is the path your code moves a shot along each frame. With gravity alone it is a perfect parabola. Air drag makes it fall shorter and steeper, and wind pushes it along or sideways. To make an aim arc that never lies, predict it with the same step function and the same fixed time step the real shot uses.
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.016per 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.
Introduction
I love a good artillery game. You set an angle, set the power, watch a dotted line curve across the screen, and fire. When the ball lands right on the ring, it feels great. When it lands five metres short of the line the game just showed you, it feels like the game cheated.
That second feeling is a bug, and it is one of the most common bugs in browser games. In this article I explain in plain words why game shots curve the way they do, what gravity, drag and wind each do to a shot, and the one rule that keeps an aim preview honest. The scene behind this page is the finished demo from my premium tutorial Three.js Projectile Trajectory Tutorial: Build an Artillery Aim Arc, firing by itself: the gold dots are the real predicted path and the blue dots show what the same shot would do in a vacuum.
What Is a Three.js Projectile Trajectory?
Three.js does not move anything by itself. It draws meshes where you put them. A projectile in Three.js is just a mesh plus two vectors that your code keeps: a position and a velocity. Every frame you change the velocity by the forces acting on the ball, then change the position by the velocity. Do that 60 times a second and the ball traces a curve. That curve is the trajectory.
This idea is called numerical integration, and the simplest useful version is semi-implicit Euler: update the velocity first, then move the position with the new velocity. It is stable enough for cannonballs, arrows, grenades and thrown balls, and it is only a few lines of code.
Gravity Only: The Perfect Parabola
With gravity as the only force, the sideways speed never changes and the vertical speed drops by 9.81 m/s every second. The result is a parabola, and it has a famous shortcut: on flat ground, the best launch angle for the longest shot is 45°, and the range is the speed squared times sin(2 × angle) divided by gravity.
That formula is lovely, and it is exactly why so many aim previews go wrong. It only describes a vacuum. The moment you add air, the formula is no longer the shot your game actually fires.
Air Drag: Why Real Shots Fall Short
Air pushes back against anything moving through it, and for a fast ball that push grows with the square of the speed. Twice as fast means four times the drag. So a hard shot loses a lot of speed early, then loses less as it slows down. The rising half of the arc is long and flat, the falling half is short and steep, and the shot lands well short of the parabola.
In my demo the drag constant is 0.016 per metre. At 41° and 30 m/s that is the difference between about 99 m in a vacuum and about 52 m with air. Drag also sets a terminal velocity, the speed where drag and gravity balance on the way down. For these numbers it is about 24.8 m/s, so a ball dropped from very high never falls faster than that.
Wind: Drag Through Moving Air
Here is the part that surprises people: wind is not a separate force you add on top. Wind changes the air the ball is moving through. So instead of using the ball's velocity for drag, you use its velocity minus the wind velocity, its speed through the air.
A tailwind means the ball is moving through the air more slowly than over the ground, so it feels less drag and flies further. A headwind does the opposite. A crosswind gives the ball a sideways speed through the air, and drag pushes it downwind. In the demo, the starting wind of 4 m/s down the range and 1.5 m/s across moves the 30 m/s shot from about 52 m to about 60 m, and about 2 m to the side. Turn it into a 4 m/s headwind and the same shot lands near 43 m.
Why the Aim Arc Must Use the Same Physics Step
This is the rule that fixes the cheating feeling: the preview and the real shot must run the same code. Not a similar formula. The same function, with the same fixed time step, starting from the same muzzle point with the same launch velocity.
In my tutorial that function is stepBody(pos, vel, dt, air). The aim arc calls it in a loop from the barrel until the ball reaches the ground, and drops a dot every few steps. When you fire, the cannonball calls the very same function on every fixed step. Because the steps are identical, the ball passes through every dot and lands inside the ring.
The second half of the rule is the fixed time step. If you move the real ball with each frame's delta time, a 144 Hz monitor and a busy phone at 40 frames per second take different-sized steps and get slightly different curves, and neither matches the preview. An accumulator fixes that: add each frame's time to a bucket and run as many 1/60 s steps as fit. Same steps, same path, on every screen.
How To Think About It
- Start with gravity. Each step, subtract gravity times the time step from the vertical speed, then move the ball by its velocity. With nothing else acting, that traces a perfect parabola.
- Add air drag. Work out the ball's velocity relative to the air, and push against it in proportion to that speed squared. Fast shots lose speed quickly and fall steeper than a parabola.
- Add wind. Subtract the wind from the ball's velocity before computing drag. Air moving with the shot carries it further; air moving against it or across it pushes it short or sideways.
- Predict with the same step. Draw the aim arc by running that exact step function forward from the muzzle in fixed time steps until the ball reaches the ground, then fire the real ball with the same steps.
Bonus: Let the Game Aim for You
Once prediction is cheap and honest, you get auto-aim almost for free. Pick a target, point the cannon at it, and search for the power that lands on it: if the predicted shot is short, try more power; if it is long, try less. Halving the range each time (a bisection search) finds the answer in about 20 predictions. Then measure how far the crosswind pushed the landing spot and turn the cannon into the wind by that angle. That is how the cannon behind this page hits the crates on its own, with no aiming formula at all.
In a Real Game
The same thinking runs Rocket Lander, my free Indie Forge Studio game where gravity and wind fight your thrusters on the way down to the pad. If you want rigid bodies that tumble and stack, read Three.js Rapier Physics: Landing a 3D Rocket with Real Rigid Bodies. For one point flying through the air, the little step function in this article is all you need.
Frequently Asked Questions
What is a three.js projectile trajectory?
It is the path a thrown or fired object follows in a Three.js scene. Three.js only draws the object; your code moves it each frame with gravity and, if you want realism, air drag and wind. The curve those forces make is the trajectory.
Do I need a physics engine for projectiles?
No. A cannonball or arrow is a single point with a velocity, so a few lines of JavaScript per frame are enough. Use a physics engine such as Rapier when the projectile must bounce off or knock over many rigid bodies.
Why does my aim preview not match where the shot lands?
Usually the preview uses the textbook parabola formula while the shot is moved with drag, wind or a variable frame time. Run the preview through the same step function with the same fixed time step and they match.
Why use a fixed time step instead of the frame time?
Frame times change from frame to frame and from device to device, and every different step size gives a slightly different curve. A fixed step, with an accumulator that runs as many steps as the frame needs, gives the same path on every screen.
Is the tutorial code free?
The article is free. The matching tutorial is a premium tutorial: the live demo is free when you sign in free or play as a guest, and members unlock the step-by-step code, the full file and the Launch button.
Build It Yourself
The premium tutorial builds this exact ballistics range step by step: the cannon, the dotted aim arc, drag and wind, auto-aim, glowing trails and crates that blow apart, with a free live demo.