Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

giant

Finding where vectors intersect

This topic is 5737 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. I am trying to program realistic car physics for a go cart game that I am writing. I am following the car physics tutorial in the resources area of this site. When turning you need to find the circle created by the front wheels and back wheels. I have the right vector of the front wheels and that of the back wheels. When the front wheel is tryned to the right, these should intersect. How do I find out where they intersect, so that I can get the redius of this circle. Thanks Giant

Share this post


Link to post
Share on other sites
Advertisement

  
class Line
{
public:
// Line equation: L = m_M * t + m_B


Vector3 m_M; ///< Direction (length is always 1)

Vector3 m_B; ///< Location of a point on the line

};

int Intersection( Line const & a, Line const & b, float * pt, float * pu )
{
Vector3 const n = Cross( a.m_M, b.m_M );
Vector3 const ab = b.m_B - a.m_B;

// If the lines are parallel, then there are either 0 or infinite intersections ...


if ( Math::IsCloseToZero( n.Length2(), 2.*Math::DEFAULT_FLOAT_TOLERANCE ) )
{
float const d = Dot( ab, a.m_M );

if ( Math::IsRelativelyCloseTo( ab.Length2(), d * d, 2.*Math::DEFAULT_FLOAT_TOLERANCE ) )
{
return std::numeric_limits<int>::max(); // Co-linear, return big number

}
else
{
return 0; // No intersection, return 0

}
}

// ... they are not parallel but if the distance between them is > 0 ...


else if ( !Math::IsCloseToZero( Dot( n, ab ) ) )
{
return 0; // No intersection, return 0

}

// ... otherwise, they intersect at a single point ...


else
{
// Source: http://www.flipcode.com/geometry/gprimer2_issue02.shtml


// Use the minor with largest determinant to maintain precision and prevent divide-by-0


if ( fabs( n.m_Z ) > fabs( n.m_X ) && fabs( n.m_Z ) > fabs( n.m_Y ) )
{
*pt = ( ab.m_X * b.m_M.m_Y - ab.m_Y * b.m_M.m_X ) / n.m_Z;
*pu = - ( ab.m_X * a.m_M.m_Y - ab.m_Y * a.m_M.m_X ) / n.m_Z;
}
else if ( fabs( n.m_X ) > fabs( n.m_Y ) )
{
*pt = ( ab.m_Y * b.m_M.m_Z - ab.m_Z * b.m_M.m_Y ) / n.m_X;
*pu = - ( ab.m_Y * a.m_M.m_Z - ab.m_Z * a.m_M.m_Y ) / n.m_X;
}
else
{
*pt = ( ab.m_Z * b.m_M.m_X - ab.m_X * b.m_M.m_Z ) / n.m_Y;
*pu = - ( ab.m_Z * a.m_M.m_X - ab.m_X * a.m_M.m_Z ) / n.m_Y;
}

return 1; // 1 intersection, return 1

}
}


int Intersection( Line const & a, Line const & b, Point * pi )
{
float t, u;
int const n = Intersection( a, b, &t, &u );

if ( n == 1 )
{
*pi = a.m_M * t + a.m_B;
}

return n;
}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!