简体   繁体   中英

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. 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".

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM