Sign in to follow this  

Crosshair aims in front of the enemy

Recommended Posts

How to make a crosshair similar to the one in descent freespace. For those who don't know about it it was a full 3d space sim in which you selected an enemy and it showed you a moving crosshair in front of the selected enemy so if you shot through the crosshair you hitted the enemy and the crosshair always moved to help you aim depending on the position and speed of the enemy. Any ideas how can I make such a smart crosshair to help players aim at moving targets?

Share this post

Link to post
Share on other sites
Suppose both the projectile you shoot and the target move in straight lines at constant speed. So between the time you shoot and the projectile arrives, the target hasn't changed direction or sped up or slowed down.

Then the position of the target at 't' seconds later is
T = T0 + VT*t

and the position of the projectile is
P = P0 + vp*D*t

T0 = where the target is right now (vector)
P0 = where the shooter is right now (vector)
VT = velocity of the target right now (vector)
vp = speed of the projectile (scalar)
D = direction the projectile is shot at (unit vector) (this is what we want to find)

If the projectile hits the target, then P = T. Set these equatios equal and solve for D, and (if I didnt make a mistake) you get:
D = (T0-P0)/(vp*t) + VT/(vp)

But we still dont know what t is, so we need to use another piece of information, and that is that D is a unit vector. Thus ('.' means dot product)

1 = ||D|| = ||D||^2 = D.D
= ((T0-P0)/(vp*t) + VT/(vp)).((T0-P0)/(vp*t) + VT/(vp))
= ||T0-P0||^2/(vp*t)^2 + ||VT||^2 + 2*(T0-P0).VT/(vp*t)

Multiply each side by t^2 and it is a quadratic equation for t, which you can solve, since you know everything else in there. Then plug t get back into the equation for D, to find the direction the shooter needs to aim. (:

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this