Quantcast
Channel: Answers by "3j-"
Viewing all articles
Browse latest Browse all 8

Answer by 3j-

$
0
0
I like to split these problems out into two components. Figure out how long it will take your missile to reach your target, then use that time to predict where your target will be. To figure out the first part, you'll want to figure out how far your target is from the missle currently and how fast your target is moving *away* from the missile. This means trimming the target's velocity so that all that remains is how fast he is moving along the line between where he is and where the missile is. You then use this relative velocity to determine how long it will take for the missile to catch up to the target. Once you have that time, you use it to predict where your target will be in that time, and then aim the missile for that destination. // The vector that extends from the missile to the target TargetOffset = TargetLocation - MissileLocation; // The distance from the missile to the target TargetDistance = TargetOffset.magnitude; // Normalize the offset vector into a direction - same as doing TargetOffset.normalized TargetDirection = TargetOffset / TargetDistance; // How fast the target and missle are moving relative to one another (if the missile // hasn't been fired yet, use TargetDirection * FiringSpeed for his velocity) // Another way to think of this is how fast the missile would be moving relative to // the target if the target wasn't moving at all RelativeVelocity = MissileVelocity - TargetVelocity; // How fast the target is moving away from the missile RelativeSpeed = Vector3.Dot( RelativeVelocity, TargetDirection ); // If RelativeSpeed is negative, that means the target is traveling faster than the // missile and the missile cannot catch up to it. // For this case, you can just fake an estimated intercept time so the missile at // least makes a decent attempt if ( RelativeSpeed <= 0.0 ) { InterceptTime = 1.0; } else { InterceptTime = TargetDistance / RelativeSpeed; } // We now have an estimate of how long it will take our missile to catch up to the // target - plug it in to his physics equation to predict where the target will be. InterceptLocation = TargetLocation + TargetVelocity * InterceptTime; // Aim the missile towards this location AimDirection = ( InterceptLocation - MissileLocation ).normalized; MissileVelocity = AimDirection * MissileVelocity.magnitude; Note I didn't test any of this code, it's just from the top of my head from having to do this several times. Also, this won't give you the calculus-perfect interception time and location, but it's pretty close and can be refined by running another iteration of it on the newly-predicted intercept location.

Viewing all articles
Browse latest Browse all 8

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>