• Advertisement
Sign in to follow this  

Sweep Sphere vs Vertex intersection

This topic is 3381 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;
			float flDenominator = Dot( vecRayDelta, vecRayDelta );
			if ( flNumerator > flDenominator )
				t = 1.0f;
				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, 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
Sign in to follow this  

  • Advertisement