Some of you probably saw my question projectile motion formula in lounge. I'm trying to implement two formulas, however I'm having an issue.
When I input an angle into the functions, for whatever reason some of them completely screw the thing up.
45 degrees works fine. However I turn it down one notch to 44 and the animation my program creates (using sfml by the way) shows drastically different results. It looks as if I shot the projectile straight up. If I set the angle to something like 30, then it just goes completely off-screen, -x & -y. Hell, even the ones I can see I know aren't right. If have the initial x velocity as 1, the vertex is way up on the y-axis.
I have two functions related to calculating the position.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
float getX(float in_vel,float angle,float time)
{
float x;
x = (25*sin(angle))*time;
return x;
}
float getY(float in_vel,float angle,float time)
{
constfloat g = -9.8;
float sAngle = 90 - angle;
sAngle = sin(sAngle);
float x = in_vel * sAngle;
x = x * time;
float y = ((g/2)*(time*time)) + x;
return y;
}
I'm not getting enough sleep lately, finding it super hard to think straight. Can someone please help me with what I'm doing wrong here?
I assume your angle is in standard position (no rotation is pointing directly right along the positive x-axis).
Well, getX() should just return in_vel*cos(angle)*time. Imagine the unit vector pointed at the angle you've specified; cosine will give you the fraction along the x-axis (in other words, the fraction of the velocity that is in the x-direction). Multiply that by your initial velocity and the time to get your position at the given time.
getY() needs to return (-9.8/2)*time*time + in_vel*sin(angle)*time. The first addend handles the gravity portion and the second addend handles the y-position over time.
Does that make sense?
Disclaimer: I wrote all of this from off the top of my head, so perhaps a few math errors may linger...
Edit: Also, I suspect the strange answers are resulting from sin() and such needing an angle in radians, not degrees.