In this project, I implemented Dynamic Obstacle Avoidance for an AI Agent. I say dynamic because the agent is reacting to the environment and steering away from obstacles in real time, as obstacles move around and the agent wanders around the map (see demo below).
In the project, I use scriptable objects to define steering behaviours: each behaviour is derived from a base Steering class, and each class derived from Steering implements a GetSteering( ) function, which returns a Vector2 for the direction in which the AI Agent will steer towards. Also implemented is a CompositeSteering object, which allows me to add as many Steering objects as I would like, and combine their behaviours using a weighted blending algorithm (basically scaling the resulting Vector2 by the corresponding weight, and averaging the result of all the Steering objects in the array).
In the demonstration, the AI Agent's CompositeSteering contains a blend of WanderSteering and ObstacleAvoidanceSteering. In this case, both are of equal weight, so they have equal control of the agent's behaviour.
Wander is implemented by having the agent select a point on a circle some distance away and of some radius (both values are modifiable in the scriptable object) every few frames, and steer in the direction of the vector. This is the method described by Craig Reynolds, and linked in the references below.
Obstacle avoidance is implemented through Raycasting: the agent casts 3 rays forward (visualised with a line renderer), using them to detect obstacles and avoid collision. When a ray hits an obstacle, the agent moves away depending on the angle of the ray relative to the surface normal at the point of contact. If the normal is to the left side of the ray, then the boid must steer left (the same logic applies to the right side). All values related to raycasting, including the separating angles of the rays cast to the right and left of the centre ray, can be tweaked in the inspector window for the Steering behaviour.
References:
Craig Reynolds: Steering Behaviors For Autonomous Characters (p. 12): Explains multiple steering behaviours and their implementation for AI agents. Page 12 briefly goes over the Wander method I was referring to.
Object avoidance video: Link to a time stamp explaining the logic behind the steering direction in relation to the surface normal where the ray hit.
Comments