Friday, December 6, 2013

Aeolus AI



Aeolus Turret AI

In my GDW game we don’t have some intense A.I. that uses all the new techniques. Looking back at a previous blog, I mentioned that AI for games can get away with basic properties and behaviours. The basics you need for turret AI is if a player is in range attack the player. Aeolus emulates turrets in the same way; if the player is close enough, attack it.

Let’s take a look at how exactly the turret A.I. is done. We can start by examining the update function. There are two kinds of update functions in the A.I. . One is for the common properties each turret A.I. has. These things are the weapon update, generator update, getting the players position, getting the rotation to the player and updating the behaviour. The second kind of update function is the update behaviour function. This call is actually a function pointer to the current update behaviour function. The function is where all AI state specific processes are handled. I like this implementation because it cleans up the code. Another perk is that the function pointer takes away the nasty switch statement from being evaluated every frame, which would have reduced performance. Below is a high level representation of the main update function
Turret Update
                {               Update common steps for all AI States

                                //update behavior
                                ((*this).*_AIUpdateFunction)();

                                Set the orientation of the turret
                               
                                Set the orientation of the Havok body of the turret
                }

            One of the states that the “_AIUpdateFunction” can point to is the “Idle A.I. behaviour update” function. The idle state is the default state that eh AI is set to. This function is very simple as you can see below but it shows the structure of the AI code.
                void Turret::_AIBehaviourIdle()
                {
                                if(_PlayerPosition.length() < _SightRange)
                                {ChangeState(ATTACKING);}
                }

All this update does is check if the player is in the turrets shooting range. If the player comes close enough the A.I. will change state to the attacking state. If the player is out of range the turret just sits there doing nothing.

            There are 4 states in the turret AI. Two of these states are idle states. The first idle state is your basic Idle – do nothing. The other is a state called wondering. Wondering doesn’t refer to actually walking around, but having the turret’s gaze wonder around. The implementation of two idle A.I. states was to emulate a move lively scene. The third state is searching state which act like the attack state. The difference is that in the searching state the turret looks at the player but does not shoot at it. The last state is the attacking state. You guessed it in this state the turret attacks the player.

            Changing states is done through a switch statement that basically takes in an enumeration and sets the state and the behaviour update call back to the correct call back. 

            The hardest to write and the only function that needed bug testing was the function that got the rotation to turn to the player. Maybe it was that Emi and I were very tired (all-nighter :P) and were incapable of doing math. The math to get the quaternion to the player has some very simple steps. We construct a rotation matrix and convert it to a quaternion.

·         -First step was to get a vector to the player but with 0 as y. We could take y out because we know that it   doesn’t make scents to have the turret rotate in the y axis

·         -We take the result and get the cross product with the y axis to give us the right vector

·         -To get the up vector we do another cross product but with the vector to the player and the right vector.

·         -Using the 3 vectors to construct the matrix and convert it to the end resulting quaternion 

This was our simple A.I. set up this semester.

No comments:

Post a Comment