Sunday, March 13, 2011

Horizontal Target Tracking

Woo!

Just finished coding the turret of my current project to lead a target thats moving only horizontally. It took me a while to wrap my head around alot of the numbers, and also I was accidentally using degrees to start off my turrets heading and not radians. Wups!

Anyway, so this is how I accomplish leading a target horizontally:


 first using vector arithmatic and dot products and arcCos, we get the heading we'll be firing on.
 then we account for possible time delays in order to account for differences in distance.

 Heres the code: there is some stuff missing that is declared a bit above, such as getting the hBound which is the time where the distance the projectile will travel becomes greater (or equal to) than the distance the target will travel in the same amount of time. This is used so that we can lead the target. But because we lead it, we need to make sure to fire at the correct time in order to score a direct hit. So thats why we do some subtraction between the amount of distance and time and all that. Anyway: Heres the code:



if(target.velocity.y == 0 && target.velocity.x != 0){ //target only moves horizontally
                //get the heading to fire on.
                estMagPos = estMagPos + estMagVel*hBound;
                Vector2D gt =  (estMagPos - gun.position ).Normalize();
                gun.theta -= acos(Dot(gt,gun.heading));
                gun.heading = gt;
                //now, account for leading and time delay
                //hBound * vel = how much distance the proj will travel. 
                //Use the gun.theta and this to resolve the vector into it's components
                //then the distance between the (hBound * vel) - x of target
                double projd = hBound * vel;
                projd = (cos(gun.theta ))*projd; // x distance of vector
                double deltax = projd - estMagPos.x;
                double waitT = deltax/vel;
                gun.countdown = -waitT; //negation to make positive. no neg times
                gun.locked = true;
                std::cout << "Target Locked. Countdown Til Firing: " << gun.countdown << std::endl;
                system("hal target lockd");


So, the calls to system and hal are just a speech program to say the string aloud. 

No comments:

Post a Comment