Vector problem

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....

  1. #1
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584

    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.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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?

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    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.

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    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.

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    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); 
    }
    ...
    Now all that is left to do is to pass in vecIntersectionPoint to D3DXVec3Project() to get the screen coordinates and I should be able to place a reticle at the point where the lasers will meet.
    Last edited by VirtualAce; 01-15-2009 at 01:31 PM.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Dang, that is one ugly API. I'd be tempted to wrap that just to make it readable.

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    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.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21