- Home /

# How do you check if two objects WOULD collide if their trajectory reimains the same?

Is there any build-in function? If not, could you explain me the principle? Example: Two cars running at different speeds on two different streets which at some points cross. Can you know when & where would they collide if the drivers don't break?

### FAQ :

We want Unity Answers to become a comprehensive database of questions and answers related to Unity.

Your question is not directly related to Unity, this is a pure mathematical problem, and thus, does not belong to Unity Answers. You will easily find an answer of your problem on the Internet with scientific papers about the subject.

NB : If you check your mails, you will see I have let a comment before rejecting your question

**Answer** by elenzil
·
Jul 08, 2016 at 09:35 PM

if you approximate your objects as spheres, then you can set this up algebraicly.

```
T0 = time now
RA = radius of sphere A
RB = radius of sphere B
VA = velocity vector of A
VB = velocity vector of B
PA0 = position of sphere A at time 0
PB0 = position of sphere B at time 0
PA(t) = PA0 + VA * (t - T0) // position of A at time t
PB(t) = PB0 + VB * (t - T0) // position of B at time t
want to find:
TC = time of collision, if any.
so you want to find the moment when the distance between the spheres is exactly equal to the sum of their radii. ie, solving for TC in this formula:
magnitude(PA(TC) - PB(TC)) = RA + RB.
when you solve for TC you will get 0, 1 or 2 real solutions.
if you get 2 solutions, the lesser of them is the moment of collision.
if you get 1 solution (unlikely) then they just graze each other.
if you get 0 solutions they don't collide.
this approach does not deal with the case where the two spheres are already overlapping.
```

**Answer** by Kusras
·
Jul 08, 2016 at 09:52 PM

Maybe this could help you - https://docs.unity3d.com/ScriptReference/Rigidbody.SweepTest.html. with some math added

this is a cool routine but i'm not sure it will work in this case, as the other object needs to be swept also, and you need to account for time. for example, if one car passes through the intersection long before the other car does, then their sweep volumes will intersect even tho the cars did not actually collide.

also, your URL has a dangling period on it.

**Answer** by EDevJogos
·
Jul 08, 2016 at 09:33 PM

You could use a Raycast with x distance, if the trajectory remains the same eventualy the ray will collide with the other car, just set the direction of the ray to the other object you want to check if a collision is imenent.

https://docs.unity3d.com/ScriptReference/Physics.Raycast.html

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Detect Side of Collision 2 Answers

Manual Collision Detection... 2 Answers

Particle Collision / Trigger not being reported to On_xx Event 1 Answer

CharacterController collision detection with pendulum like object 0 Answers

Collision detection and prevention between a rigid body and box collider 0 Answers