# Checking collision if you have two velocity vectors and two 3D positions

## Recommended Posts

I have two velocity vectors in 3D and their positions, how would I check if they are colliding in 3D without using bounding sphere or any other primitive.

 const double tx = poi.Position().GetLongitude() - m_sPosAV.GetLongitude() / m_sVelAV.x - velocity.at(0).toDouble();
const double ty = poi.Position().GetAltitude() - m_sPosAV.GetAltitude() / m_sVelAV.y - velocity.at(1).toDouble();
const double tz = poi.Position().GetLatitude() - m_sPosAV.GetLatitude() / m_sVelAV.z - velocity.at(2).toDouble();

if (tx == 0 || ty == 0 || tz == 0)
{
qDebug() << "collision ";
return true;
}

if (tx && ty && tz)
{

if (tx == ty)
{
qDebug() << "collision at that time ";
return true;
}
else if (tx == tz)
{
qDebug() << "collision at that time";
return true;
}
}
}

##### Share on other sites
Checking to see if two lines overlap in 3D will rarely return true for an exact overlap unless your artificially force the data to have exact collisions and even then floating point errors will mess with your results.

I would recommend defining a tolerance of how close two points can pass and be considered a hit. Then you simply do a sphere/sphere collision where the radius of the points is half the error tolerance.
http://www.gamasutra.com/view/feature/3015/pool_hall_lessons_fast_accurate_.php Edited by HappyCoder

##### Share on other sites

In order to find if these two particles will collide, follow these steps:

1. construct a ray for each particle using its begin and the end position (end position = begin position + velocity * time)
2. look for the point of intersection by solving the following algorithm: b0 + alpha * e0 = b1 + beta * e1 (where b0 and e0 are the begin and end vectors for the first point, b1 and e1 and the begin and end vectors for the second point, and alpha and beta are scalar values for the two points.
3. If alpha and beta exist, check to see if 0 <= alpha <= 1 && 0 <= beta <= 1

If all 3 conditions are met, alpha will equal beta, and the time of intersection is alpha * time = beta * time. However, this is not a trivial task, as the solution #2 consists of trying to solve a 3x2 matrix:

  b0 + A*v0 = b1 + B*v1
b0 - b1 = B*v1 - A*v0
B*v1 - A*v0 = b0 - b1

v1.x*B - v0.x*A = (b0-b1).x
v1.y*B - v0.y*A = (b0-b1).y
v1.z*B - v0.z*A = (b0-b1).z

| v1.x, -v0.x | * | A |  = | (b0-b1).x, (b0-b1).y, (b0-b1).z |
| v1.y, -v0.y |   | B |
| v1.z, -v0.z |

Solve for A and B (wait, how?)



It is however, possible to reduce the above solution into two dimensions. You can construct a plane by using one of the 4 points and the cross product of the two rays to find the projected distance and the plane's normal respectively. If all 4 points (b0, e0, b1 and e1) are NOT coplanar (a simple plane test against one point belonging to the ray NOT used to construct this plane), then the lines cant possibly intersect.

If they are coplanar, then you can create x and y axes such that you can project the 4 points onto these axes and perform a 2D line intersection test. This will reduce your 3x2 matrix down to a 2x2, which is easily invertible. Your x axis can be the vector e0-b0, and your y axis can be the vector plane.normal cross (e0-b0). If you have more questions, I can show you how to do this as well.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628682
• Total Posts
2984213

• 11
• 13
• 13
• 9
• 10