Turret rotation w/ compensation

Hi,

I making an asteroids type game that has turrets (with AS3 in Flash Develop). What I’m trying to do is hit the moving ship, by compensating for its speed. So if the ship stays moving the same speed at the same slope etc. the shot will hit it. Right now I’ve been using this code (that isn’t really working):

var dis = Math.sqrt(Math.pow((Main.ship.x-this.x),2)+Math.pow((Main.ship.y-this.y),2))
                var m1 = (Main.yVelocity/Main.xVelocity);
                var m2 = dis/m1/m1;
                var x1 = (Main.ship.y-this.y)/(m2-m1);
                var y1 = (m1*(x1 - Main.ship.x))+Main.ship.y;


I’ve also tried:

var dis = Math.sqrt(Math.pow((Main.ship.x-this.x),2)+Math.pow((Main.ship.y-this.y),2))
                var m1 = (Main.yVelocity/Main.xVelocity);
                var m2 = dis/m1/m1;
                var x1 = ((-m2*Main.ship.x)+this.y+(m1*this.x)-Main.ship.y)/(m1-m2);
                var y1 = (m1*(x1 - Main.ship.x))+Main.ship.y;

Here’s the shot movement:


this.x += Math.sin(Trotation*(Math.PI/180))*10;
            this.y -= Math.cos(Trotation*(Math.PI/180))*10;

But neither seem to work. Note: yVelocity/xVelocity is the ship’s yspeed/xspeed, dis is the distance between the turret and the ship, m1 is the slope of the ship, m2 is the to-be slope of the shot, and x1, y1 calculates the intersection point between the 2 lines, which the turret then rotates to. Trotation being the rotation of the shot.

Now that I look at my idea for the dis/m1/m1 it is completely wrong because the bullet won’t have that speed, and the speed will get to high…

Any ideas how this would work?

Thanks!