TheOneTrueKing

THE ONE TRUE KING

Introduction

This project is a continuation of the SDL project and is used as a testing ground where I learn how to use and test different AI algorithm that is commonly used or can be used in games. So far, the idea is to add multiple game states where you can fight against waves of enemies, tactical groups and bosses.

Steering behavior

Introduction

The first thing I wanted to add was movement behaviors. In the SDL project I was using separation behavior to prevent the enemies from stacking up in the same position. Now I've added more steering behaviors for both linear and angular movement.

Functionality

Currently, I am mostly using arrive, align and face behavior. Arrive makes the enemy move toward the target position and slows down when it is at a set radius from the target. Align is used when the enemies has a slot assignment, this makes it mimic the orientation of the slot it is assigned to. Face behavior is used in the survival game mode, which sets the enemies orientation so it look at the target.

Angular behaviors

Angular behaviors controls the characters orientation and rotation. By setting a angular acceleration and angular max speed, the behavior will control how much the character rotates until it has reached the target orientation. I am using three angular behaviors.

Align

Align is used as the base of angular behaviors. It makes the characters orientation mimic the target orientation and rotates the character based on its acceleration and max speed.

LookAtDirection

Face

The other angular behaviors are Face and LookAtDirection. Face behavior sets the target orientation to the direction of where the target position is. LookAtDirection sets the target orientation based on the characters velocity. Then they both return the Align steering behavior with the newly set target orientation.

Linear behaviors

Linear behaviors controls the characters position and velocity. Just as for angular behaviors, they have a set acceleration and max speed. I have implemented four linear behaviors.

Seek

Seek behavior is often used as the base for linear behaviors, it is also the simplest steering behavior. It makes the character move toward the target while accelerating toward a set max speed. The issue with seek is that it doesn't slow down even if it has reached the target, which causes it to overshoot. It can be used for characters that are further away and once they are close enough, you could replace seek with another behavior.

Pursue

Pursue inherits from seek behavior, it sets the target position based on the velocity of the target character and then returns the seek behavior with the new target position.

Velocity match

Velocity match behavior is useful for swarms or troops. It matches its velocity with the target.

Arrive

Another linear behavior I've implemented is arrive behavior. It is similar to seek, but has a deacceleration process when it's at a set distance from the target where it will slow down and a target distance where it will stop. It requires some more calculations, so seek is cheaper and could be used if the character is further away from the target as a way to optimize it.

Avoidance behaviors

When it comes to avoidance behavior, I can use the CollisionAvoidance for characters and ObstacleAvoidance for obstacles. There are some problems with the detection, especially when it comes to corners and tight spaces where it tend to get confused and stuck. It is less troublesome to use pathfinding combined with grid or navmesh than making this work in most cases. It was still a fun thing to try out, just to see and understand how it would work.

Collision avoidance

Collision avoidance checks if the character is close to other characters, then goes through characters in range and calculates based on position and velocity the time it will take for them to collide. The character that has the shortest time before collision becomes priority. The characters velocity is set based on the direction between the characters.

The green line goes from the characters position to the new target position, the red line goes from the character to the relative velocity between the current character and the character it collides with.

Obstacle avoidance

Obstacle avoidance uses raycasts to detect if the character is going toward a wall. If a wall is detected, the target position is updated based on the normal of raycast point. It inherits from and returns the seek behavior with the updated target position.

Wander behavior

This behavior uses elements from both Face and Seek behavior. It behavior creates a point in front of the character and slightly offsets it each update with a random float number between -1 and 1. Then it uses the face behavior to orient itself toward the target and uses the seek behavior to walk toward the target.

Blend steering and priority steering

Blend steering stores different behaviors in a map and returns a combined result of the behaviors.

Priority steering uses a vector of blend steering to store the behaviors as a group. It goes through the blend steering and returns as soon as it got a value. This is useful when you are using obstacle avoidance, when you want the character to use the collision avoidance without getting disturbed by other steering outputs.

The enemies are currently using priority steering with a blend steering group of Arrive, Separation and Face behavior, where it is able to replace face behavior for align behavior if needed and vice versa.

Slot formation

Introduction

The idea is to create a slot formation, currently there is a VShape and Circle formation. When activated, it creates slots in the set formation and then I can use them as targets for the characters. This makes it look like the characters are working together with defenders in front of mages.

Functionality

First I create the slots based on the chosen pattern and give each slot a attack type, then the enemy will pick a weapon based on that attack type and the slot assignments will be reconstructed. Then I'm calling an UpdateSlots function which moves the slot toward the target position, since the characters are using the arrive behavior, they will automatically follow the slot.

If a characters dies, it is removed from the slot and the slot assignments can be reconstructed, but I'm currently not using the function for that.

V-Shape formation

Circle

formation