I need to compute the intersection point of two 3D vectors.

EDIT:

Found the answer with a board search.

This is a discussion on *Vector problem* within the **Game Programming** forums, part of the General Programming Boards category; I need to compute the intersection point of two 3D vectors.
EDIT:
Found the answer with a board search....

- 01-13-2009 #1
## Vector problem

I need to compute the intersection point of two 3D vectors.

EDIT:

Found the answer with a board search.Last edited by VirtualAce; 01-13-2009 at 11:09 PM.

- 01-13-2009 #2
I suppose the question has to be asked, what do you think "infinite 3D vectors" means, if anything? Do you want the intersection point of two lines, or what?

- 01-13-2009 #3
After doing a board search I found that I answered this question for dwks sometime back. Now I have to go back into my code that had it to see what I did.

- 01-15-2009 #4
I think what tabstop means is that the intersection point of two vectors is not a meaningful question, since a vector is a direction and a magnitude, but has no position. The intersection point of two lines (possibly represented by a vector and a starting point) is probably more what you're after.

- 01-15-2009 #5
No it's the intersection point of two vectors. However the vectors do have an origin which might have started the confusion but they are still vectors in 3 space. They could be thought of as lines but I rarely use the term lines anymore. Whether they are 2D or 3D I usually refer to them as vectors since this makes thinking about the various operations that can be performed on them and their geometric interpretations much clearer.

X = cross product

. = dot product

mag = magnitude

v1 = p1 + t1 * d1

v2 = p2 + t2 * d2

t1 = ((p2 - p1) X d2) . (d1 X d2) / mag((d1 X d2) * (d1 X d2))

t2 = ((p2 - p1) X d1) . (d1 X d2) / mag((d1 X d2) * (d1 X d2))

- p1(t1) and p2(t2) are the intersection point or the point closest to the intersection if one exists
- If the vectors are skew (lie in different planes) then the closest point of intersection is p1(t1) and p2(t2). There should be a theoretical distance between these by which to determine if the vectors are 'close enough' to be considered crossing.
- if (d1 X d2) == (0.0f,0.0f,0.0f) then the vectors are parallel or coincident and they do not cross.

So the solution to my problem as to where the vectors cross is:

Code:D3DXVECTOR3 vecLeftFire = GetLook() * GetRight() + (convergence_offset_vector); D3DXVECTOR3 vecRightFire = GetLook() * GetRight() + (-convergence_offset_vector); D3DXVECTOR3 toVecRightFire = vecRightFire - vecLeftFire; D3DXVECTOR3 vecCrossDirections; D3DXVec3Cross(&vecCrossDirections,&vecLeftFire,&vecRightFire); D3DXVECTOR3 vecCrossWithRightFire; D3DXVec3Cross(&vecCrossWithRightFire,&toVecRightFire,&vecRightFire); D3DXVECTOR3 vecCrossWithLeftFire; D3DXVec3Cross(&vecCrossWithLeftFire,&toVecRightFire,&vecLeftFire); D3DXVECTOR3 vecCrossDirectionsSqrd = vecCrossDirections * vecCrossDirections; float t1 = D3DXVec3Dot(&vecCrossWithRightFire,&vecCrossDirections) / vecCrossDirectionsSqrd; float t2 = D3DXVec3Dot(&vecCrossWithLeftFire,&vecCrossDirections) / vecCrossDirectionsSqrd; D3DXVECTOR3 vecLeftIntersect = GetLaserPos(LEFT_WING) + (t1 * vecLeftFire); D3DXVECTOR3 vecRightIntersect = GetLaserPos(RIGHT_WING) + (t2 * vecRightFire); D3DXVECTOR3 toRightIntersect = vecRightIntersect - vecLeftIntersect; float length = D3DXVec3Length(&toRightIntersect); if (length <= <some_fudge_distance>) { D3DXVECTOR3 vecNormToRightIntersect; D3DXVec3Normalize(&vecNormToRightIntersect,&vecToRightIntersect); D3DXVECTOR3 vecIntersectionPoint = vecLeftIntersect + ((length * 0.5f) * vecNormToRightInstersect); } ...

Last edited by VirtualAce; 01-15-2009 at 01:31 PM.

- 01-15-2009 #6
Dang, that is one ugly API. I'd be tempted to wrap that just to make it readable.

- 01-15-2009 #7
Technically I think the functions return a D3DXVECTOR3 so they can be used as rvalues but I didn't use them this way in my example.

But all math operations are overloaded for D3DXVECTOR2, 3, and 4. However they did not choose to overload any operators for normalize, dot, or cross. API's that have tried this are usually more confusing than those who don't. There aren't any operators in C++ that clearly denote normalize, dot product, or cross product without breaking the standard use of the operator. As such these don't lend themselves well to operators.

But the following are overloaded for the type: - + * / == =.

Also in my example some of that could be removed via member variables and/or optimization but I chose not to do anything with it for clarity. I left out the magnitude in the divisor since my directional vectors are unit vectors. The example formula that was given works for non unit vectors. Almost all of my vectors in the game are normalized.Last edited by VirtualAce; 01-15-2009 at 02:05 PM.

- Exactly how to get started with C++ (or C) today
- C Tutorial
- C++ Tutorial
- 5 ways you can learn to program faster
- The 5 Most Common Problems New Programmers Face
- How to set up a compiler
- 8 Common programming Mistakes
- What is C++11?
- Creating a game, from start to finish

- How to create a shared library on Linux with GCC - December 30, 2011
- Enum classes and nullptr in C++11 - November 27, 2011
- Learn about The Hash Table - November 20, 2011
- Rvalue References and Move Semantics in C++11 - November 13, 2011
- C and C++ for Java Programmers - November 5, 2011
- A Gentle Introduction to C++ IO Streams - October 10, 2011