Hey guys, can you help me with this?
The problem is about projectile motion. Given an origin point (ox,oy), a target point (tx,ty) and a initial velocity v, I want to find the angle at which the projectile should be launhed from the origin in order to touch the target. This is the formula I found at wikipedia:
Angle required to hit coordinate (x,y) from origin (0,0)
double getAngleToHit(double ox, double oy, double tx, double ty, double v){
double x = tx - ox;
double y = -(ty - oy);
double g = 10;
return atan2(v*v - sqrt(pow(v,4) - g*(g*x*x + 2*y*v*v)),g*x);
}
Notes:
1) x = tx - ox because the x of the wiki formula is the horizontal range.
2) y = -(ty - oy) same reason as above AND in my game coordinate system the y grows towards the bottom of the screen.
The angle returned is not quite right. I watched this code run various times with different parameters and I noticed a pattern... if y is equals 0, that meaning origin and target are on the same height, the result is something like this:
[attachment=28343:c1.png]
Red = Origin, Green = Target, Blue = Resulting Trajectory
Then I though maybe the wiki formula isn't right and decided to tweek it a little bit and divided x and y by 2. And it worked perfectly, in all cases.
I looked for other sources that could confirme that the wiki formula is right but I couldn't find any. So this is the code I'm working with now:
double getAngleToHit(double ox, double oy, double tx, double ty, double v){
double x = (tx - ox)/2;
double y = -(ty - oy)/2;
double g = 10;
return atan2(v*v - sqrt(pow(v,4) - g*(g*x*x + 2*y*v*v)),g*x);
}
But just because I'm not wrong doesn't mean I'm right. Does that makes sense? Can someone give me a light here? Where is the proof of wiki's formula? Why am I wrong and right at the same time? It is driving me crazy, I already checked all my other related funcions dealing with the projectile speed, acceleration, gravity etc.
Thx!