InExIn


Detailed Description

Library for an artificial intelligence in computer games.

At the moment the library provides pathfinding via navigation meshes, organization of AI objects in AI world, actor's decisioning via state machine and basic set of states for actors.

Coordinate system and units

AI Lib use right-handed coordinate system. For more information about coordinate systems see DirectX documentation (DirectX Graphics / Programming Guide / 3-D Coordinate Systems and Geometry / 3-D Coordinate systems).

AI Lib has no space units but it is tested on 1 unit = 1 m. Please do not use too little metrics because of possible float rounding errors.

Guide to usage

Even if you can use some classes such as AI_NavigationMesh, AI_AStar or spatial database classes by your own it is the common way how to use the library:

Examples

World creation

    //create world    
    AI_World world;
    
    //build navigation mesh
    AI_NavigationMesh &navigation_mesh = world.getNavigationMesh();
    
    float           ptr_vertices[];
    unsigned short  ptr_triangles[];
    unsigned short  ptr_edges[];
    
    int i_num_vertices; //num of vertces * 3 (x,y,z)
    int i_num_triangles; // num of triangles * 3 (index vertex1, index vertex2, index vertex3)
    int i_num_edges; //num of edges * 4 (index triangle1, index triangle2, index edge1, index edge2)
    
    loadMeshFromYourFile(ptr_vertices, i_num_vertices, ptr_triangles, i_num_triangles, ptr_edges, i_num_edges);
    
    for (int i=0; i<i_num_vertices; i++)
        navigation_mesh.addVertex( AI_Vector3(ptr_vertices[i], ptr_vertices[i+1], ptr_vertices[i+2]) );

    for (int i=0; i<i_num_triangles; i+3)
        navigation_mesh.addTriangle( ptr_triangles[i], ptr_triangles[i+1], ptr_triangles[i+2]) );

    for (int i=0; i<i_num_edges; i+3)
        navigation_mesh.addEdge( ptr_edges[i], ptr_edges[i+1], ptr_edges[i+2], ptr_edges[i+3] );

    //setup world
    world.setAutoEnable(true);
    world.setAutoEnableDistance(50.0f);

    world.setAutoDisable(true);
    world.setAutoDisableDistance(70.0f);
    
    //this line must be add after the pivot is known and added into the world
    world.setAutoEnableDisablePivot( ptr_agent_pivot );

Actor creation

    //create an actor    
    AI_Actor *ptr_actor = new AI_Actor;
    
    //set characteristics
    ptr_actor->setLinearVelocity( 3.0f );
    ptr_actor->setAngularVelocity( 1.0f );
    ptr_actor->setRefex( 0.2 );
    ptr_actor->setSight( 30.0f );
    ptr_actor->setEarshot( 15.0f );
    ptr_actor->setFOV( 0.785f );
    ptr_actor->setRange( 10.0f );
    ptr_actor->setOptimalDistance( 7.0f );

    //create and set state machine (this state machine consists of one stop state)
    //store state machine pointer and delete them at the end of the game (SM clear all states)
    MUT_AI_StateMachine *ptr_sm = new AI_StateMachine;
    
    ptr_sm->beginStates(1);
    ptr_sm->addState( 0, new AI_Actor::State(0) );
    ptr_sm->endStates();
    
    ptr_actor->setStateMachine( ptr_sm );

    //add into the world
    ptr_actor->AddRef();
    world.addAgent(ptr_actor);

Actor handling

    void YourActorWrapper::readState(void)
    {
        switch ( this->ptr_actor->getAct() )
        {
        case AI_Actor::WALK:
            this->updateActorMovement( this->ptr_actor->getWalkDirection() );
            this->updateActorRotation( this->ptr_actor->getTurnDirection() );
            this->playWalkingAnimation();
            break;
    
        case AI_Actor::TURN:
            this->updateActorRotation( this->ptr_actor->getTurnDirection() );
            this->playTurningAnimation();
            break;
    
        case AI_Actor::ATTACK:
            attackToActor( (YourActorWrapper*)this->ptr_actor->getAttackTarget()->getUserData() );
            this->playAttackAnimation();
            break;
        }        
    }
    
    
    void YourActorWrapper::sendFeedback(void)
    {
        if ( this->isActorMovedSomehow() )
        {
            this->ptr_actor->setPosition( this->getTranslation() );
            this->ptr_actor->setOrientation( this->getEulerAnglesIn0ToPi() );
            this->ptr_actor->move();
        }
        
        if ( this->ptr_actor->getAct()==AI_Actor::ATTACK )
        {
            if ( this->isAttackAnimationFinished() )
            {
                this->ptr_actor->sendFeedback(AI_Actor::ATTACK_FINISHED);
            }
        }
    }


Modules

 Mathematics
 Mathematics library.
 Spatial Database
 Basic spatial database for visibility and spatial region queries.
 Utilities
 Very basic help classes.

Classes

class  AI_Actor
 AI agent as an actor. More...
class  AI_Agent
 Agent in the AI world. More...
class  AI_AStar
 Generic A* (pronounce a-star) algorithm implementation. More...
class  AI_NavigationMesh
 Navigation mesh for agent pathfinding. More...
class  AI_Obstacle
 AI agent as an obstacle. More...
class  AI_State
 One generic state of state machine. More...
class  AI_StateMachine
 Generic state machine. More...
class  AI_World
 World for AI agents. More...