Image from imdb.com

*This is the view from the NPC. Those would all be mine.*

Parking in a specified direction involves two steps: the approach and the landing.

**Parking, the Approach**

We could just fling our vehicle directly at a destination and let it line itself up when it gets there. However since there are 360 angles it could arrive at and 360 angles it could be destined for, it may spend quite some time turning around. So ideally we want it as close as possible to its final rotation when it begins the landing.

To do that, we're going to inject a waypoint into the array of Path Points returned by the NavigationPath object. We want to calculate a waypoint that is 90 to 180 degrees behind the destination. If the vehicle is in front of the destination, it'll aim for 90 degrees to the side. As the vehicle drives behind the destination, it will curve inward to 180 degrees directly behind the destination:

*The similarity to aircraft landing is not lost on me.*

maxDistance * SIN( ABS( DeltaAngleFromNextNavToFinalRotation ) * 0.5 )

I chose 1500 for maxDistance, as longer distances makes the vehicle appear to veer way off course and shorter distances didn't give it enough length to line up behind it.

The direction from the destination to insert the waypoint (the orange dotted line above) is:

COS( DeltaAngleFromNextNavToFinalRotation * 0.333 ) * SIGN( DeltaAngleFromNextNavToFinalRotation ) * -180

The result is a rotator from 90 to 180 degrees, to be added to the final rotation. The numbers work out like this:

Angle | Rotator | Length |

-180 | 90.1 | 1 |

-150 | 115.8 | 0.96 |

-120 | 137.9 | 0.86 |

-90 | 155.9 | 0.70 |

-60 | 169.1 | 0.5 |

-30 | 177.2 | 0.25 |

0 | 0 | 0 |

30 | -177.2 | 0.25 |

60 | -169.1 | 0.5 |

90 | -155.9 | 0.70 |

120 | -137.9 | 0.86 |

150 | -115.8 | 0.96 |

180 | -90.1 | 1 |

**Parking, the Landing**

Once the vehicle is likely behind the destination, probably facing nearly the correct direction, and within CloseEnough(), we can start actually parking.

The primary goal is to align the vehicle along the rotation plane that passes through the destination. That's the hard part. Then it's a simple matter to forward or reverse to the destination.

We need to calculate a target to steer toward again, but unlike the Approach target, the Landing target will always be in a line along the rotation plane. The farther to the side the vehicle is, the straighter it will aim directly for the plane. As it gets closer, it will steer to drive alongside it:

*I would have been more interested in trigonometry if the problems were like this instead of the height of trees.*

We need three values: how far the vehicle is along the plane, its distance away from the plane and whether the vehicle is facing toward or away from it.

We know how far the vehicle is along the plane (the yellow line above) using:

COS( DeltaAngleFromFinalRotationToVehicle ) * DistanceFromDestinationToVehicle

And its distance away from the plane (the orange dotted line above) with:

SIN( DeltaAngleFromFinalRotationToVehicle ) * DistanceFromDestinationToVehicle

We can tell if the vehicle is aimed toward or away from the plane by:

SIGN( DeltaBetweenFinalRotationAndVehiclesRotation ) *

SIGN( DOTPRODUCT( DestinationRightVector, VehicleRightVector ) )

SIGN( DOTPRODUCT( DestinationRightVector, VehicleRightVector ) )

That results in a 1 or -1 describing whether the vehicle is aiming left or right of the destination and which side of the plane it's on. That becomes the vehicle's throttle.

Finally, we can calculate an exponentially increasing length away from the vehicle's position along the plane:

ABS( ( 1 - ( CloseEnough / ABS( DistanceFromPlane ) ) ) * DistanceAlongPlane ) * Throttle + DistanceAlongPlane

The blueprints:

*You said THREE values?!*

Will it fit like a glove? Probably not. Could wasteland warriors use it to setup an ambush? Absolutely:

The logic still isn't too complicated yet:

Next time, we'll spawn multiple vehicles and handle group selection.

## No comments:

## Post a Comment