Ejemplo n.º 1
0
    public override Steering getSteering(AgenteNPC agente)
    {
        // Inicializo el detector de colisiones
        cd = new CollisionDetector();
        // Calculo  el rayo del vector de colision
        Vector3 targetPosition = targetWallAvoidance.getPosition();

        // Vector3 que representan los "bigotes" que buscaran colisiones
        //rayVector = agente.getVelocity().normalized;
        rayVector = (targetPosition - agente.getPosition()).normalized;

        //float angle = Cuerpo.positionToAngle(agente.getPosition());
        float angle = positionToAngle(rayVector);

        rayVectorL  = orientationToVector(angle + anguloApertura * Mathf.Deg2Rad);
        rayVectorR  = orientationToVector(angle - anguloApertura * Mathf.Deg2Rad);
        rayVector  *= lookAhead;
        rayVectorL *= lookAhead * .8f;
        rayVectorR *= lookAhead * .8f;

        // Encuentra las posibles colisiones
        Collision collision, collisionL, collisionR;

        collision  = cd.getCollision(agente.getPosition(), rayVector);
        collisionL = cd.getCollision(agente.getPosition(), rayVectorL);
        collisionR = cd.getCollision(agente.getPosition(), rayVectorR);

        target = new AgenteNPC(targetPosition);
        if (collision == null && collisionL == null && collisionR == null)
        {
            return(base.getSteering(agente));
        }

        if (collision != null)
        {
            targetPosition = collision.getPosition() + collision.getNormal() * avoidDistance;
        }
        if (collisionL != null)
        {
            targetPosition = collisionL.getPosition() + collisionL.getNormal() * avoidDistance;
        }
        if (collisionR != null)
        {
            targetPosition = collisionR.getPosition() + collisionR.getNormal() * avoidDistance;
        }

        target = new AgenteNPC(targetPosition);
        return(base.getSteering(agente));
    }
Ejemplo n.º 2
0
    public override Steering getSteering(AgenteNPC agente)
    {
        int   count = 0;
        float distance;
        float orientacion = Cuerpo.positionToAngle(Vector3.zero);
        float direction   = 1;
        float alpha;

        foreach (Agente t in targets)
        {
            distance = (t.getPosition() - agente.getPosition()).magnitude;

            if (distance > threshold)
            {
                continue;
            }

            orientacion += t.getOrientation();
            count++;
        }

        if (count > 0)
        {
            orientacion /= count;
            orientacion -= agente.getOrientation();
        }


        return(new Steering(orientacion, Vector3.zero));
    }
Ejemplo n.º 3
0
    public Steering getSteering(AgenteNPC agente)
    {
        int     count        = 0;
        Vector3 centerOfMass = Vector2.zero;
        Vector3 direction;
        float   distance;

        foreach (Agente t in targets)
        {
            direction = t.getPosition() - agente.getPosition();
            distance  = Vector3.Magnitude(direction);

            if (distance > threshold)
            {
                continue;
            }

            centerOfMass += t.getPosition();
            count++;
        }

        if (count == 0)
        {
            return(null);
        }

        centerOfMass /= count;
        target        = new Agente(centerOfMass);


        return(base.getSteering(agente));
    }
Ejemplo n.º 4
0
    public Steering getSteering(AgenteNPC agente)
    {
        Vector3 lineal = (agente.getPosition() - target.getPosition()).normalized;

        lineal *= agente.maxAcceleration;
        lineal -= agente.velocity;
        return(new Steering(0, lineal));
    }
Ejemplo n.º 5
0
    public Steering getSteering(AgenteNPC agente)
    {
        Vector3 direction = objetivoPursue.getPosition() - agente.getPosition();
        float   distance  = direction.magnitude;

        float speed = agente.getVelocity().magnitude;

        if (speed <= distance / maxPrediction)
        {
            prediction = maxPrediction;
        }
        else
        {
            prediction = distance / speed;
        }

        target = new AgenteNPC(objetivoPursue.getPosition());

        target.position += objetivoPursue.getVelocity() * prediction;

        return(base.getSteering(agente));
    }
Ejemplo n.º 6
0
    public override Steering getSteering(AgenteNPC agente)
    {
        float    maxAcc   = agente.getMaxAcceleration();
        Steering steering = new Steering(0, Vector3.zero);
        Vector3  direction;
        float    distance;
        float    strength;

        foreach (Agente t in targets)
        {
            direction = agente.getPosition() - t.getPosition();
            distance  = direction.magnitude;

            if (distance < threshold)
            {
                strength         = Mathf.Min(decayCoefficient / (distance * distance), maxAcc);
                direction        = direction.normalized;
                steering.linear += strength * direction;
            }
        }

        return(steering);
    }
Ejemplo n.º 7
0
    public override Steering getSteering(AgenteNPC agente)
    {
        Vector3 relativePos, relativeVelocity;
        float   relativeSpeed, timeToCollision;
        float   distance, minSeparation;
        float   shortestTime = Mathf.Infinity;
        Agente  firstTarget = null;
        Vector3 firstRelativePos = Vector3.zero, firstRelativeVelocity = Vector3.zero;
        float   firstMinSeparation = 0, firstDistance = 0;

        foreach (Agente t in targets)
        {
            // Calculo del tiempo hasta la colision
            relativePos      = t.getPosition() - agente.getPosition();
            relativeVelocity = t.getVelocity() - agente.getVelocity();
            relativeSpeed    = relativeVelocity.magnitude;
            timeToCollision  = Vector3.Dot(relativePos, relativeVelocity) / relativeSpeed * relativeSpeed;


            // Determina si va a haber una colision
            distance      = relativePos.magnitude;
            minSeparation = distance - (relativeSpeed * timeToCollision);

            if (minSeparation > (2 * radius))
            {
                continue;
            }

            // Comrpuebo si es el mas cercano/corto
            if (timeToCollision > 0 && timeToCollision < shortestTime)
            {
                shortestTime          = timeToCollision;
                firstTarget           = t;
                firstMinSeparation    = minSeparation;
                firstDistance         = distance;
                firstRelativePos      = relativePos;
                firstRelativeVelocity = relativeVelocity;
            }
        }

        // Si no tenemos objtivo, salimos
        if (firstTarget == null)
        {
            return(new Steering(0, Vector3.zero));
        }

        // Si vamos a colisionar y ya estamos colisionando, hacemos steering
        // basado en la posicion actual;
        if (firstMinSeparation <= 0 || firstDistance < 2 * radius)
        {
            relativePos = firstTarget.getPosition() - agente.getPosition();
        }
        else
        {
            relativePos = firstRelativePos + (firstRelativeVelocity * shortestTime);
        }

        // Evitar al objetivo
        relativePos = relativePos.normalized;
        evasion     = relativePos * agente.getMaxAcceleration();
        return(new Steering(0, evasion));
    }