Sign in to follow this  

Sweep Sphere vs Vertex intersection

This topic is 3311 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'm wondering, have found algorithm of intersection sphere with ray without solving quadratic equation.

	// For this algorithm, find a point on the ray  which is closest to the sphere origin
	// Do this by making a plane passing through the sphere origin
	// whose normal is parallel to the ray. Intersect that plane with the ray.
	// Plane: N dot P = I, N = D (ray direction), I = C dot N = C dot D
	// Ray: P = O + D * t
	// D dot ( O + D * t ) = C dot D
	// D dot O + D dot D * t = C dot D
	// t = (C - O) dot D / D dot D
	// Clamp t to (0,1)
	// Find distance of the point on the ray to the sphere center.

	// a = DotProduct( vecRayDelta, vecRayDelta );
	// b = 2 * DotProduct( vecRayOrigin - vecCenter, vecRayDelta )
	// c = DotProduct(vecRayOrigin - vecCenter, vecRayOrigin - vecCenter) - flRadius * flRadius;
	inline bool RaySphereIntersectionTime( Vector vecRayOrigin, Vector vecRayDelta, Vector& vecCenter, float radius, float &tray  )
	{
		Vector vecRayToSphere;
		vecRayToSphere = vecCenter - vecRayOrigin;
		float flNumerator = Dot( vecRayToSphere, vecRayDelta );

		float t;
		if (flNumerator <= 0.0f)
		{
			t = 0.0f;
		}
		else
		{
			float flDenominator = Dot( vecRayDelta, vecRayDelta );
			if ( flNumerator > flDenominator )
				t = 1.0f;
			else
				t = flNumerator / flDenominator;
		}

		Vector vecClosestPoint = vecRayOrigin + t * vecRayDelta;
		
		tray = t;

		return ( vecClosestPoint.SqrDistTo( vecCenter ) <= radius * radius );
	}



So now I'm wondering, is this is possible to use that for vertex vs sweep sphere? I've done simple line segment vs sweep sphere test which founding proper intersection point and collision time without solving any square or any other equation, just by getting dist to plane and look if projected point is in segment range. Or look by negative dist how far we penetrate segment, but for testing vertices of that segment, I also need find such time of collision on sphere velocity, so and there I forced to solve quadratic equation. But if this algorithm works just fine, so It's possible to adopt it for vertex spehre test. I tried to use it as I use for quadratic like :

if ( RaySphereIntersectionTime( circ.position, circ.transition, v[i], circ.radius, tcoll ) )
{
 ...
}


And this even give me some results, but seems like it's works as like my segment is infinity, continues goes on with start of that vertice. So because I'm not too smart in equations, can someone tell me how to expand this algorithm properly ?

Share this post


Link to post
Share on other sites

This topic is 3311 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this