# Projectile Rotation During Flight (Tank Game)

I have a tank firing projectiles into the air.

I want the projectiles to be rotated relative to their position along their flight path.

See the diagram I drew in the image below.

At any given point I know the x and y of the projectile, z represents initial velocity, theta is also known.

• – Anko
Aug 19 '15 at 15:03

Your problem is not that tricky : you do have a formula that will give you the next position (at t+dt), given the current one.
Now say the projectile is at P : use the formula to get the next point NP : D = (NP-P) is the direction vector at t.

Now depending on what you seek :
• Angle ? Get the angle (atan2) of this D vector, to have the angle of your projectile.

angle = atan2 ( ( D.y), (D.x) ) ;


• Vector ? Normalize the Direction vector to have the normalized direction of your missile.

dLength = sqrt( sq(D.x) + sq(D.y) ) ;
D.x /= dLength;
D.y /= dLength;


(Rq : normalized means it has a length of one.
So if P is the rear of the missile, and D is the normalized direction, missile's head is at P + missileLength * D );

( Another remark : from your post it seems that you do not compute the velocity vector. But if you do, the missile direction is this very velocity vector, just take its angle or normalize it to get what you want. )

if you now x and y at any given point then localy derivate in respect of time.

Let x,y be current position at current time, let x1 and y1 be position at current time - small delta , then compute the slope as

m = (y-y1) / (x-x0)

beware subdivision by zero (x = x0 means proiectile perpendicular)

For the rotation angle given y-y1 and x-x1 you can enjoy some trigonometry :

it is very simple, somehow you have some kind of speed right ? i mean on each update you add some kind of velocity to x and y so they can move to their target?
now
you have your bullet x and y
and you have the velocity that must be added each update
bullet.rotation = atan2(velocity.y, velocity.x);