Beispiel #1
0
    private void RaycastBlockEvaluator(float[] mapToModify, CombiningMethod combiningMethod,
                                       Vector3 origin, float radius)
    {
        int count = mapToModify.Length;

        Vector3[] directions = ContextMap.GetDiscreteUnitCircleDirections(count);

        RaycastHit hit;

        for (int i = 0; i < count; i++)
        {
            bool  didHit = Physics.Raycast(origin, directions [i], out hit, radius);
            float hitFraction;
            if (!didHit)
            {
                hitFraction = 1f;
            }
            else
            {
                hitFraction = hit.distance / radius;
            }

            ContextMap.CombineMapValues(mapToModify, hitFraction * hitFraction, i, combiningMethod);
        }
    }
Beispiel #2
0
    private Vector3 ChooseDirectionGivenContextMapAndHeading(float[] map, Vector3 heading)
    {
        int count = map.Length;

        Vector3[] directions = ContextMap.GetDiscreteUnitCircleDirections(count);
        float     epsilon    = 0.1f;
        int       bestIndex  = 0;
        int       i          = 0;
        float     maxValue   = map [i];
        float     bestAmountTowardDirection = Vector3.Dot(heading, directions [i]);

        for (i = 1; i < count; i++)
        {
            float currVal = map [i];

            // When there is a tie between directional goodness, prefer the one closest to heading.
            if (Mathf.Abs(currVal - maxValue) <= epsilon)
            {
                float currAmountTowardDirection = Vector3.Dot(heading, directions [i]);
                if (currAmountTowardDirection > bestAmountTowardDirection)
                {
                    bestAmountTowardDirection = currAmountTowardDirection;
                    bestIndex = i;
                    maxValue  = currVal;
                }
                continue;
            }

            if (currVal > maxValue)
            {
                bestAmountTowardDirection = Vector3.Dot(heading, directions [i]);
                bestIndex = i;
                maxValue  = currVal;
                continue;
            }
        }

        Vector3 dir = directions[bestIndex] * map[bestIndex];

        dir += directions[MathUtils.Modulo(bestIndex + 1, count)] * map[MathUtils.Modulo(bestIndex + 1, count)];
        dir += directions[MathUtils.Modulo(bestIndex - 1, count)] * map[MathUtils.Modulo(bestIndex - 1, count)];
        dir /= 3f;

        return(dir);
    }