Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
    
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
add comment

1 Answer

up vote 7 down vote accepted

Use degrees instead of radians.

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.

share|improve this answer
    
"This formula gives the horizontal distance of the object's travel only" -- CalculateMaximumRange looks just right. –  Krom Stern Mar 30 '11 at 5:46
1  
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) –  Krom Stern Mar 30 '11 at 8:50
1  
@Nevermind: Mathf.Sin appears to use radians: unity3d.com/support/documentation/ScriptReference/… –  Olhovsky Mar 30 '11 at 9:08
2  
WTF, I could've sworn it uses degrees. OK, I was wrong, sorry. –  Nevermind Mar 30 '11 at 13:07
show 13 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.