Entry tags:
Closest approach of two particles.
So, I'm trying to work out whether a projectile will hit a moving target. That is, it its closest approach within the collision sphere of the target.
I have the position and velocity of the target and the position and velocity of the target. The obvious way of doing this is work out the position w.r.t time, use pythagoras to work out the distance (or distance squared because it makes things easier), differentiate to work out the time of closest approach and then put t back into the previous formula to work out the distance.
It works. Probably. But there has to be a nicer way. It's not like I need to know the time this happens or the actual positions. Just the distance. Adding the extra variable just seems to be making things horribly complicated.
So far what I have it that we can assume that the target is stationary at the origin, and subtract its position and velocity from our projectile, and work out closest approach to the origin. Now, this will be when the origin is at a right angle to the modified vector. I just can't work out how to establish how close this is.
Advice, mathematical geniuses of the interwebs?
I have the position and velocity of the target and the position and velocity of the target. The obvious way of doing this is work out the position w.r.t time, use pythagoras to work out the distance (or distance squared because it makes things easier), differentiate to work out the time of closest approach and then put t back into the previous formula to work out the distance.
It works. Probably. But there has to be a nicer way. It's not like I need to know the time this happens or the actual positions. Just the distance. Adding the extra variable just seems to be making things horribly complicated.
So far what I have it that we can assume that the target is stationary at the origin, and subtract its position and velocity from our projectile, and work out closest approach to the origin. Now, this will be when the origin is at a right angle to the modified vector. I just can't work out how to establish how close this is.
Advice, mathematical geniuses of the interwebs?
no subject
Still, I agree there ought to be a simple formula for the distance between 2 objects moving in a straight line.
no subject
I'm just a bit confused by this line:
var t = DotProduct(diffX, diffY, dirX, dirY) / DotProduct(dirX, dirY, dirX, dirY);
Isn't the dot product going to be scaled by the square root of that? In my case it doesn't matter because my motion vector is a unit vector plus a speed but I'm a little confused that I don't quite get it.
EDIT: Ah! Got it! The result is scaled by length but so is the initial vector. So we divide by that squared!
no subject
function DotProduct(x_1, y_1, x_2, y_2)
{
// finds the dot product of two vectors
return ((x_1 * x_2) + (y_1 * y_2));
}