   Turning a turret against a linear moving target
  I am working on a aiming mechanism to turn a turret against a linear moving target. I have its direction, speed and current position and the turret turn at a fixed rate, and I update its position.1)My initial try just moved the turret to the angle of my opponent and fired, but because of the timedelay of the bullet the enemy was missed.2)I then tried estimating the angle offset to aim at to make the bullet hit the target. I just made a triangle with the following sides:a = initial distance to targetb = Venemy * timec = Vbullet * timeI know the angle C, between the vector from my enemy to his movement direction, so i solve this equation.cos(B)=(a^2^+c^2-b^2)/(2*a*c)This works, but the turret does not move instantly, so I hit begind my target as long as it moves. Now I want to extend the c length to: c = Vbullet*(t+B/turnrate), to get the turrets turn-delay into the equation.This is where I am stuck. Is the problem setup correctly, and how do I solve the equation.Thanx in advance.TAX   
  Whoop the first equation was:cos(B)=(a^2+c^2-b^2)/(2*a*c)And I forgot to say the bullet also moves linearly, so I solve it in 2D.TAX;  this question has been asked many times. It is best to work with vectors than trigonometry. here's a thread about it.Basically, you work out the point of intercept (there can be two BTW), and rotate your turret towards it. There is some sample code provided as well.   ;  It sounds like he's already got the intercept right, if the turret hits when allowed to snap into the correct aim direction instantly. On the other hand I'm not sure what the current problem is... If you know the correct aim direction, all you should need to do is turn the turret towards that angle and fire when you're pointing the right way. Are you calculating the firing solution only once, and then firing after a delay caused by the turret's slow rotation? This will not work. You need to calculate the firing angle continuously even as the turret is swiveling; this will cause it to eventually reach a position where the turret is pointing in the ideal angle for that instant in time (or close enough to score a hit) and then you fire. The firing decision making is "parallel" to the aiming process... Not "1. decide to fire  2. aim  3. fire" but "while(aiming) { if (ok_to_fire) { fire } }"   ;  Thanx Guys!I looked at the example ai_aim.zip file and I think thats my solution. I will try to use it and see how it goes from there.I will just move the turret and fire when the error is small enough. :)Ill get back when its implementedTAX;  If your turret rotates at a constant rate and your projectiles advance with uniform speed, a more elegant solution would be to compute the exact solution once, taking into account the turret's rotation time.r := rotational speed of the turret, in radians per seconds := speed of the projectile, in meters per secondP := target's initial positionv := target's velocity (a vector describing how much it moves in a second)O := turret positiond := turret's initial direction (as a unit vector)t := time elapsedIf you turn the turret at full speed, fire and hit the target, let's see how much time has elapsed. The position where you hit the target is P+t*v.t = acos(dot_product(P+t*v-O,d))/r + distance(O,P+t*v)/sThat's an equation in t. You can use something like Newton-Raphson to find a solution, and now you have the value of t (remember you are only interested in positive values of t). From it, you compute where the target will be in t seconds, which is P+t*v. Now just rotate the turret to that position at full speed and shoot.   ;Quote:Original post by alvaroIf your turret rotates at a constant rate and your projectiles advance with uniform speed, a more elegant solution would be to compute the exact solution once, taking into account the turret's rotation time.Isn't that a little less than practical in most situations? How many targets would keep moving in a linear direction while your turret rotates to fire at them? The best solution to the situation itself (not the aiming algorithm) already calls for constant adjustments, so managing the aiming goal in one go probably won't do much for it.   ;  Hi All!First of all thanx for the replies.To continue my quest towards nice shooting, I have been looking into the solution posted by alvaro: Thanx! :) I think it might fit my application nicely.  (A bot implementation for Gun-Tactyx)My math skills might be a bit dodgy, but I just dont get it? So please hear me out:Here's the equation:t = acos(dot_product(P+t*v-O,d))/r + distance(O,P+t*v)/s1) I think that the dot product assumes that the "P+t*v-O" part should be normalized to make sense when divided by the rotation rate? Is that correct?2) I dont get how to read the equation. As I read it:The total time = the angle difference divided by the rate of turret change + the distance to the moving target, divided by the bulletspeed.As i see it: the equation expresses the time as one, and not as two seperate times required for the two actions? Please correct me, because I cant seem to get it right. The value I need to end up with would be the time to turn the turret (and its turn-direction) before pressing the launch button.-I have tried just following the eqation without understanding it, and I did the Newton-Raphson method, which I did not know beforehand. A very nifty tool I havent used before! But it does not work for me.This is what I did:t=arccos((Px+t*Vx-Ox)*Dx+(Py+t*Vy-Oy)*Dy)/r+sqrt((Px+t*Vx-Ox)^2+(Py+t*Vy-Oy)^2)/s;With the following example: Px:=10;Py:=2;Ox:=2;Oy:=2;Vx:=0;Vy:=1;Dx:=-1;Dy:=0;r:=-Pi/s;s:=2;That gives:t = -2*(Pi-arccos(8))/Pi+1/2*(64+t^2)^(1/2)I tried my NR guesses around 10 seconds but I keep getting results in negative time. What am I doing wrong here.Thanx in advance.TAX;Quote:Original post by taxfromdkHere's the equation:t = acos(dot_product(P+t*v-O,d))/r + distance(O,P+t*v)/s1) I think that the dot product assumes that the "P+t*v-O" part should be normalized to make sense when divided by the rotation rate? Is that correct?Ooops! You are right. You do need to divide by the lengths to compute the angle.Quote:2) I dont get how to read the equation. As I read it:The total time = the angle difference divided by the rate of turret change + the distance to the moving target, divided by the bulletspeed.Yes, that's what the equation says.Quote:As i see it: the equation expresses the time as one, and not as two seperate times required for the two actions? Please correct me, because I cant seem to get it right.I don't see the problem. We first compute the total time to hitting the target, from that we know the position of the target when we hit it, and from that we can compute very easily what the angle of the turret should be when we shoot.Can you redo your example with the normalization fix?   