[Dev] Making Vehicle AI in Unreal Engine 4

[Dev] Making Vehicle Movement AI in Unreal Engine 4

Project Start date: 24 September 2017
Engine: Unreal Engine 4


Overview
This blog is mainly focused on movement AI of wheeled actors.The method used is my own, so it might not be very efficient or best. During development of my game UltimateDefence, I wanted to add enemies that had movement similar to vehicles. Typical biped character class's movement is not set up to be physically-based, while,  on the contrary, wheeled based actors need to be simulated. UE4 wheeled actors are highly detailed physics simulation with all real-world factors like Drag, Torque, Lat and Long Forces etc. Also note that not every kind of game, that has wheeled based actors need to have physically based movements.Some games animate their wheeled actors to look and feel real and use custom physics and not high detailed physics simulation. Doing this gives them more creative flexibility on how their wheeled actors act and react to the environment and game mechanics.I never thought of this method when I was building wheeled enemies for my game. Using animated actors and custom physics is great for improving the performance of your game as PhysX based wheeled actor need to calculate a lot of information which might be irrelevant to your game.


Concept
The concept which I used for navigating my vehicle is similar to line follower bot. Line follower bot uses the line to follow. My idea was to use an array of 3D coordinates of the path I want my vehicle to follow. I would like to call this method Passive pathfinding. This method requires you to specify the exact route you want your vehicle to follow. Active Pathfinding case would be where we only specify the endpoint where we want our vehicle to move and it finds the best viable path on its own.(Similar to AI Move To Node in UE4).The passive method is suitable for a game similar to tower defense or racing where the path is usually the same and already decided.
The active method can be used in open world game scenario where paths may get obstructed or may be dynamic and constantly changing.

Driving and steering
For simplicity let us keep acceleration and max speed (throttle) of our vehicle limited. So after deciding the path all I had to do was keep the vehicle's forward vector always pointed in direction of the path to be followed. The angle between two vectors, Forward vector and path vector, is what I used as a factor to calculate the amount of steering that we need to apply, to keep the vehicle on track.

As steering amount is clamped in the range -1 to 1 in UE4 we need to interpolate the angle properly in the range -1 to 1, for that I made a steering curve which I use to interpolate Angle. Changing Steering curve will yield a different kind of handling results and how your vehicle reacts on turns.
Example of a good curve is given below.

Acceleration and Braking (Making Curves/Turns)
Above concept is self-sufficient for you to get your vehicle rolling. However, you might soon realize that your vehicle easily losses it's handling at curves or turns. You can overcome this problem by reducing the top speed of your vehicle or else by doing more mathematical interpolation and apply it to your throttle input. This will help your vehicle AI decide when to brake and when to accelerate.
Now the method which I used for my vehicle AI involves using trial and error method to find out the perfect constants for my curve ( acc = ((a*c)/2)*tan(45deg*((MaxSpeed*c-CurrentSpeed)/MaxSpeed)).

Finding formula of curve requires studying lots of data, trails, making sheets and curve fittings.
Now you need to study the curve to find out what could be a and c. For my vehicle a=0.7 and c was clamp(0,1)(distance /500).
Usually, the relationship is like a= max throttle that you might want
c=clamp(0,1)(Distance/ braking distance in cm) , braking distance for my vehicle was around 5m (distance from 60 Kmph to 0 ).

Using all this data , the result I got is shown in below video

BASIC LINE FOLLOWER (PASSIVE)
SELF DRIVING VEHICLE AI (ACTIVE)

Comments

  1. The link is provided by the way (Ultimate Defense).

    Thanks for your article!

    ReplyDelete

Post a Comment

Popular Posts