Build the Offline Flight Model
Mediumdifficulty
You'll build a simulateShot(...) function that says where the ball crosses the rim height on the way
down, how fast it's moving, how long it took, and whether it fits through the opening. The math uses
vectors, differential equations, and numerical integration. You can use a tested library for those, as
long as you're careful about units and exactly which moment you're measuring.
A shot aimed at the front lip can look correct as a line on a screen and still clip the goal. The ball has a radius. Its center must pass far enough inside both edges, and it must be descending.
First, draw the available opening
The paper’s opening is 0.465 m deep and the ball’s diameter is 0.127 m. The usable center interval is therefore:
For a front lip at (70 inches), the center must cross between 1.8415 m and 2.1795 m. The launch height is 0.400 m and the rim plane is 0.984 m above the floor. These are the case-study geometry, from paper §§3 and 5.3.
Worked example: a 22.5 cm shift
At 5.25 m/s and 58°, a vacuum calculation places the descending crossing at 2.084 m. With the paper’s effective drag coefficient, , it crosses at 1.859 m: approximately 22.5 cm earlier, a 10.8% reduction.
Both predictions go in. But with drag, the ball's center clears the front lip by only about 1.8 cm. That's why the next lesson checks nearby settings too instead of trusting one good-looking line. Source: paper §4, Figure 3.
Before touching the controls, predict what increasing drag will do to the crossing. Set the drag coefficient to zero, restore it to 0.47, then lower the exit speed. Watch for the difference between missing the opening and never reaching a descending rim event.
The dot uses the 20 ms solver with Hermite event reconstruction. The main path uses 5 ms samples for drawing. Neither the nearest sample nor the later ground impact defines entry.
In the Hermite event locator, press Test midpoint one step at a time. Each test asks whether the reconstructed height is still above the rim. On a descending segment, “above” keeps the right half and “below” keeps the left. After 16 decisions, the original 20 ms interval is only about 0.305 µs wide; the same fractional time is then used to reconstruct horizontal position and velocity.
Two forces change the velocity
Gravity points down. Drag points opposite the ball’s velocity relative to the air. For still air, the two-dimensional equations are:
The model uses a 0.0748 kg ball, air density 1.204 kg/m³, and a cross-section of . The drag coefficient was chosen for the model, not measured. The model leaves out spin, sideways forces, and anything after the ball touches the goal.
For horizontal wind , use in the drag term. Keep in : the position is measured relative to the ground, while drag depends on air-relative motion. Mixing those two frames changes the trajectory even when the force equation looks familiar.
How much do those choices matter? At the example shot, changing from 0.40 to 0.55 moves the crossing by 6.3 cm, and changing the ball's diameter from 0.124 to 0.127 m moves it by 0.9 cm (paper §4).
Find the event between samples
RK4 advances the state in 20 ms steps. Scoring happens at the instant the ball descends through the rim plane, which usually lies between two samples. Cubic Hermite interpolation uses both endpoint positions and velocities to reconstruct that short segment. Sixteen binary-search halvings locate the crossing inside the known bracket.
The same crossing supplies the horizontal coordinate, flight time, and entry velocity. The time when the ball later hits the floor describes a different event and cannot replace it.
Narrowing the search that far only makes the crossing time precise within the reconstructed segment. It doesn't fix errors in the simulation itself or in the drag coefficient.
Explore the integration method
Euler takes one starting slope for a whole step. RK4 evaluates four slopes, including estimates inside the step, and combines them with weights divided by six. With smooth dynamics its global error decreases as the fourth power of the step size.
Compare rim-crossing error against a 1 ms RK4 reference. This checks step-size sensitivity within one model; the paper also used an independent solver.
For an endpoint position with endpoint velocity , the Hermite segment is:
Here is the step duration and runs from zero to one inside the step. Apply this formula to height to find the root , then use that same for horizontal position. Source: paper §5.2, Figure 4.
Implement the solver in this order
In the pinned physics engine, follow the calculation in this order:
- ShotParams describes speed, angle, release geometry, target geometry, wind, and drag enablement.
- _accel_np evaluates the same gravity/drag law over arrays of active trajectories.
- _integrate advances them with RK4 and detects a descending height bracket.
- _hermite_root bisects that bracket; _hermite_derivative gives the entry velocity.
- _made_from compares the event coordinate with the radius-shrunk opening.
- simulate exposes event time separately from the end of the displayed path.
The source uses 0.98425 m rim height and 0.46482 m depth; the paper and teaching examples round these to 0.984 m and 0.465 m. State the geometry when comparing last-decimal results. The source's spin parameters are compatibility fields; they do not add Magnus forces to the equations.
Verify the mathematics before calibrating the hardware
The paper (§5.6) compared the simulator with the exact no-drag parabola 993 times, and with a separate high-accuracy solver 158 times with drag. The biggest differences were under a micrometer. That shows the code solves the equations correctly. It doesn't mean the robot aims to a micrometer.
The verification harness also checks caching, exhaustive selection, input validation, and concurrent requests. Its DOP853 comparison is optional and requires SciPy; a skipped comparison is not a passed independent check.
Acceptance checks before you trust the solver
| Check | Expected result |
|---|---|
| Disable drag and compare with an analytic parabola | Crossing position converges to the analytic answer. |
| Repeat with a smaller integration step | Rim position and time stabilize within a declared tolerance. |
| Use a command that never descends through rim height | The solver returns no event, rather than a fabricated range. |
| Cross outside the radius-shrunk opening | The event exists, but the geometric result is a miss. |
| Continue the display path to the floor | Rim-event time remains distinct from ground-impact time. |
Java: turn the crossing into an entry test
Once the solver has found a descending event, the geometric test is small:
boolean entersOpening(double crossingX, boolean descending,
double frontLip, double openingDepth,
double ballRadius) {
if (!descending || !Double.isFinite(crossingX)) return false;
double firstCenter = frontLip + ballRadius;
double lastCenter = frontLip + openingDepth - ballRadius;
return crossingX >= firstCenter && crossingX <= lastCenter;
}
All distances in this method use metres. An unavailable event must not be substituted with a ground range.
For the 70-inch example, does a descending center crossing at 1.830 m enter? How about 1.900 m? The valid interval is [1.8415, 2.1795] m. The 1.830 m crossing misses the front lip; 1.900 m lies inside the interval and passes the geometric test. Neither result predicts whether the ball will bounce out afterward.Show solution