Exercise: Physics Trajectory Engine
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
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 staticdirective to directly access theSystem.Mathmethods without typing theMath.prefix.You must encapsulate the physics formula inside a
staticlocal function.The local function must not capture variables from the parent scope; you must pass
velocityandangleInRadiansinto 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 callPow()andSin()directly.A
staticlocal function resides inside the top-level scope and uses thestatickeyword to guarantee data isolation.
Exercise: Physics Trajectory Engine
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
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 staticdirective to directly access theSystem.Mathmethods without typing theMath.prefix.You must encapsulate the physics formula inside a
staticlocal function.The local function must not capture variables from the parent scope; you must pass
velocityandangleInRadiansinto 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 callPow()andSin()directly.A
staticlocal function resides inside the top-level scope and uses thestatickeyword to guarantee data isolation.