UE4 behavior tree implementation for cars.
To run behavior tree in UE4 the actor needs a controller class inherited from the AIController. In this project VehicleAIController is inherited from AAIController class which is a base class in UE4. Unreal also provides a WheeledVehicle base class that is inherited by the WheeledVehicleObject class in this project. This class is included in PhysXVehicle. To include this plugin in the project add 'PhysXVehicle' in Public Dependency Module of the Build.cs file. It should look like this .
Unreal gives an interface to add and remove task easily in the behavior tree. Selector, sequence, decorator can be added without implementing task in C++. But in this project we followed the following convention.
- Precondition: Each task contains a precondition checker that returns true only at particular situation. All the precondition classes start with 'TaskCheck' keyword. For example, 'TaskCheckChangeSpline' is a precondition class.
- Task: Executes after preconditions are true. Start with keyword 'Task'.
Each Behavior tree contains a blackboard. In this project blackboard asset contains all the control, state and threshold variable that will be used inside and outside of the task. For now the blackboard looks like this. More variables will be added for more tasks.
This class contains the behavior tree and blackboard asset. It gets the behavior tree asset from the content folder, initialize blackboard and starts running the behavior tree. Initialize the state, control and threshold variables.
This class initialize some control variables in the blackboard. During each frame it gets the control values from blackboard and apply it.
Unreal provides basic documentation of their vehicle structure. The basic vehicle structure in UE4. Follow the steps to configure physics asset, skeletal mesh, animation blueprint, wheel blueprint.
- Create a blueprint class based on WheeledVehicleObject class. This will give a hierarchy like this.
- Add the VehicleAIController class in the pawn.
- Add skeletal mesh, animation blueprint and materials in the Skeletal Mesh Component.
- Configure the wheels in the vehicle movement component. Bone name in the Wheels setting should be the same as the physics asset. Add appropriate wheel class.
The basic diagram for car is similar to this.
- Constructor AWheeledVehicleObject
- Constructor AVehicleAIController
- BeginPlay AVehicleAIController
- BeginPlay AWheeledVehicleObject
- Tick AWheeledVehicleObject & AVehicleAIController
Vehicles in this implementation assumes that there is a WayPoint class associated with the roads. This Waypoint class needs to have a splinecomponent associatited with it. Vehicles while moving along the road follow this spline to keep track of the roads. The implementation also assumes that each WayPoint has a list of connected spline and boolean variables for stop sign.
Place the car blueprint in the scene and select a WayPoint actor from the scene.
Blackboard plays the most important role in this implementation. All the variables important for the vehicle are in the blackboard. All the task read and write on the blackboard.
Controller class initialize the blackboard and start the behavior tree. At each frame, WheeledVehicleObject reads the control values(Brake, Steering, Throttle) and apply those on the vehicle.
- Add neccesary variables in the blackboard by adding a new key.
- Create two C++ class inherited from UBTTask_BlackboardBase. One is for precondition check and the other is for actual task implementation.
- If the task requires perception to get activated, change the state variable from the perception update function in Controller class.
- Implement actual task in the other C++ class and change the control variable accordingly.
For example, the changing spline task have two classes. TaskCheckChangeSpline check if the car is at the end of spline. If true than it hands the control to the task TaskChangeSpline. In the TaskChangeSpline access the Waypoint from the blackboard and change it to another Waypoint connected with the previous Waypoint.