You want the angle of the arrow at any point in time.
You remembered that in order to compute an angle, there's a tangent. But here's where your thinking started to go wrong:
- What you want is delta y / delta x, because the slope is the rate of change (mentioned in one of the other answers).
Note that x is just the position where you are at any moment in time, not dx.
Ok, so if you neglect air friction, then the x-velocity of the arrow is a constant.
First, decompose velocity into x and y components. You could be shooting at an angle of 45 degrees or 60 degrees. So you need launchVelocity and an angle, it's not a scalar.
Second, compute everything as double, not float. You're not numerically sophisticated enough to know when roundoff error won't kill you, so don't try. It's not a great time saver in any case.
Third, don't use Math.pow, it's slow and not as accurate as multiplying for integer powers. Also you can save a lot of time by using Horner's form (see below)
final double DEG2RAD = Math.PI/180;
double ang = launchAngle * DEG2RAD;
double v0x = launchVelocity * cos(ang); // initial velocity in x
double v0y = launchVelocity * sin(ang); // initial velocity in y
double x = (v0x * time);
// double y = (v0y * time) + (0.5 * g * (float)Math.Pow(time, 2));
double y = (0.5 * g * time + v0y) * time
If you are desperate for performance, you can even precompute 0.5*g, but the above code will take you 90% of the way there without doing anything too crazy. Benchmark doing this 10 million times if you like, it's admittedly not a huge amount of time but percentage-wise it's pretty big -- libraries are very slow in Java
So, if you wanted the angle at which the arrow should go, what you want is
And in this case, that would work because dx is a constant. But in general, dx can be zero, so you usually want to use:
which is a function specifically designed for this job.
But as I said, library functions in Java are hideously slow, and in this case there's a better way of doing it without as alluded to by @FxIII above.
If the horizontal velocity is always v0x, and the vertical velocity is:
double vy = v0y - 0.5 * g * time;
then your delta is:
You don't need the angle. If you wanted to draw an arrow, use something nominally like:
plot( x, y, x+vx, y+vy);
I don't know what you're drawing, so if you do need the angle to rotate it (like you're using JOGL) then sure, use the angle.
Don't forget if you are using opengl to turn the angle back into degrees, because ATAN2 returns radians:
final double RAD2DEG = 180 / Math.PI;
double ang = Math.atan2(vy,vx); // don't forget, vy first!!!
double deg = ang * RAD2DEG;