简体   繁体   English

如何计算沿路径法线的两个点,仅给出该路径的两个点?

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

Sorry about the title, It's really not a good one.对不起这个标题,这真的不是一个好标题。

So I have created a path of points for my game.所以我为我的游戏创建了一条积分路径。 This path is "randomly" generated so I never know what it's going to look like all I know is it will never cross itself.这条路径是“随机”生成的,所以我永远不知道它会是什么样子,我只知道它永远不会交叉。

What I am trying to do is take any point along that path and calculate 2 points exactly 10 units away from the line in the direction of the normal vectors on either side.我想要做的是沿该路径取任意点,并计算 2 个点,该点距该线在任一侧的法向量方向上正好 10 个单位。 An example would be I have the points of the lane markers in the center of a two lane road.一个例子是我在两条车道道路的中心有车道标记的点。 I want to calculate the location of both curbs for any given point on the lane markers.我想计算车道标记上任何给定点的两个路缘石的位置。

I've attempted to just calculate the normal slope which I think I'm getting the correct result for that, but I don't know how I can say "a point 10 units away on this line".我试图只计算正常斜率,我认为我得到了正确的结果,但我不知道我怎么能说“这条线上 10 个单位以外的点”。

If you need some extra info I can gladly provide it.如果您需要一些额外的信息,我很乐意提供。

Someone asked for code:有人问代码:

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

Basically, this is what I want to do, but in code:基本上,这就是我想要做的,但在代码中:

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

I can't take credit for this because I found it somewhere else, but this is the implementation of the equation I was attempting to solve.我不能相信这一点,因为我在其他地方找到了它,但这是我试图解决的方程的实现。 I'm going to post in case others stumble across this in the future.我将发布以防其他人将来偶然发现此问题。

Please note: this is horribly optimized and the variable names are a bit inaccurate (particularly left/right), but it was a proof of concept that I will be implementing into a larger project.请注意:这是非常优化的,变量名称有点不准确(特别是左/右),但这是我将在更大项目中实施的概念证明。

            //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