コード例 #1
0
Vector3 UnitAiMoonGuard::avoidParticles(const WorldInterface& world, const Vector3& originalVelocity) {
    Vector3 agentPosition = getShip().getPosition();
    if (world.getRingDensity(agentPosition) <= 0) {
        return originalVelocity;
    }
    vector<WorldInterface::RingParticleData> particles = world.getRingParticles(agentPosition, SAFE_DISTANCE);
    unsigned int amount = particles.size();
    if (amount == 0) {
        return originalVelocity;
    }

    WorldInterface::RingParticleData cloestParticle = particles[0];
    double closestDistance = cloestParticle.m_position.getDistanceSquared(agentPosition);
    for (unsigned int i = 1; i < amount; i++) {
        WorldInterface::RingParticleData particle = particles[i];
        double distance = particle.m_position.getDistanceSquared(agentPosition);
        if (distance < closestDistance) {
            cloestParticle = particle;
            closestDistance = distance;
        }
    }

    Vector3 avoidVelocity = m_steering_behaviour.avoid(world, avoidVelocity,
                            cloestParticle.m_position,
                            cloestParticle.m_radius,
                            cloestParticle.m_radius,
                            SAFE_DISTANCE);
    double speed = getShip().getSpeedMax() / amount * SAFE_DISTANCE / 100;

    return avoidVelocity.getCopyWithNorm(speed);
}
コード例 #2
0
ファイル: SpaceMongolsUnitAi.cpp プロジェクト: streibeb/cs409
Vector3 UnitAiMoonGuard::avoidRingParticles(const WorldInterface& world, const Vector3& orig_velocity)
{
    if (nearbyRingParticles.size() == 0)
    {
        return orig_velocity;
    }

    RingParticleData nearestParticle;
    Vector3 ship_pos = getShip().getPosition();

    double min_distance = ship_pos.getDistanceSquared(nearbyRingParticles[0].m_position);
    nearestParticle = nearbyRingParticles[0];

    for (int i = 1; i < nearbyRingParticles.size(); i++)
    {
        double temp = ship_pos.getDistanceSquared(nearbyRingParticles[0].m_position);
        if (temp < min_distance)
        {
            min_distance = temp;
            nearestParticle = nearbyRingParticles[i];
        }
    }

    Vector3 v = steeringBehaviour->avoid(world,
                                         orig_velocity,
                                         nearestParticle.m_position,
                                         nearestParticle.m_radius,
                                         RING_PARTICLE_CLEARANCE,
                                         RING_PARTICLE_AVOID_DISTANCE);

    double ringDensity = world.getRingDensity(ship_pos);
    if (ringDensity >= 1)
    {
        v -= (v * ringDensity/100);
    }

    //printf("Avoiding Ring Particles\n");
    return v;
}