The title says it all. My research has turned up nothing, but I’ve probably missed something really obvious as usual. I suppose I could create a plane that pretends to be the vector I want it to be and just see if a raycast intersects that, but that solution sounds like it would get messy quickly.
I think you’ll have to define the problem more clearly. Testing for intersection between two vectors isn’t terribly useful, so I’m assuming you actually mean something else.
If you’re wanting to test for intersection between two linear components (i.e. lines, rays, or line segments), keep in mind that linear components rarely intersect exactly when using a floating-point numerical representation. There are ways to work around that, but it would help to know what exactly you’re wanting to do. What problem is it that you’re trying to solve?
If it’s not clear why, a vector in 3D is similar to a slope in 2D, and you’ve never ask if two slopes intercepted. ![]()
A vector provides a magnitude and a direction. You also need a point (x,y,z) to define a line.
This is the method to find the closer point (common normal) of 2 lines or vectors in 3D space (red lines). So that, if the distance of the 2 lines (green line) is equal or very closed to zero, that is means they are intersected.
function FixedUpdate()
{
var A = Vector3(0,0,-1);
var B = Vector3(-1,1,0);
var a = Vector3(0,0.5,1);
var b = Vector3(1,0,0);
var n = cross(a,b);
var u = cross(n, A-B)/dot(n,n);
var AA = A - a *dot(b,u);
var BB = B - b *dot(a,u);
Debug.DrawRay (A, a*2, Color.red);
Debug.DrawRay (B, b*2, Color.red);
Debug.DrawLine(AA, BB, Color.green);
}
private var cross = Vector3().Cross;
private var dot = Vector3().Dot;
What you’ve shown are not vectors. (Strictly speaking they’re not lines either, but rather line segments.)
That said, the method you’ve described is indeed the recommended method for determining intersection (or near intersection) between linear components in 3D.
Yeah… In fact, each red line (for example A,a) are represent 2 vectors, one position vector (A) and one direction vector (a). Since, I am not the person to create that equation. So that, I don’t have knowledge to speak too much in detail. ![]()
http://en.wikipedia.org/wiki/Position_(vector)
http://en.wikipedia.org/wiki/Direction_vector
Perhaps too late to the party:
/// <summary>
/// Calculates the intersection of two given lines
/// </summary>
/// <param name="intersection">returned intersection</param>
/// <param name="linePoint1">start location of the line 1</param>
/// <param name="lineDirection1">direction of line 1</param>
/// <param name="linePoint2">start location of the line 2</param>
/// <param name="lineDirection2">direction of line2</param>
/// <returns>true: lines intersect, false: lines do not intersect</returns>
public static bool LineLineIntersection(out Vector3 intersection,
Vector3 linePoint1, Vector3 lineDirection1,
Vector3 linePoint2, Vector3 lineDirection2)
{
Vector3 lineVec3 = linePoint2 - linePoint1;
Vector3 crossVec1and2 = Vector3.Cross(lineDirection1, lineDirection2);
Vector3 crossVec3and2 = Vector3.Cross(lineVec3, lineDirection2);
float planarFactor = Vector3.Dot(lineVec3, crossVec1and2);
//is coplanar, and not parallel
if (Mathf.Abs(planarFactor) < 0.0001f
&& crossVec1and2.sqrMagnitude > 0.0001f)
{
float s = Vector3.Dot(crossVec3and2, crossVec1and2) / crossVec1and2.sqrMagnitude;
intersection = linePoint1 + (lineDirection1 * s);
return true;
}
else
{
intersection = Vector3.zero;
return false;
}
}