RandyGaul / cute_headers

Collection of cross-platform one-file C/C++ libraries with no dependencies, primarily used for games
4.25k stars 264 forks source link

cute_c2.h: c2RaytoCircle only detects collision for the top half of the circle #261

Closed ebmaxwell closed 3 years ago

ebmaxwell commented 3 years ago

It seems the c2RaytoCircle function only detects collision for the top half of the circle. This is my line and circle:


line.p = c2V( 400, 300 );
line.d = c2Norm( c2V(100, 300) );
line.t = c2Len( c2V(100, 300) );
circle.r = 100;
circle.p = c2V(500, 500);

and I check the point of collision like this:

circle.p = c2V(mousPosition.x, mousePosition.y);
if( c2RaytoCircle(line, circle, &cast) )
{
    c2v collision_at = c2Impact( line, cast.t );
}

as you can see from the gif the lower half of the circle colliding with the line isn't detected:

GIF 6-16-2021 2-49-46 PM

RandyGaul commented 3 years ago

Hey there, that’s intended behavior. Most of the time only the first point is needed, so no time is wasted calculating the other. You can perform two ray casts if you really want two points, or use a different library/function to calculate both points.

RandyGaul commented 3 years ago

So by two raycasts I mean flip your ray backwards if you hit the circle to find the exit point

ebmaxwell commented 3 years ago

I got it to work nicely using two rays like you suggested. Thanks!