Public Member Functions | Protected Member Functions | Protected Attributes

MovementGenerator Class Reference

(Abstract) MovementGenerator class More...

#include <MovementGenerator.h>

Inheritance diagram for MovementGenerator:
greatGathering groupRoaming hotspotRoaming randomRoaming realWorldRoaming traverseRoaming

List of all members.

Public Member Functions

 MovementGenerator (double areaDimension, double speed, NeighborMap *Neighbors, GlobalCoordinator *coordinator, CollisionList *CollisionRect)
 Initialize the generator with the movement area dimensions and node movement speed.
virtual ~MovementGenerator ()
virtual void move ()=0
 Defined in subclasses only.
Vector2D getPosition ()
 Get the nodes current position.

Protected Member Functions

bool testBounds ()
 Prevents the node from leaving the defined area and checks for obstacle hits.
void flock ()
 Simple flocking algorithm.
void generateScenery (unsigned int seed)
 Generates scenery objects.

Protected Attributes

double areaDimension
double speed
Vector2D direction
Vector2D position
Vector2D target
NeighborMapNeighbors
NeighborMap::iterator itNeighbors
GlobalCoordinatorcoordinator
CollisionListCollisionRect

Detailed Description

(Abstract) MovementGenerator class

An interface for different movement generation set-ups.

Definition at line 52 of file MovementGenerator.h.


Constructor & Destructor Documentation

MovementGenerator::MovementGenerator ( double  areaDimension,
double  speed,
NeighborMap Neighbors,
GlobalCoordinator coordinator,
CollisionList CollisionRect 
)

Initialize the generator with the movement area dimensions and node movement speed.

@param areaDimension Movement range from [-areaDimension, -areaDimension] to [areaDimension, areaDimension]. @param speed Movement speed in units per movement.

Definition at line 31 of file MovementGenerator.cc.

{
    this->areaDimension = areaDimension;
    this->speed = speed;
    this->Neighbors = Neighbors;
    this->coordinator = coordinator;
    this->CollisionRect = CollisionRect;

    Vector2D center(areaDimension / 2, areaDimension / 2);
    position.x = uniform(0.0, areaDimension);
    position.y = uniform(0.0, areaDimension);
    direction = center - position;
    direction.normalize();
    if(CollisionRect != NULL) {
        generateScenery(coordinator->getSeed());
    }
}

virtual MovementGenerator::~MovementGenerator (  )  [inline, virtual]

Definition at line 61 of file MovementGenerator.h.

{}


Member Function Documentation

void MovementGenerator::flock (  )  [protected]

Simple flocking algorithm.

Definition at line 120 of file MovementGenerator.cc.

Referenced by traverseRoaming::move(), randomRoaming::move(), hotspotRoaming::move(), groupRoaming::move(), and greatGathering::move().

{
    Vector2D separation, alignment, cohesion, toTarget;
    int NeighborCount = 0;

    for(itNeighbors = Neighbors->begin(); itNeighbors != Neighbors->end(); ++itNeighbors)
        if(position.distanceSqr(itNeighbors->second.position) < 2.5 && direction.cosAngle(itNeighbors->second.position - position) > -0.75) {
            separation += position - itNeighbors->second.position;
            alignment += itNeighbors->second.direction;
            cohesion += itNeighbors->second.position;
            ++NeighborCount;
        }

    if(NeighborCount > 0) {
        cohesion /= (double)NeighborCount;
        cohesion = cohesion - position;
        separation.normalize();
        alignment.normalize();
        cohesion.normalize();
    }
    toTarget = target - position;
    toTarget.normalize();

    direction = separation * 0.4 + alignment * 0.1 + cohesion * 0.35 + toTarget * 0.25;
    direction.normalize();
}

void MovementGenerator::generateScenery ( unsigned int  seed  )  [protected]

Generates scenery objects.

Definition at line 147 of file MovementGenerator.cc.

Referenced by MovementGenerator().

{
    int dimension = (int)(areaDimension - 10.0);
    int sceneryType;
    double sceneryX, sceneryY;

    srand(seed);

    for(int i = 0; i < dimension; i += 10) {
        for(int j = 0; j < dimension; j+= 10) {
            sceneryType = rand() % 3;
            switch(sceneryType) {
                case 0: { // mud
                    // do nothing except calling rand() for deterministic scenery generation
                    sceneryX = rand();
                    sceneryY = rand();
                } break;
                case 1: { // rock
                    sceneryX = 1 + (rand() % 8);
                    sceneryY = 1 + (rand() % 8);
                    CollisionRect->insert(CollisionRect->begin(), BoundingBox2D(i + sceneryX - 0.25, j + sceneryY - 0.5, i + sceneryX + 1.25, j + sceneryY + 0.25));
                } break;
                case 2: { // tree
                    sceneryX = 1 + (rand() % 6);
                    sceneryY = 1 + (rand() % 5);
                    CollisionRect->insert(CollisionRect->begin(), BoundingBox2D(i + sceneryX, j + sceneryY + 2.0, i + sceneryX + 3.0, j + sceneryY + 3.5));
                } break;
            }
        }
    }
}

Vector2D MovementGenerator::getPosition (  ) 

Get the nodes current position.

@return Returns the current node position.

Definition at line 49 of file MovementGenerator.cc.

Referenced by SimpleGameClient::updatePosition().

{
    return position;
}

virtual void MovementGenerator::move (  )  [pure virtual]
bool MovementGenerator::testBounds (  )  [protected]

Prevents the node from leaving the defined area and checks for obstacle hits.

Definition at line 54 of file MovementGenerator.cc.

Referenced by traverseRoaming::move(), randomRoaming::move(), hotspotRoaming::move(), groupRoaming::move(), and greatGathering::move().

{
    bool obstacleHit = false;

    if(position.x < 0.0) {
        position.x = 0.0;
    }
    if(position.x > areaDimension) {
       position.x = areaDimension;
    }
    if(position.y < 0.0) {
        position.y = 0.0;
    }
    if(position.y > areaDimension) {
        position.y = areaDimension;
    }

    double cosAngle = direction.x;
    SCDir scDirection;
    if(cosAngle > 0.71) {
        scDirection = DIR_RIGHT;
    }
    else if(cosAngle < -0.71) {
        scDirection = DIR_LEFT;
    }
    else if(direction.y > 0.0) {
        scDirection = DIR_UP;
    }
    else {
        scDirection = DIR_DOWN;
    }

    if(CollisionRect != NULL) {
        CollisionList::iterator i;
        for(i = CollisionRect->begin(); i != CollisionRect->end(); ++i) {
            if(i->collide(position)) {
                switch(scDirection) {
                    case DIR_UP:
                        position.y = i->bottom();
                        direction.x = 1.0;
                        direction.y = 0.0;
                        break;
                    case DIR_DOWN:
                        position.y = i->top();
                        direction.x = -1.0;
                        direction.y = 0.0;
                        break;
                    case DIR_LEFT:
                        position.x = i->right();
                        direction.x = 0.0;
                        direction.y = 1.0;
                        break;
                    case DIR_RIGHT:
                        position.x = i->left();
                        direction.x = 0.0;
                        direction.y = -1.0;
                        break;
                }
                obstacleHit = true;
            }
        }
    }

    return obstacleHit;
}


Member Data Documentation

Definition at line 82 of file MovementGenerator.h.

Referenced by generateScenery(), and testBounds().

NeighborMap::iterator MovementGenerator::itNeighbors [protected]

Definition at line 80 of file MovementGenerator.h.

Referenced by flock().

Definition at line 79 of file MovementGenerator.h.

Referenced by flock().


The documentation for this class was generated from the following files: