Jump to content
• Advertisement

# Solve path around a given point and radius

This topic is 2104 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

The attached image helps to describe my problem.

Given a direction vector(V1), location to avoid along with the radius, and a destination point, how could I calculate the blue travel path?

I know that given the angle in radians of a circle you can find the vector location at that angle on the circle, but I am not sure how I would implement this equation to solve this.

Thank you

#### Share this post

##### Share on other sites
Advertisement

If you are having a point particle:

vector velocity = normalize(destination - start) * speed;

vector t = p - c; //vector from point to circle center
float d = dot(t, t); // squared length of t
if( d < r*r)  // check if circle radius^2 > d
{
float root = sqrt(d);
vector overlap = (t / root) * (r - root);
velocity -= overlap; // offset the point
}



edit:small correction

Edited by Doublefris

#### Share this post

##### Share on other sites

So, it looks like you'll end up with sort of an inverse dubins car, where instead of an arc, line, arc, you end up with a line, arc, line.

So, first thing I'd do is a Line vs Circle intersection test, and get the two points of intersection with the circle.  (if you only have 1, then you are in the circle, and no points, you just head down the line.)

From there, you just need the start angle (atan2 is your friend) and the angle between that and the end point.  I can dig up the exact code later if you need it.

EDIT:  Follow up

angleStart = atan2(ptStart.y - center.y, ptStart.x - center.x);
deltaAngle = atan2(ptEnd.y - center.y, ptEnd.x - center.x);
if (deltaAngle < 0.0f && (!clockwise))
{
deltaAngle += 2.0f * PI;
}
else if (deltaAngle > 0.0f && (clockwise))
{
deltaAngle -= 2.0f * PI;
}

endTheta = startTheta + deltaAngle



You might be able to figure out ahead of time whether you'd be going clockwise or not, worse comes to worse, check both angles, take the smaller arc length

Edited by ferrous

#### Share this post

##### Share on other sites

Here's a fairly decent way to solve this problem. I'm assuming this will be a "line,circle,line" problem, but you can alter this for different cases. I would put the line into parametric form and the circle into implicit form:

$C(t) = (x-a)^2 + (y-b)^2 - r^2 = 0; L(x(t),y(t)) = [x_0 + ct, y_0+dt]$

Here, $$(a,b)$$ is the center of the circle, $$r$$ is the radius, and $$(x_0,y_0)$$ is the start point of the path and your direction vector $$V_1 = (c,d)$$. By substituting the parametric line formulas into the implicit circle formula, we can get a polynomial whose roots are the intersection points in the line parameter space:

\begin{aligned} (x_0+ct-a)^2 + (y_0+dt-b)^2 -r^2 &= 0 \\ (c^2+d^2)t^2+2[c(x_0-a)+(y_0-b)]t+[(x_0-a)^2+(y_0-b)^2-r^2] &= 0 \\ At^2+Bt+c &= 0 \\ \end{aligned}

The roots can be solved for quickly using the quadratic equation:

$t = \frac{-B\pm \sqrt{B^2-4AC}}{2A}$

If the discriminant $$B^2 - 4AC < 0$$, then you have no intersections. If  $$B^2 - 4AC = 0$$, you've got 1 intersection. If  $$B^2 - 4AC > 0$$, you've got 2 intersections, which is really the only case you have to worry about to avoid the object since the path can stay linear for any other cases. You can then evaluate the parametric line at the roots of the polynomial to get the intersection points in (x,y) form. You can get the angles of the points on the circle using $$\theta = \text{atan2}(y-b, x-a)$$. If the problem is as shown in the OP's picture (where the starting path point isn't inside the circle), you also know which point to start from and which point to end with because the point with the lesser parameter value t is the point closest to $$(x_0,y_0)$$.

Deciding which path around the circle is shorter is the last issue to resolve. We could calculate arc length, but we can figure out which way to go by using the implicit form of the line and figuring out which side of the line the circle's center is. The implicit line formula is given by $$\mathcal{A}x+\mathcal{B}y+\mathcal{C}=0$$. The coefficients for the implicit line are simple to calculate from the parametric form: $$\mathcal{A} = -d, \,\mathcal{B} = c, \,\mathcal{C} = dx_0-cy_0$$. The equation then becomes $$D = d(x_0-a)+c(b-y_0)$$. If D < 0, then the circle center is on the right side of the line (looking in the direction of the line). That means the clockwise path around the circle is the shortest path. If D > 0, then the circle center is on the left side of the line, which means the counterclockwise path is the shortest. If D = 0, then the circle center lies on the line and each path is the same length.

You can step with constant velocity along the path by using the parametric form of the line and along the circle by figuring out your angle step via $$ds = r d\theta$$. As I see it, this is fairly elegant because there's nothing more than additions, multiplications, atan2, and a square root function, making it a fairly fast method.

Hope that helps!

Edited by cadjunkie

#### Share this post

##### Share on other sites

The shortest path would have the two straight line portions tangent to the circumference rather than aligned with each other. As a bonus, continuity would be G1 rather than G0.

#### Share this post

##### Share on other sites

The shortest path would have the two straight line portions tangent to the circumference rather than aligned with each other. As a bonus, continuity would be G1 rather than G0.

How would one go about calculating that?  Find the two tangent lines to the circle for each endpoint, turn them into rays and intersect them with each other?

Edited by ferrous

#### Share this post

##### Share on other sites

The shortest path would have the two straight line portions tangent to the circumference rather than aligned with each other. As a bonus, continuity would be G1 rather than G0.

How would one go about calculating that?  Find the two tangent lines to the circle for each endpoint, turn them into rays and intersect them with each other?

Almost. You would intersect the two tangents with the circle, to find the tangency points where the arc portion of the path begins and ends.

A concise explanation with formulas: http://mathworld.wolfram.com/CircleTangentLine.html

#### Share this post

##### Share on other sites

• Advertisement
• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• Advertisement

• ### Popular Now

• 11
• 15
• 21
• 26
• 11
• Advertisement
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!