# Maximum range of projectile fired at given force and elevation

I've gone over this again and again, and my result is obviously wrong when viewed in-action. Here's the initial formula I converted (first one):

http://en.wikipedia.org/wiki/Range_of_a_projectile

and here's my C# code:

``````float CalculateMaximumRange() {
float g = Physics.gravity.y;
float y = origin.position.y;
float v = projectileSpeed;
float a = 45;

float vSin = v * Mathf.Sin(a);
float vCos = v * Mathf.Cos(a);

float sqrt = Mathf.Sqrt(vSin * vSin + 2 * g * y);

return Mathf.Abs((vCos / g) * (vSin + sqrt));
}
``````

Does anyone see what I did wrong?

-
Two things, this is in 2d? And does your Trig functions require radians instead of degrees? – Chewy Gumball Mar 30 '11 at 2:14
Not only that but your variable names could be better. vSin should probably be v_x to represent that it it the X component of v for example. – Robert Massaioli Mar 30 '11 at 3:28
What is the MathF class anyway, I' used to seeing Math and MathHelper (float math), not MathF. – Roy T. Mar 30 '11 at 6:41
@Roy T, it's Unity's floating point math helper class. – Olhovsky Mar 30 '11 at 20:22
Do you have unit tests where you verify the range given a bunch of different inputs? – ashes999 Mar 31 '11 at 11:07

45 degrees is Pi/4 radians ~= 0.785398.

The `y` in your formula should be the object's distance above the ground when it is released. Is the ground 0? If the ground is not at y=0, then y should be:

`y = origin.Position.y + groundHeight`

Finally, are you sure that this formula is giving you what you want? This formula gives the horizontal distance of the object's travel only -- not the distance of the object along the path (which is longer or equal to the horizontal travel).

Based on your response, it sounds like you're actually looking for the maximum possible distance that can be achieved.

To find the angle that gives the maximum possible distance, use this (using your variable names):

`````` a = arccos(sqrt((2*g*y + v*v)/(2*g*y + 2*v*v)))
``````

Now you can plug `a` into the equation you already have to find what the distance is at that angle.

Now you have both the angle that produces the maximum distance, and the maximum possible distance itself!

For example: g = 30, y = 3.85, v = 35, gives:

Wolfram Alpha computation.

a = 0.7422637 radians = 42.53 degrees.

Then plugging a into your original formula gives range = 44.517.

-
"This formula gives the horizontal distance of the object's travel only" -- CalculateMaximumRange looks just right. – Kromster Mar 30 '11 at 5:46
Judging by "MathF", Joren is using Unity. In Unity MathF.Sin and MathF.Cos indeed work with degrees, not radians. – Nevermind Mar 30 '11 at 6:55
Joren should definitely check his constants (g) and units (g, v, y, a, result) – Kromster Mar 30 '11 at 8:50
@Nevermind: Mathf.Sin appears to use radians: unity3d.com/support/documentation/ScriptReference/… – Olhovsky Mar 30 '11 at 9:08
WTF, I could've sworn it uses degrees. OK, I was wrong, sorry. – Nevermind Mar 30 '11 at 13:07