Simple Vehicle Ai

UNREAL ENGINE

Description

This is a component, which controls vehicle movement by providing throttle, brake and steering input values to the movement component. By default, input provided to the ChaosWheeledVehicleMovementComponent, but you can easily change it to whatever you want. To change movement component – you need to change variable type in "BPC_VehicleAI", and then update all occurrences of this variable.

The component provides the following mechanics: 

How to start

Step 1:

Step 2:

Parametres

Default > 

Draw Debug - Debug Boxes? lines? strings

Vehicle Parameters >

Auto Size Check - True - try to get vehicle bounds via interface or get skeletal mesh bounds False - Use vehicle dimension variable as is

Vehicle Dimension - Vehicle body bounds (not incl clearance)

Append Vehicle Dimension - How much we append vehicle bounds

Vehicle Clearance - Distance from surface to the bottom of the vehicle

Route Data > 

Vehicle Route Actor

Movement Control >

Movement Tick Rate - Movement tick frequency

Steering Boost - Steering boost on route

Turn Steepnes Braking - Min turn steepness to start braking

Turn Detection Distance Multiplier - How far before the turn we try to start braking (multiplied by current speed)

Min De Throttling Speed - If our speed is lower than this - we don't use brakes

Returnal Speed - If our speed is lower than this - we don't use brakes

Struck Detection Speed - If our absolute speed is lower than this than we defined stuck and start reverse

Steep Turn Brake Multiplier - Multiplier of braking force applied before the steep turns

Curves >

Vehicle will behave accordingly to the curves, which define how the vehicle should brake, throttle and turn

Curve_CollisionBraking: defines how hard should the vehicle brake, depending on distance to the obstacle ahead;

Curve_MaxDirectionOffset: defines the steering offset from previous direction. This one prevents the vehicle from doing steep steering on higher speeds;

Curve_RouteReturnValue: defines how much the vehicle will try to return to the route depending on speed. Allow smooth transitions to the route, preventing from steep steering on higher speeds;

Curve_SmoothBrake: defines how smooth we push the brake pedal. (Brake release is instant);

Curve_SmoothDethrottle: defines how smooth we release the throttle. (Throttle increase is instant);

Curve_SpeedBraking: defines braking force, depending on speed. Higher speeds – higher braking forces;

Curve_TracingBySpeed: defines the amount of avoidance traces, depending on speed. Higher speeds – less traces to prevent steep steering on higher speeds;

Curve_TurnBraking: defines the brake input, depending on steering input;

Curve_TurnThrottling: defines the throttle input, depending on steering input;

Collision Avoidance>

Max Reaction Time - Delay between avoidance ticks

Turn Prediction Angle - Max angle of PathTracer rotation

Turn Prediction Smoothing - Path tracer rotation smoothing Lower = smoother

Obstacle Types - Object types to avoid

Min Obstacle Traces - Minimum amount of secondary traces

Max Obstacle Traces - Maximum amount of secondary traces

Min Braking Speed - If our speed lower than this, we don't use brakes during avoidance

Avoidance Steering Boost - How much we steer trying to avoid collision

Min Second Trace Distance - Minimal distance of secondary traces

Min Path Trace Distance - Minimal path tracer distance

Min Blocked Path Length Allowed - If a secondary trace length lower than this, we don't add this direction to blocked ones to prevent going in there

Vehicle Speed Difference  - If other vehicle speed + THIS is lower than our speed - the other vehicle treated as obstacle


FAQ:

(This section will be expanded over time)

How it works:

Following the route:

The component will direct the vehicle by the route (BP_VehicleRoute), until it gets to the end of the route, if it’s not looped, otherwise it will go for another lap.

If there’s no route – movement won’t start or will be stopped if route destroyed.

Other vehicles’ avoidance:

Other vehicles will be treated as an obstacle (go to Collision avoidance to learn more) 

if:  their speed + VehicleSpeedDifference < our speed

otherwise, vehicles in this direction will be ignored (but still tests for other objects in this direction).

VehicleSpeedDifference = 400 u/s (400/100000*3600=14.4 km/h) by default, can be changed in "BPC_VehicleAI".

Collision avoidance:

Obstacle – every actor or component with collision object type, included in ObstacleTypes array, inside BPC_VehicleAI. It’s WorldDynamic, PhysicsObject, Pawn and Vehicle by default.

If an obstacle detected on the way (blue box trace), BPC_VehicleAI will try to avoid it, by tracing and going in free direction along the route. Avoidance brake force is calculated depending on length of the yellow box trace (Curve_CollisionBraking): (рис. 1-5)

If there’s no free acceptable way, the longest trace-to-hit direction will be selected.

Functional description:

Initial Setup:

Everything starts here, this function called on “Begin Play”. First of all this function checks if owner vehicle has a ChaosMovementComponent (If there not any, destroys BPC_VehicleAI).

Then we try to get vehicle body bounds: if AutoSizeCheck is false – we continue with current VehicleDimensions, otherwise we try to automatically get the vehicle body bounds via interface, if there’s one, otherwise we just get skeletal mesh bounds of the vehicle body and then check if there are any wheels present, to calculate the VehicleClearance (No wheels = no clearance = no movement).

At the end the function check if the VehicleRouteActor is set (Just won’t start movement if there not any). 

StartSplineMovement + StartAvoidance:

This one starts the timers, release brakes and allows to perform stuck-check.

BasicMovementTick:

First check if the route still exists, stops the movement if the route invalid.

Saves current speed and current position of the vehicle along spline (closest point on spline), then check if the vehicle reached its destination (If route isn’t looped) and stops the movement, otherwise saves the next calculation point on spline.

Check if we need to make a steep turn to get back on track (Returning).

Then we calculate the throttle input, which is depending on the steering force and our speed.

Then we calculate the brake input, depending on our speed, steering force and turn steepness.

Then we calculate the steering input, depending on route direction, our direction and obstacle presence.

And at the end we check if we’re stuck and should reverse to get back on track.

SimpleAvoidanceTick:

Every tick we make a path trace (PathTrace), to see if there are any obstacles on our way.

When we detect an obstacle – we define the length, angle and amount of secondary traces, then we start to trace (TraceForObstacles) and save acceptable hit results into array, to select the best option later.

Inside TraceForObstacles is a simple trace and vehicle detection logic. If there’s a vehicle hit – we check for its speed and decide if we should treat it as an obstacle or ignore it and repeat trace in this direction, to detect other obstacles.

Then we start to process the results. First of all we check directions, where was no hit – there are no obstacles and we can go there. We are trying to find the best option which is the direction with least offset from route and the last desired direction (if it’s not = 0), if there’s no acceptable direction in the ValidDirections array, then we proceed to BlockedDirections. There we need the least offset from last desired direction (if any) and the longest trace distance.

With the found acceptable direction, we apply AvoidanceSteering to direct the vehicle in the free direction and AvoidanceBraking if the obstacle is close to the vehicle on the DesiredDirection.

Reverse:

This event triggers when our speed is close to 0, which is a consequence of head-on collision usually. If reverse is allowed, gear is switched to reverse, and the vehicle start to slowly move backwards and steer in the opposite direction, to head in the direction to the route. And if there’s no obstacle in front of the vehicle, gear is switched to Neutral and then Drive, and the movement continues.