TheAlgorithms/Java · error · IllegalArgumentException

Velocity, height, and gravity must be non-negative, and grav

Error message

Velocity, height, and gravity must be non-negative, and gravity must be positive.

What it means

Thrown by ProjectileMotion.calculateTrajectory when initialVelocity < 0, initialHeight < 0, or gravity <= 0. The trajectory solver uses the quadratic formula and divides by gravity; a negative velocity/height is unphysical for a launch, and non-positive gravity breaks the time-of-flight and max-height calculations. All three conditions share one combined guard, so any one of them triggers this single message.

Source

Thrown at src/main/java/com/thealgorithms/physics/ProjectileMotion.java:72

     * @param initialHeight Starting height of the projectile (m).
     * @return A {@link Result} object with the trajectory data.
     */
    public static Result calculateTrajectory(double initialVelocity, double launchAngleDegrees, double initialHeight) {
        return calculateTrajectory(initialVelocity, launchAngleDegrees, initialHeight, GRAVITY);
    }

    /**
     * Calculates projectile trajectory with a custom gravity value.
     *
     * @param initialVelocity Initial speed (m/s). Must be non-negative.
     * @param launchAngleDegrees Launch angle (degrees).
     * @param initialHeight Starting height (m). Must be non-negative.
     * @param gravity Acceleration due to gravity (m/s^2). Must be positive.
     * @return A {@link Result} object with the trajectory data.
     */
    public static Result calculateTrajectory(double initialVelocity, double launchAngleDegrees, double initialHeight, double gravity) {
        if (initialVelocity < 0 || initialHeight < 0 || gravity <= 0) {
            throw new IllegalArgumentException("Velocity, height, and gravity must be non-negative, and gravity must be positive.");
        }

        double launchAngleRadians = Math.toRadians(launchAngleDegrees);
        double initialVerticalVelocity = initialVelocity * Math.sin(launchAngleRadians); // Initial vertical velocity
        double initialHorizontalVelocity = initialVelocity * Math.cos(launchAngleRadians); // Initial horizontal velocity

        // Correctly calculate total time of flight using the quadratic formula for vertical motion.
        // y(t) = y0 + initialVerticalVelocity*t - 0.5*g*t^2. We solve for t when y(t) = 0.
        double totalTimeOfFlight = (initialVerticalVelocity + Math.sqrt(initialVerticalVelocity * initialVerticalVelocity + 2 * gravity * initialHeight)) / gravity;

        // Calculate max height. If launched downwards, max height is the initial height.
        double maxHeight;
        if (initialVerticalVelocity > 0) {
            double heightGained = initialVerticalVelocity * initialVerticalVelocity / (2 * gravity);
            maxHeight = initialHeight + heightGained;
        } else {
            maxHeight = initialHeight;
        }

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Pass non-negative initialVelocity (m/s) and initialHeight (m), and strictly positive gravity (m/s^2, ~9.81 on Earth).
  2. Validate the three inputs separately in your UI/parse layer so you can report which one was wrong rather than one combined message.
  3. If modelling a launch from below origin, shift the coordinate frame so initialHeight stays non-negative.

Example fix

// before
Result r = ProjectileMotion.calculateTrajectory(-10, 45, 0, 9.81);
// after
Result r = ProjectileMotion.calculateTrajectory(10, 45, 0, 9.81);
Defensive patterns

Strategy: validation

Validate before calling

if (initialVelocity < 0 || initialHeight < 0 || !(gravity > 0)) {
    // report which field failed to the user
    throw new IllegalArgumentException("invalid launch parameters");
}
Result r = ProjectileMotion.calculateTrajectory(initialVelocity, launchAngleDegrees, initialHeight, gravity);

Prevention

When it happens

Trigger: Calling calculateTrajectory with a negative initialVelocity, a negative initialHeight, or gravity of 0 or negative. A velocity or height of exactly 0 is allowed (only gravity must be strictly positive).

Common situations: Config defaulting gravity to 0 in a test, a sign error on height when the object starts below ground level modelled as negative, or a UI form that allowed a negative speed value through.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/c0def2ea1056a984. Report an issue: GitHub.