• ### What is your GameDev Story?

#### Archived

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

# "Fat-ray" collision detection

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

## Recommended Posts

I need to make a collision detection algorithm that does a ray (with a radius) to sphere collision. Here is my ray to sphere algorithm:

const bool Util::RaySphereTest( D3DXVECTOR3* vPofI, const D3DXVECTOR3* vSpherePos, const float fRadius,
const D3DXVECTOR3* vRayStartPos, const D3DXVECTOR3* vRayEndPos )
{
// Ray''s direction

D3DXVECTOR3 vRayNormal = *vRayEndPos - *vRayStartPos;
Normalize( &vRayNormal, &vRayNormal );

// Get distance of a vector from ray origin to sphere''s center pos projected on the ray

D3DXVECTOR3 vRaySphere = *vSpherePos - *vRayStartPos;
float dist = DotProd( &vRaySphere, &vRayNormal );

// Find distance from point of intersection to vector perpendicular to ray and goes through sphere''s

//	center (that''s the idea anyway)

float disc = (fRadius * fRadius) - (DotProd( &vRaySphere, &vRaySphere ) - (dist * dist) );
if( disc < 0.0f )
return false;	// No intersection

// They intersect

if( vPofI )
{
// Find point of intersection

float d = (float)sqrt( disc );
*vPofI = *vRayStartPos + ((dist - d) * vRayNormal);
}

return true;
}

How can I modify this to give the ray a radius?

##### Share on other sites
A friend of mine said that I might just be able to add the ray''s radius to the sphere''s radius. That seems to work... Is this a good way?

##### Share on other sites
Hmmm... it''s not quite what you want. Adding the ray radius to the sphere radius technically makes this an intersection test between a sphere and a capsule (the swept locus of your ray)whereas you want an intersection test between a sphere and a cylinder.

Also note that there won''t be a single intersection point in this case, but an infinite amount of intersection points along the intersection curve.

*HOWEVER*. As with most collision stuff, I''ve found that tests like the one you''ve concocted, that "kindof work", are the perfect solution if the side effects are something you can live with.

But if you want to do it proper, check these out:

http://www.magic-software.com/Intersection3D.html

##### Share on other sites
If the extremities of the ray are a concern, you can always check if there is a collision between the sphere and the disks at the end if the collision point is behind the end of the ray.

Cédric

##### Share on other sites
Adding the radius of the ray to the sphere and treating the ray as infinitely thin is not a fudge, but in fact an efficient way of performing the collision computation IF you don''t care about knowing the point/curve of collision. If you do care, you can find the curve with some calculus. The result will be an ellipse if the ray fully penetrates the sphere or half and ellipse if the ray only partially penetrates the sphere. In the first instance, the centre of the ellipse is given by the collision point of the axis of the cyclinder and in the second case, the centre of the ellipse lies at the point where the vector orthogonal to the ray (that points toward the sphere''s origin) pierces the sphere. I.e., at the point on the sphere that is tangent to the ray. You can work out the minor axis of the ellipse from the width of the portion of the cyclinder that penetrates the sphere. The major axis is more difficult to determine and depends on where the cylinder''s axis penetrates the sphere, if it does at all.

Cheers,

Timkin

##### Share on other sites
> treating the ray as infinitely thin is not a fudge

Fat Ray (for point X):
d = X - (O + tD)
dx^2 + dy^2 + dz^2 < r0^2
t >= 0
O = <0, 0, 0>
D = <0, 0, 1>
r0 = 1

Sphere (for point X):
d = X - O
dx^2 + dy^2 + dz^2 < r1^2
O = <0, 0, -0.5>
r1 = 1

Whether you want to call it a fudge or not it still gives an incorrect intersection test result if your ray is bounded (which, in the OP code, it is).

If you''re ray is not bounded then as you say, it''s perfect.

doh... sphere:

O = <0, 0, -1.5>

##### Share on other sites
> The result will be an ellipse if the ray fully penetrates the sphere or half and ellipse if the ray only partially penetrates the sphere.

Any evidence to support this claim? I was expecting the result to be a curve of degree 4.

##### Share on other sites
Neither. See MathWorld and Viviani''s curve

Cédric

##### Share on other sites
quote:

The result will be an ellipse if the ray fully penetrates the sphere or half and ellipse if the ray only partially penetrates the sphere.

Wrong wrong wrong!

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634091
• Total Posts
3015463
×