Cool :) .

Printable View

- 09-27-2009yann
Cool :) .

- 10-01-2009CrazyNorman
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

Code:`bulletX(t) = initialX + bulletVelocityX * t`

bulletY(t) = initialY + bulletVelocityY * t

sqrt((bulletVelocityX - firingShipVelocityX)^2 + (bulletVelocityY - firingVelocityY)^2) = bulletVelocity

Code:`targetX(t) = initialTargetX + targetXVelocity * t`

targetY(t) = initialTargetY + targetYVelocity * t

Code:`initialTargetX + targetXVelocity * t = initialX + bulletVelocityX * t`

t = (initialX - initialTargetX) / (targetXVelocity - bulletVelocityX)

initialTargetY + targetYVelocity * t = initialY + bulletVelocityY * t

t = (initialY - initialTargetY) / (targetYvelocity - bulletvelocityY)

Code:`(initialX - initialTargetX) / (targetXVelocity - bulletVelocityX) = (initialY - initialTargetY) / (targetYVelocity - bulletVelocityY)`

Hope that's helpful. - 10-01-2009brewbuck
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:

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) - 10-02-2009dwks
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.