Sunday, December 8, 2013

Component systems



Component systems

Since my blog on OOP vs. DDP I’ve wanted to write a blog about implementing a component system. Just to refresh component systems are different from objects because they are more worried about the data involved. A component system streamlines data and processes.

In a component system there is the entity manager, generally at the top. In order to do most things you will have to talk to it. The reason for this is because it lets other related systems know what is happening. The related systems need to know because there are some operations they will need to do to keep things going smoothly.

The entity manager will know how to create an entity and broadcast its creation to the component systems. Also, the entity manager knows about all the entities and all the component systems.  The entity manager will know how to register a component system to its list, how to add a component to an entity and any other implementation required.

A component system works off of a base class. The base class can hold the base interactions the entity manager will need. The base operations are things like a pure virtual function that is called to let the system know that its component type has been added to an entity. The base system will also need a string name to tag it and a list of the entities that have it.

The second level to a component system is where all component specific processes are held. The component system is responsible for processing its component type. It will have functions like “ProcessAll”. The “ProcessAll” function is where all the entities with the component the system cares about will be processed.

A component is kind of similar to a component system because it too inherits from a base class. This base class can be very small. In fact, in the base implementation it only needs one thing. The only thing it needs is a pure virtual function. The function will return the type of the component.

The second level to a component is where all the component specific data is held. For example a Renderer component might look like this:
class Renderer : public Component
{
                struct SpriteQuad{//quad data };
public:
                std::string GetType() { return "Renderer"; }

                SpriteQuad m_quad;
};

            Last thing is the entity. The entity in its perfect form will be just an id but for this simple implementation it will be an id and a list of its attached components. The entity will also have a private constructor. The constructor is private because the only thing that should be able to construct an entity is the entity manager. This is so that any component systems need to know about the entities creation the entity manager has already let them know.

            That is the basics for a component system. I would love to develop one and might actually implement one in a project. As far as my GDW game it’s too far down the production chain to change it now.

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.

Wednesday, December 4, 2013

Insomniac Engine’s Navigation



Insomniac Engine’s Navigation


Earlier I was treated with a video on The Insomniac engine and how it does it’s Navigation. It was a great experience to see how an AAA engine runs systems like AI Navigation. Navigation gives a games an AI system more believability if done properly. In the Insomniac engine navigation is done very well.



Implementation in Resistance: Fall of man
            In their first implementation Insomniac implemented a nav-mesh created by designers in Maya. To find the navigation path for an entity A* was used to determine a short rout. However this was an expensive operation and bottlenecked the physical processing unit or PPU. To combat the expense the designers would only have up to 8 A* operations at a given time. Meeting the quota meant that enemies far away didn’t get to path.  


Implementation in Resistance 2
            One of Insomniac’s key designs was that NPCs requested a path from the navigation system to a point the NPC choose. This allowed them to cache recent calculated paths and sub paths. Cached paths do not need to be calculated again so if a NPC happens to need one of those paths it can be given to it quickly.

To improve the performance and optimize the pathing Insomniac found that the previous implementation of n-gons as nav-mesh polys was redundant. They decided that triangles were the best polygon to use because of assumptions they could make. They also split up the nav-mesh into smaller parts that connected. Having smaller parts means more performance as the A* doesn’t have to evaluate as much. 

            To increase the navigations fidelity Insomniac added more parameters to their pathing query. The increase in parameters would allow them to have more advanced traversal methods like jumping or flying to other levels in the nav-mesh. The developers also included an “Ideal radius” which is a parameter that defines a radius around a corner. The Ideal Radius is used to push the path away from a corner that would result in geometry clipping.

Insomniac also implemented a Bezier curve to combat steering. The Bezier was made up from deriving the NPC and the corner. 



Implementation in Resistance 3
            Improving on the caching system determined that caching more information on navigation helped with performance. They also found that one of the greatest performance hits was determining if an NPC was near the border of the nav mesh. To combat this they inserted an edge loop that loops around with border edge and called it the tolerance nav-mesh edge.

            One more thing Insomniac did to increase performance was to implement custom link nodes. The custom nodes would define a start and end point as well as a curve. Also the custom nodes were a better solution to their old complicated way of determining ladders, areas where an NPC can climb, and jumps.