given the position and velocity of an object how can I detect possible Collision?

I'm trying to detect Collision between autonomous moving objects and steer direction if collision is detected. so far I've been following a tutorial and I'm having a hard time to fully understand how it works especially the role of the dot product

foreach (GameObject t in targets) //get me all moving agents
{
Vector3 relativePos;
Agent targetAgent = t.GetComponent<Agent>(); //access properties like velocity
relativePos = t.transform.position - transform.position;
Vector3 relativeVel = targetAgent.velocity - agent.velocity;
float relativeSpeed = relativeVel.magnitude;


//can someone explain this part of code how did we calculate the time of collision?

 float timeToCollision = Vector3.Dot(relativePos, relativeVel);
timeToCollision /= relativeSpeed * relativeSpeed * -1;
float distance = relativePos.magnitude;
float minSeparation = distance - relativeSpeed *
timeToCollision;

• If the code is from a tutorial, did you try to contact the author? If you did, then next you need to learn Vector Math. Go check wikipedia or a math tutorial on youtube. It'l explain dot products better than text. – zimspy Jul 6 '16 at 17:42

The dot product has two nice properties for collision avoidance. The dot product is closely related to the cosine of the angle between two vectors.

The dot product of a unit vector A and a second vector B of any non-zero length, the result is the length of vector A projected in the direction of vector B.

So as you can see in the image; if the angle is greater than 90 degrees, the dot product will be negative. So, the dot product has to be a positive value or there will be no collision. Most likely in your code/tutorial the dot product is used to check if the objects are heading towards eachother (maybe based on a "viewcone" or something similar- for example a dot product of >0.5 equals a cone of 45 degrees). This is the first property: check if the objects are actually heading towards eachother by evaluating the angle between them.

Once decided a collision is likely based on the directions the objects, the next property comes into play:

The relPosition and relVelocity are is used to project the velocity vector onto the position vector:

float timeToCollision = Vector3.Dot(relativePos, relativeVel);


This line basically projects the Velocity (A in the image) onto the RelativePosition (vector pointing from the first agent to the 2nd agent).

Then the result is divided by the relativeSpeed * relativeSpeed (since the two objects both have their velocities turned into one relative speed value), to calculate the actual timeToCollision.

Note that this calculation does not mean the objects will collide! It merely calculates the time where the two objects are the most close to eachother during their trajectories (the variable name may be confusing you here!)

This is where these lines comes in:

float distance = relativePos.magnitude;
float minSeparation = distance - relativeSpeed * timeToCollision;


The minimal Separation is the distance between the two objects at the time of the closest approach. Remember relativePos is actually a vector from object A to B; the magnitude is the current distance. Shorten the distance by the distance travelled during the calculated timeframe relativeSpeed * timeToCollision and the result will be the minimal distance between the objects during the trajectories.

Now if the minimal distance is smaller than the sizes of the objects they will collide and the agent must take an evasive action.