Exercise: Physics Trajectory Engine

Use global static imports and isolated local functions to build a physics calculator.

Problem statement

You are developing the physics engine for a 2D artillery game. When a player fires a projectile, the engine must calculate the total horizontal distance it will travel before hitting the ground.

The formula for projectile distance is:

where vv is velocity, θ\theta is the angle in radians, and gg is gravity (9.819.81).

Task requirements

  • Implement the physics formula to calculate the distance.

  • Format the final distance by rounding it to exactly two decimal places.

  • Print the calculated, rounded distance to the console.

Constraints

  • You must add a using static directive to directly access the System.Math methods without typing the Math. prefix.

  • You must encapsulate the physics formula inside a static local function.

  • The local function must not capture variables from the parent scope; you must pass velocity and angleInRadians into it as parameters.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • Place using static System.Math; at the very top of the file to call Pow() and Sin() directly.

  • A static local function resides inside the top-level scope and uses the static keyword to guarantee data isolation.

Exercise: Physics Trajectory Engine

Use global static imports and isolated local functions to build a physics calculator.

Problem statement

You are developing the physics engine for a 2D artillery game. When a player fires a projectile, the engine must calculate the total horizontal distance it will travel before hitting the ground.

The formula for projectile distance is:

where vv is velocity, θ\theta is the angle in radians, and gg is gravity (9.819.81).

Task requirements

  • Implement the physics formula to calculate the distance.

  • Format the final distance by rounding it to exactly two decimal places.

  • Print the calculated, rounded distance to the console.

Constraints

  • You must add a using static directive to directly access the System.Math methods without typing the Math. prefix.

  • You must encapsulate the physics formula inside a static local function.

  • The local function must not capture variables from the parent scope; you must pass velocity and angleInRadians into it as parameters.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • Place using static System.Math; at the very top of the file to call Pow() and Sin() directly.

  • A static local function resides inside the top-level scope and uses the static keyword to guarantee data isolation.

C# 14.0
// TODO: Add the using static directive for System.Math
double velocity = 25.0; // meters per second
double angleInRadians = 0.785398; // 45 degrees
// TODO: Call your local function, round the result, and print it
// TODO: Define a static local function named CalculateDistance
// It should accept velocity and angle as parameters
// Inside, declare gravity as 9.81 and implement the formula