Question by
DanielDollerup · May 11, 2017 at 08:29 AM ·
velocityprojectileaccuracy
Calculating projectile lead - improving accuracy
Hello!
I'm trying to work out projectile leading in a space-game setting, so target objects will be moving all over the place.
Here's my code so far:
/// <summary>
/// Global method to help calculate leading projectile position.
/// </summary>
/// <param name="startPosition">The shooter position</param>
/// <param name="targetPosition">The target position, eg. the target you wish to hit</param>
/// <param name="targetVelocity">The targets current move speed</param>
/// <returns>Point in worldspace, as to where the target will be</returns>
private Vector3 ProjectileCalculateLead(Vector3 startPosition, Vector3 targetPosition, Vector3 targetVelocity, float velocity)
{
float distance = Vector3.Distance(startPosition, targetPosition);
float travelTime = distance / (velocity);
return (targetPosition + targetVelocity * travelTime) - startPosition;
}
It does what I want to some extent, but it seems to overshoot the target slightly.
So here's my question:
Anyone's got some ideas as to how to improve the accuracy on this?
Thanks in advance :)
Comment