Jump to content
  • Advertisement
Sign in to follow this  
ahlywog

Time till intersect?

This topic is 3491 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all, I'm trying to the equation to figuring out when two objects, traveling at different speeds, will intersect if they're heading towards each other. 10 years ago in high school I was thinking 'when will I EVER need this?' and now I do. LOL. Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
distance = velocity * time

Since both points are moving linearly, you can use this to write the entire thing as 1 equation:

distance_between = combined_velocity * time

Or,

time = distance_between / combined_velocity

It doesn't get any easier!

Share this post


Link to post
Share on other sites
Alright I think I get what you're saying.

That is pretty easy. Makes me with I had paid more attention in school. :-P

Thanks again.

Share this post


Link to post
Share on other sites
That only works if they are heading straight for each other (think 2 trains on the same track heading towards each other). If this is your situation, great, otherwise, it is not so simple. If it is a 3d equation, then you have to solve the following

Object 1
x1 = currentx1 + vx1*t
y1 = currenty1 + vy1*t
z1 = currentz1 + vz1*t

Object 2 has the same equations, but different numbers
Your question is when they will collide, so a collision will occur when all 3 of these equations are true:

x1 = x2 therefore, currentx1 + vx1*t = currentx2 + vx2*t
y1 = y2 therefore, currenty1 + vy1*t = currenty2 + vy2*t
z1 = z2 therefore, currentz1 + vz1*t = currentz2 + vz2*t

Solve the equations and you will have your answer.

Share this post


Link to post
Share on other sites
Quote:
Original post by ahlywog
It's in 2D for now so what he gave me works.

Thanks though.


Well, actually the problem (as you stated it) and the solution that I gave you, is 1-dimensional (although, it still works when embedded in a 2D or any higher dimensional space).

landagen's generalization is written for 3-dimensions. It can be easily simplified to work in 1 or 2 dimensions by throwing away the additional equations.

For example, if you throw away the Y and Z dimensions, you have just 1 equation left:

currentx1 + vx1*t = currentx2 + vx2*t

Which can be rearranged as:

vx1*t - vx2*t = currentx2 - currentx1
t ( vx1 - vx2 ) = currentx2 - currentx1
t = (currentx2 - currentx1 ) / ( vx1 - vx2 )

Refer back to the equation I gave you:

time = distance_between / combined_velocity

And you will see that, in fact, they are identical. Actually, my equation is technically wrong because I should have said combined rate (or speed), whereas velocity is a vector that has a direction. landagen's version correctly uses velocity as a vector, so for two objects approaching each other, one of them would have negative velocity, and the negatives cancel making an addition.

Share this post


Link to post
Share on other sites
Quote:
Original post by ahlywog
Hey all,

I'm trying to the equation to figuring out when two objects, traveling at different speeds, will intersect if they're heading towards each other.

10 years ago in high school I was thinking 'when will I EVER need this?' and now I do. LOL.

Thanks in advance.


Think of a sailing party.
You are sailing boat A, your companion is sailing boat B. Assume both of you sail along a straight track with uniform speed. Then you are on collision course with B if and only if the the line AB does not change direction and the distance decreases.

Vector calculation: Let O be the origin of coordinates. Then OA and OB are the radii vectores of you and your companion. Vector AB = OB - OA is B's position relative to you.

Assumption: Both of you sail with a certain constant speed in a certain constant direction.
During a unit of time A travels from point pA to point qA. Likewise, B travels from pB to qB.
The vectors vA = from pA to qA and vB = from pB to qB are the velocity vectors by definition.
The velocity of B relative to A is vB - vA. If and only if this vector has a direction exactly opposite to AB you are on collision course with B.

Now for the time t to actual collision: t = distance AB / relative speed.

Distance AB = sqrt ( (xB-xA)^2 + (yB-yA)^2 ) - Good old Pythagoras.
Likewise, relative speed = sqrt ( (xvB-xvA)^2 + (yvB-yvA)^2 ),
where...
xvA, yvA are the x and y components of the velocity vector of A, and
xvB, yvB are the x and y components of the velocity vector of B.

Tests for actual collision during the game: (A) if the relative speed does change you are not on a collision course. (B) if the direction of line AB changes you are not on a collision course.

All this also holds in 3D. One only has to add the Z component to the vector calculations; in particular for distance and speed we have the generalized Pythagorean formula sqrt (x^2 + y^2 + z^2) instead of sqrt (x^2 + y^2).

Happy sailing and happy gaming: Johan E. Mebius, AKA Joher

[Edited by - Joher on January 27, 2009 5:14:00 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!