簡體   English   中英

如何計算沿路徑法線的兩個點,僅給出該路徑的兩個點?

[英]How can I calculate two points along the normal of a path given only two points from that path?

對不起這個標題,這真的不是一個好標題。

所以我為我的游戲創建了一條積分路徑。 這條路徑是“隨機”生成的,所以我永遠不知道它會是什么樣子,我只知道它永遠不會交叉。

我想要做的是沿該路徑取任意點,並計算 2 個點,該點距該線在任一側的法向量方向上正好 10 個單位。 一個例子是我在兩條車道道路的中心有車道標記的點。 我想計算車道標記上任何給定點的兩個路緣石的位置。

我試圖只計算正常斜率,我認為我得到了正確的結果,但我不知道我怎么能說“這條線上 10 個單位以外的點”。

如果您需要一些額外的信息,我很樂意提供。

有人問代碼:

//These are set elsewhere
float x1;
float y1;
float x2;
float y2;
float Distance = 10;

基本上,這就是我想要做的,但在代碼中:

((x1+x2)/2, (y1+y2)/2) +- Distance * (-(y2-y1), (x2-x1)) / sqrt((x2-x1)^2+(y2-y1)^2)

我不能相信這一點,因為我在其他地方找到了它,但這是我試圖解決的方程的實現。 我將發布以防其他人將來偶然發現此問題。

請注意:這是非常優化的,變量名稱有點不准確(特別是左/右),但這是我將在更大項目中實施的概念證明。

            //Calculate left
            float left_x1 = current.x;
            float left_y1 = current.y;
            float left_x2 = previous.x;
            float left_y2 = previous.y;
            float D = 30;

            float left_dx = left_x1-left_x2;
            float left_dy = left_y1-left_y2;
            float left_dist = (float) Math.sqrt(left_dx*left_dx + left_dy*left_dy);
            left_dx /= left_dist;
            left_dy /= left_dist;
            float left_x3 = left_x1 + D * left_dy;
            float left_y3 = left_y1 - D * left_dx;
            float left_x4 = left_x1 - D * left_dy;
            float left_y4 = left_y1 + D * left_dx;

            //Calculate right
            float right_x1 = previous.x;
            float right_y1 = previous.y;
            float right_x2 = old.x;
            float right_y2 = old.y;

            float right_dx = right_x1-right_x2;
            float right_dy = right_y1-right_y2;
            float dist = (float) Math.sqrt(right_dx*right_dx + right_dy*right_dy);
            right_dx /= dist;
            right_dy /= dist;
            float right_x3 = right_x1 + D * right_dy;
            float right_y3 = right_y1 - D * right_dx;
            float right_x4 = right_x1 - D * right_dy;
            float right_y4 = right_y1 + D * right_dx;

            //Draw bounds
            shapeRenderer.line(right_x3, right_y3, left_x3, left_y3);
            shapeRenderer.line(right_x4, right_y4, left_x4, left_y4);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM