**0**

# Sweep test for circle vs line segment

Started by Feb 10 2008 12:07 PM

,
2 replies to this topic

###
#1
Members - Reputation: **128**

Posted 10 February 2008 - 12:07 PM

Hello!
I recently have read through Swept Ellipsoids article and findout there the both (math and code) for swept sphere and static plane tests.
But the problem is that this is infinite plane or something, and when I implement in in 2d with tries to make in work with segmented line, it works just awful.
So I'm up here to ask you about the math and code for perform
Swept circle vs Static segment and Swept circle vs Swept segment tests...
The second one just have translation vector... like if describe it as segment with two point in space as A,B and normal and then translate it through some displacement like nA = A+D nB = B+D... no any angular rotations and so on...
The example why I need that :
Fast growable surface which goes up, and fast flying player who trying to hit this surface by fastly falling, and going from the top...
So the point is to find out boolean results and also calculate (exactly contact point with normal and separation). How calculate this stuff using parameter
t -&rt [0,1] I know only for Static Segment vs Swept Circle, because has write Sweep Circle vs Sweep Circle test, but what suppose to be in Swept Segment vs Swept Circle, well I can't figure it out, my math level doesn't let me imagine exactly cases for that.
So I beg you guys to help me somehow to dealing with sjb.

###
#2
Members - Reputation: **2078**

Posted 10 February 2008 - 12:27 PM

Just a few comments to get you started...

First of all, intersection tests involving two moving objects can generally be expressed such that one of them is stationary (by subtracting the velocity of one object from the velocity of the other). As such, all you really need is a swept circle vs. static line segment test.

In turn, this problem can be expressed as a capsule raytracing test. The ray originates at the circle center, and its direction vector is equal to the circle's velocity vector. The capsule's medial segment is equal to the line segment, and has a radius equal to the circle's.

The ray vs. capsule test can be implemented in various different ways, but a relatively straightforward (if not maximally efficient) approach would be to intersect the ray with the oriented box and pair of circles that make up the capsule, and then take the least of the parametric values returned (if any) as the parametric hit point.

Once you have the hit point in parametric form (i.e. the time of collision), you can move the circle and the segment to the point where they are just touching. At this point you can compute both the contact point and normal (if needed) by computing the point on the line segment closest to the circle center.

I wasn't clear from your post whether you already have the swept circle vs. static line segment test written or not; if you do, you're almost done, but if not, you may need to implement the ray vs. capsule test described above.

(Note that most of these tests can be expressed in more than one way; for example, the swept circle vs. segment test can be approached in terms of a circle vs. hyperplane test and two circle vs. point tests. However you express it though, the computations you end up performing will be more or less equivalent.)

First of all, intersection tests involving two moving objects can generally be expressed such that one of them is stationary (by subtracting the velocity of one object from the velocity of the other). As such, all you really need is a swept circle vs. static line segment test.

In turn, this problem can be expressed as a capsule raytracing test. The ray originates at the circle center, and its direction vector is equal to the circle's velocity vector. The capsule's medial segment is equal to the line segment, and has a radius equal to the circle's.

The ray vs. capsule test can be implemented in various different ways, but a relatively straightforward (if not maximally efficient) approach would be to intersect the ray with the oriented box and pair of circles that make up the capsule, and then take the least of the parametric values returned (if any) as the parametric hit point.

Once you have the hit point in parametric form (i.e. the time of collision), you can move the circle and the segment to the point where they are just touching. At this point you can compute both the contact point and normal (if needed) by computing the point on the line segment closest to the circle center.

I wasn't clear from your post whether you already have the swept circle vs. static line segment test written or not; if you do, you're almost done, but if not, you may need to implement the ray vs. capsule test described above.

(Note that most of these tests can be expressed in more than one way; for example, the swept circle vs. segment test can be approached in terms of a circle vs. hyperplane test and two circle vs. point tests. However you express it though, the computations you end up performing will be more or less equivalent.)

###
#3
Members - Reputation: **128**

Posted 11 February 2008 - 10:40 AM

I think youre talking about not so efficiant way.

So what we got here is Plane vs Circle vs Circler's Line equation test.

So there we go, I read this article and even try to implement something,

take a look, here is the article and theory:

I'm not too smart in mathematics, and don't understand what exactly plane constant suppose to be, I mean if we talking about finite plane.

And here what I try:

So I don't take good results with this why, maybe you see some errors in implementation?

So what we got here is Plane vs Circle vs Circler's Line equation test.

So there we go, I read this article and even try to implement something,

take a look, here is the article and theory:

I'm not too smart in mathematics, and don't understand what exactly plane constant suppose to be, I mean if we talking about finite plane.

And here what I try:

inline static float PointDistToLineSeg( Vector P, Vector vSrc, Vector vEnd )

{

Vector v = vEnd - vSrc;

Vector w = P - vSrc;

float c1 = w.Dot(v);

if ( c1 <= 0 )

return w.Length();

float c2 = v.Dot(v);

if ( c2 <= c1 )

return (P - vEnd).Length();

float frac = 0.0f;

if ( fabsf( c2 ) < 1.0e-6f ){ frac = 0.0f; }

else { frac = c1 / c2; }

return (P - (vSrc + v * frac)).Length();

}

// swept circle vs swept line

bool SegmentCircleIntersect( const circle_t &circ, const lineseg_t &line, circ_int_t &int_info )

{

// substitute for moving line

Vector transition = circ.transition - line.transition; // souldn't use that!!!! not works!

// If the center of the sphere moves like: center = position + t * translation for t -> [0, 1]

// then the sphere intersects the plane if: -R <= distance plane to center <= R

float dist_norm = Dot(line.normal, transition); // something from plane equation

float dist_to_c = PointDistToLineSeg(circ.position, line.src, line.end); // NOTE: !SIGNED! distance to line seg? how?

if ( fabsf(dist_norm) < 1.0e-6f )

{

// The sphere is moving nearly parallel to the plane, check if the distance

// is smaller than the radius

if ( fabsf(dist_to_c) > circ.radius )

return false;

// Intersection on the entire range

int_info.entire_range = true;

int_info.fraction1 = 0.0f;

int_info.fraction2 = 1.0f;

}

else

{

// Determine interval of intersection

int_info.fraction1 = ( circ.radius - dist_to_c) / dist_norm;

int_info.fraction2 = (-circ.radius - dist_to_c) / dist_norm;

// Order the results

if (int_info.fraction1 > int_info.fraction2)

swap(int_info.fraction1, int_info.fraction2);

// Early out if no hit possible

if (int_info.fraction1 > 1.0f || int_info.fraction2 < 0.0f)

return false;

// Clamp it to the range [0, 1], the range of the swept circle

if (int_info.fraction1 < 0.0f) int_info.fraction1 = 0.0f;

if (int_info.fraction2 > 1.0f) int_info.fraction2 = 1.0f;

// contact points

Vector point1 = circ.position + transition * int_info.fraction1; // determine where exactly circle should stop

Vector point2 = circ.position + transition * int_info.fraction2; // determine where exactly circle should exit

int_info.numpts = 1; // determine whether or not second point should be used

int_info.points[0].position = point1;

// int_info.points[1] = point2;

int_info.points[0].separation = dist_to_c - circ.radius;

int_info.points[0].normal = line.normal;

}

return true;

}

So I don't take good results with this why, maybe you see some errors in implementation?