Cool .
My friend solved a similar problem in 2-d for turrets firing at moving targets.
You have a few equations for the motion of the bullet
You also have a couple for the predicted motion of a target.Code:bulletX(t) = initialX + bulletVelocityX * t bulletY(t) = initialY + bulletVelocityY * t sqrt((bulletVelocityX - firingShipVelocityX)^2 + (bulletVelocityY - firingVelocityY)^2) = bulletVelocity
You can now solve X and Y seperately (with bulletVelocityX/Y and t being unknowns).Code:targetX(t) = initialTargetX + targetXVelocity * t targetY(t) = initialTargetY + targetYVelocity * t
Now you have two equations, one which tells you what time the x-pos would intersect given bulletXVelocity, and one given Y.Code:initialTargetX + targetXVelocity * t = initialX + bulletVelocityX * t t = (initialX - initialTargetX) / (targetXVelocity - bulletVelocityX) initialTargetY + targetYVelocity * t = initialY + bulletVelocityY * t t = (initialY - initialTargetY) / (targetYvelocity - bulletvelocityY)
So if you fill in an estimate X velocity for the bullet, you get back the Y velocity you'd want to fire it at and vice versa. Of course, you want the net velocity of the bullet to be the firing speed of the gun, minus whatever speed the ship is already moving it. We couldn't find a way to solve this part in to one equation, but you can iterately test X velocities till you find something that satisfies your velocity constraint, something ALA newtons method gets fairly accurate in 3-4 iterations.Code:(initialX - initialTargetX) / (targetXVelocity - bulletVelocityX) = (initialY - initialTargetY) / (targetYVelocity - bulletVelocityY)
Hope that's helpful.
Programming Your Mom. http://www.dandongs.com/
I'd describe the path of the laser and the path of the object parametrically, then solve the following three equations simultaneously for t, laser_vel_x, and laser_vel_y:
Having found t, which you are not directly interested in (it is the time it will take for the laser to reach the target), and the two laser velocities, you back-substitute to find the intersection point. This point is where you should put the reticle.Code:target_x + target_vel_x * t = laser_x + ( laser_vel_x + source_vel_x ) * t target_y + target_vel_y * t = laser_y + ( laser_vel_y + source_vel_y ) * t laser_vel_x * laser_vel_x + laser_vel_y * laser_vel_y = laser_speed * laser_speed
The math is identical in three dimensions, you just have a fourth equation to solve.
(And I don't mean solving these equations at run-time via some iterative method. The solution exists in closed form. Due to eq 3, the solution will involve taking a square root at some point. And in some cases, the radicand of the square root will be negative, which indicates that it is IMPOSSIBLE to hit the target -- for instance, it is moving away from you faster than the laser can travel)
Last edited by brewbuck; 10-01-2009 at 12:18 PM.
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}
Just a little SDL program I made a while back which involves these sorts of calculations: http://dwks.theprogrammingsite.com/m...332814f.tar.gz
(Or http://dwks.theprogrammingsite.com/m....0-332814f.zip)
There's a comment at the top of the source which explains what's going on.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.