[Physics] How to calculate the time until collision

collisionhomework-and-exercisesnewtonian-mechanics

I'm programming a 1 dimensional physics simulator in python. How do I calculate the amount of time until two particle points that are traveling towards eachother collide? I am using the equation:

time = (m2.pos - m1.pos) / (m1.velocity - m2.velocity). 

You can assume m1 is always on the left, and m2 is always on the right. This equation works fine in my program if both points are traveling in the same direction at slightly different speeds, but when m1 has a positive velocity and m2 has a negative velocity, that gives the denominator of my equation a negative number which produces a negative amount of time until the next collision.

Best Answer

If m1 has a positive velocity and m2 has a negative velocity, you get (m1.velocity - m2.velocity) = (positive - negative) = positive. On the other hand if the signs are switched, you'll get a negative result because the particles are moving away from each other.

Related Question