我發現這2個神奇的教程如何檢測和處理圓與線段碰撞。角上的圓線段碰撞
我實現了它在C#中沒有任何問題。 我喜歡這個解決方案,因爲它是一個乾淨而簡單的解決方案。
但是沒有解釋如何處理線端/拐角上的碰撞。
添加此功能的最佳方式是什麼?
左側說明了我的函數如何表現如此。 在右邊,我想表現得如何。 案件和工作很好,正是我想要的。 但是,如果圓圈不會發生碰撞。 我必須執行類似於右側的操作。 但我不知道這可以如何工作。
到目前爲止,我有這樣的:
// A line is defined by two points.
// A circle is define by a point and a radius.
public static bool CircleVsLine(Circle circle, Vector2d circleDirection, ref Vector2d circleSolved, Line line)
{
// Circle position before movement.
Vector2d circle0 = circle.Position;
// Circle position after movement.
Vector2d circle1 = circle.Position + circleDirection;
Vector2d lineDirection = line.Position1 - line.Position0;
Vector2d lineNormal = new Vector2d(lineDirection.Y, -lineDirection.X).Normalized();
Vector2d circle0ToLine0Direction = line.Position0 - circle0;
Vector2d circle1ToLine0Direction = line.Position0 - circle1;
// Calculate distance to line before movement.
double circle0DistanceToLine = Vector2d.Dot(lineNormal, circle0ToLine0Direction);
// Calculate distance to line after movement.
double circle1DistanceToLine = Vector2d.Dot(lineNormal, circle1ToLine0Direction);
// The time when the circle radius equals the distance to the line.
double t = (circle.Radius - circle0DistanceToLine)/(circle1DistanceToLine - circle0DistanceToLine);
// If true collision on endless line occured.
if (t >= 0 && t <= 1)
{
// EPSILON is a very small double number to prevent bugs caused by rounding errors.
circleSolved = circle0 + circleDirection * t - lineNormal * EPSILON;
Vector2d line0ToPlayerSolved = circleSolved - line.Position0;
Vector2d line1ToPlayerSolved = circleSolved - line.Position1;
// If true collision happened on the line sgment.
if (Vector2d.Dot(lineDirection, line0ToPlayerSolved) >= 0 && Vector2d.Dot(lineDirection, line1ToPlayerSolved) < 0)
{
return true;
}
}
// No collision so circle can be moved.
circleSolved = circle1;
return false;
}
將圓心投影到線上以確定它是否會與端點衝突並不總是有效。你也必須考慮速度矢量。如果我在(0,0)處有一個半徑爲1,速度矢量(6,4)的圓以及由點(1,2)和(4,2)定義的線段,圓會在端點。如果你畫它,你會明白我的意思。 – dylan
我已經實現了一個算法,可以在一個時間步驟上完成這種類型的碰撞。我稱之爲Line Segment vs Circle Swept Collision Detection and Resolution。你想看看嗎? – dylan
我很清楚移動物體的困難。投影是在圓圈接觸到之前計算的終點的位置完成的。該投影僅用於在接觸結束點之前檢查圓與該線是否相交。除非我錯過了一些東西,否則這應該是正確的。隨意發佈你的算法作爲另一個答案。 –