After some research found a wiki page on projectile motion:
Wiki: Projectile Motion
Here is a simple function that takes a velocity (speed) and an angle and generates all the points through the flight of the object.
Test Function
void projectile(double velocity, double angle, float delta) { double vx, vy, ux, uy, tt, terminal_v; double gravity = 9.8; ux = velocity * Math.cos(angle * Math.PI / 180); uy = velocity * Math.sin(angle * Math.PI / 180); terminal_v = uy/gravity; tt = time_x*time_x; vy = uy * time_x - 0.5 * - -gravity * tt; if (time_x < terminal_v*2){ Vector2 t = new Vector2((float) vx + hero.x ,(float) vy + hero.y); DotArray.add(t); } time_x += delta; }ux - the initial velocity accross
uy the initial velocity upwards
terminal_v is the time it takes for the object to stop moving upwards, if you double this you get the total flight time (Given the object takes off from and lands at zero Y).
For each time render is called (usually 60fps) calculate the position of the object at the current flight time and add it to an array.
In the draw part of the code I just draw each co-ordinate held in the array of vectors. This needs work as it only works for positive x (Left to right) and is hard coded to run from the hero current position but this is just a test.
Was unsure how to square a double so just multiples time.
tt = time_x*time_x;Screen Shot
Better Total flight time calculation:
ReplyDeletetime_in_air = (uy + (Math.sqrt(Math.pow(uy, 2)-4*(gravity/2)*(-y_offset))))/gravity;