Exemplo n.º 1
0
def pursueoffset(agent, 
                 targetAgent, 
                 targetToOffset):
    agentPosition = agent.getPosition()
    agentMaxSpeed = agent.getMaxSpeed()
    targetPosition = targetAgent.getPosition()
    targetDirection = targetAgent.getDirection()
    targetSpeed = targetAgent.getSpeed()
    targetVelocity = targetAgent.getVelocity()
    worldTargetToOffset = convert.vectorToWorldSpace(targetToOffset,
                                                     targetPosition,
                                                     targetDirection)
    offsetPosition = calculate.addPointAndVector(targetPosition,
                                                 worldTargetToOffset)
    
    agentToOffset = calculate.subtractPoints(offsetPosition,
                                             agentPosition)
    distanceToOffset = vector.getMagnitude(agentToOffset)
    
    if targetSpeed == 0:
        lookAheadTime = 0
    else:
        lookAheadTime = distanceToOffset / (agentMaxSpeed + targetSpeed)
    
    targetToPredictedPosition = calculate.multiplyVectorAndScalar(targetVelocity,
                                                                  lookAheadTime)
    predictedOffsetPosition = calculate.addPointAndVector(offsetPosition,
                                                          targetToPredictedPosition)
    
    return arrive.arrive(agent,
                         predictedOffsetPosition,
                         .9)
Exemplo n.º 2
0
def hide(agent,
         attacker, 
         obstacles):
    distanceFromObstacleBoundary = 30
    closestDistance = None
    closestHidingPlace = None
    attackerPosition = attacker.getPosition()
    agentPosition = agent.getPosition()
    for obstacle in obstacles:
        obstaclePosition = obstacle.getPosition()
        hidingPlaceDistanceToObstacle = distanceFromObstacleBoundary + obstacle.getRadius()
        attackerToObstacle = calculate.subtractPoints(obstaclePosition,
                                                      attackerPosition)
        attackerDistanceToObstacle = vector.getMagnitude(attackerToObstacle)
        attackerDistanceToHidingPlace =  hidingPlaceDistanceToObstacle + attackerDistanceToObstacle
        attackerToHidingPlace = vector.setMagnitude(attackerToObstacle,
                                                    attackerDistanceToHidingPlace)

        hidingPlace = calculate.addPointAndVector(attackerPosition,
                                                  attackerToHidingPlace)
        
        agentToHidingPlace = calculate.subtractPoints(hidingPlace,
                                                      agentPosition)
        distanceToHidingPlace = vector.getMagnitude(agentToHidingPlace)
        
        if closestDistance is None or distanceToHidingPlace < closestDistance:
            closestDistance = distanceToHidingPlace
            closestHidingPlace = hidingPlace
        
    if closestHidingPlace is None:
        return evade.evade(agent,
                           attacker)
        
    return arrive.arrive(agent,
                         closestHidingPlace)
Exemplo n.º 3
0
def interpose(agent,
              enemy,
              charge):
    agentPosition = agent.getPosition()
    agentMaxSpeed = agent.getMaxSpeed()
    enemyPosition = enemy.getPosition()
    enemyVelocity = enemy.getVelocity()
    chargePosition = charge.getPosition()
    chargeVelocity = charge.getVelocity()
    enemyToCharge = calculate.subtractPoints(chargePosition,
                                             enemyPosition)
    midVector = calculate.multiplyVectorAndScalar(enemyToCharge,
                                                  .5)
    midPoint = calculate.addPointAndVector(enemyPosition,
                                           midVector)
    
    agentToMidPoint = calculate.subtractPoints(midPoint,
                                               agentPosition)
    distanceToMidPoint = vector.getMagnitude(agentToMidPoint)
    timeToMidPoint = distanceToMidPoint / agentMaxSpeed
    
    enemyToFuturePosition = calculate.multiplyVectorAndScalar(enemyVelocity,
                                                              timeToMidPoint)
    enemyFuturePosition = calculate.addPointAndVector(enemyPosition,
                                                      enemyToFuturePosition)
    
    chargeToFuturePosition = calculate.multiplyVectorAndScalar(chargeVelocity,
                                                               timeToMidPoint)
    chargeFuturePosition = calculate.addPointAndVector(chargePosition,
                                                       chargeToFuturePosition)
        
    
    
    enemyFutureToChargeFuture = calculate.subtractPoints(chargeFuturePosition,
                                                         enemyFuturePosition)
    futureMidVector = calculate.multiplyVectorAndScalar(enemyFutureToChargeFuture,
                                                        .5)
    futureMidPoint = calculate.addPointAndVector(enemyFuturePosition,
                                                 futureMidVector)
    
    return arrive.arrive(agent,
                         futureMidPoint)