简体   繁体   中英

Draw a turning arc connecting 3 waypoints in Java

Let's say I am starting at point W, heading through point A to final point B.

从W到A和从A到B的直线

I would like to instead draw a turning arc that represents more realistic behavior, ideally constrained by speed and turning rate.

I've been looking for examples of this, such as what is described in this article , but am struggling a little bit extend the information I've read.

真实轨迹的转弯弧示例

Does anyone know of a sample implementation of this behavior or maybe a tutorial that has some code samples to work from? It seems like a super common thing to look for in game AI, but most of the descriptions I find are rather high level.

I've tagged this post with Java, because that is the solution domain in which I am working, but an example in another language may be sufficient as well.

You can either devise a mathematical formula (harder but more efficient) or think of something like this:

Imagine driving a car and that there is a turn (at point A) after a straight road (point W to point A). You will have a velocity vector pointing along the road just before you start turning. Once you begin the turn you will have a (increasing) velocity vector pointing along the turn (from point A to point B) and a (decreasing) velocity vector pointing along the road. You can calculate the resulting velocity vector by adding the two together. A sample algorithm would look like this:

while(turning){
    straight_vector.magnitude = straight_vector.magnitude - 1;
    turn_vector.magnitude = turn_vector.magnitude + 1;

    resulting_vector = straight_vector + turn_vector; //vector addition

    next_point.x = prev_point.x + resulting_vector.magnitude * Math.cos(resulting_vector.direction);
    next_point.y = prev_point.y + resulting_vector.magnitude * Math.sin(resulting_vector.direction);

    plot(next_point);

    prev_point = next_point;
}

You can use Vector3d class in Java to represent vectors.

Geometrically and from Fig. 2, I would travel west from point W until I hit the shaded region where you have vector R going south. Up until that point it is just a straight line. Then you need to draw an arc to take you from where the line ends to the (0, 0) point. You draw arcs using information here . You set up a complete JavaFX application using information here to draw lines and arcs and more. You have to figure out the coordinates to parameterize those methods with -- that it, it is math from here onwards.

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