I need a bit of help with the maths for a targeting system for a space simulator. The problem is working out the correct turret angle so that the bullets hit the target ship...
I have a ship at location X, Y and the ship has turrets which can turn 360 degrees. I want to aim at an asteroid at location Xa, Ya.
The maths for this which is working great is like this....
VectorX = X - Xa VectorY = Y - Ya Angle = (57.2957795 * atan2(VectorX , -VectorY ))
Now consider the asteroid is moving at a velocity vector of Xsa, Ysa and the bullets fired from the turret have a speed.
The maths for this which again works really well is....
VectorX = X - Xa VectorY = Y - Ya Distance = sqrt((VectorX * VectorX) + (VectorY * VectorY)) VX = VectorX - (Xsa * (Distance / BulletSpeed)); VY = VectorY - (Ysa * (Distance / BulletSpeed)); Angle = (57.2957795 * atan2( VX, -VY )
Ok the bit I cant get to work right is when the ship is also moving, lets call this velocity vector Xs, Ys. When the ship is moving the bullets fire maintain the movement of the ship plus the bullet firing speed at the angle vector. Think of driving a car and shooting a bullet out the window the bullet would fire out sideways from the car but would also maintain the car's forward momentum as it traveled.
BulletVelX = Xs + ( BulletSpeed * cos( Radians( Angle ) ) ) BulletVelY = Ys + ( BulletSpeed * sin( Radians( Angle ) ) )
I have found a site that says simultaneous equasions could be used to solve the problem but has anyone working knowledge of how this is done.