Esempio n. 1
0
void Ant::drive(float distance) {

    float speed = *travelVelocity;
    float direction = getCompass();

    util->tic(distance / speed * 1000);

    move->forward(speed, speed);

    //Drive while adjusting for detected objects and motor drift
    while(1) {
        driftCorrection(direction); //correct for motor drift

        float distanceLeft = (util->timerStop - millis()) / 1000 * speed;
        direction = collisionAvoidance(direction, distanceLeft); //check for collision; maneuver and update location

        //If the timer has expired (i.e. we have reached our goal location)
        if(util->isTime()) break;
    }

    move->stopMove();
}
Esempio n. 2
0
void AutoSystem::automize(Entity _entity)
{
  // step through each of the behaviors in the given order
  for(unsigned int i=0;i<_entity->m_auto.m_behaviors.size();++i)
  {
    // check free space in accumulator
    m_accelSpace=_entity->m_flight.m_maxAcceleration-_entity->m_auto.m_accumulator.length();
    if(m_accelSpace<=0.0f)
    {
      // stop loop if empty
      break;
    }
    // assign behavior to string to allow quicker access
    std::string behavior=_entity->m_auto.m_behaviors[i];
    // below is a large, inefficient list of conditional checks to call the correct function
    // Would have liked to implement the following :-
    // Insane Coding (2007). Secrets to Optimization - Function Pointers [online]. [Accessed 2013].
    // Available from: <http://insanecoding.blogspot.co.uk/2007/05/secrets-to-optimization-function.html>.
    if(behavior=="containmentSphere")
    {
      containmentSphere(_entity);
    }
    else if(behavior=="followLeader")
    {
      followLeader(_entity);
    }
    else if(behavior=="clearPath")
    {
      clearPath(_entity);
    }
    else if(behavior=="targetPlayer")
    {
      targetPlayer(_entity);
    }
    else if(behavior=="avoidPlayer")
    {
      avoidPlayer(_entity);
    }
    else if(behavior=="collisionAvoidance")
    {
      collisionAvoidance(_entity);
    }
    else if(behavior=="velocityMatching")
    {
      velocityMatching(_entity);
    }
    else if(behavior=="flockCentering")
    {
      flockCentering(_entity);
    }
    else if(behavior=="seekTarget")
    {
      // the pursue function without inverse
      pursueTarget(_entity, false);
    }
    else if(behavior=="fleeTarget")
    {
      // the pursue function with inverse to create avoidance behavior
      pursueTarget(_entity, true);
    }
    else if(behavior=="wanderAround")
    {
      wanderAround(_entity);
    }
    else if(behavior=="arriveTarget")
    {
      arriveTarget(_entity);
    }
    else if(behavior=="death")
    {
      death(_entity);
    }
    else
    {
      std::cout<<"--INVALID BEHAVIOR--\n";
      return;
    }
  }
}