Esempio n. 1
0
 void Instances()
 {
     _rb2d                   = GetComponent <Rigidbody2D>();
     _cam                    = Camera.main;
     _circleCollider         = GetComponent <CircleCollider2D>();
     _raycastCollision       = GetComponent <RaycastCollision>();
     _circleCollider.enabled = false;
 }
Esempio n. 2
0
 private void OnEnable()
 {
     script        = (RaycastCollision)target;
     m_ShowFields1 = new AnimBool(true);
     m_ShowFields1.valueChanged.AddListener(Repaint);
     m_ShowFields2 = new AnimBool(true);
     m_ShowFields2.valueChanged.AddListener(Repaint);
     _type   = serializedObject.FindProperty("type");
     _offset = serializedObject.FindProperty("offset");
     _radius = serializedObject.FindProperty("radius");
     _box    = serializedObject.FindProperty("box");
 }
    public RaycastResult RaycastUntil(List<RaycastCollision> passThroughCollisions, IntegerVector origin, Vector2 direction, int passThroughMask, int haltMask, float range = 100000.0f)
    {
        passThroughMask &= ~haltMask;
        Vector2 d = direction * range;
        Vector2 chunkD = range <= this.RaycastChunkSize ? d : direction * this.RaycastChunkSize;

        IntegerVector halfwayPoint = new IntegerVector(chunkD / 2.0f) + origin;
        IntegerVector rangeVector = new IntegerVector(Mathf.RoundToInt(Mathf.Abs(chunkD.x) + 2.55f), Mathf.RoundToInt(Mathf.Abs(chunkD.y) + 2.55f));
        List<IntegerCollider> possibleCollisions = this.GetCollidersInRange(new IntegerRect(halfwayPoint, rangeVector), passThroughMask | haltMask);

        Vector2 positionModifier = Vector2.zero;
        IntegerVector position = origin;

        float incX = d.x;
        float incY = d.y;

        if (Mathf.Abs(incX) > RAYCAST_MAX_POSITION_INCREMENT || Mathf.Abs(incY) > RAYCAST_MAX_POSITION_INCREMENT)
        {
            Vector2 dNormalized = d.normalized * RAYCAST_MAX_POSITION_INCREMENT;
            incX = dNormalized.x;
            incY = dNormalized.y;
        }

        Vector2 projected = Vector2.zero;
        Vector2 soFar = Vector2.zero;
        float dMagnitude = d.magnitude;
        RaycastResult result = new RaycastResult();
        List<IntegerCollider> collided = new List<IntegerCollider>();
        bool endReached = false;
        int chunksSoFar = 1;

        while (true)
        {
            if (soFar.magnitude >= this.RaycastChunkSize * chunksSoFar)
            {
                // Recalculate chunk
                halfwayPoint = new IntegerVector(chunkD / 2.0f) + position;
                rangeVector = new IntegerVector(Mathf.RoundToInt(Mathf.Abs(chunkD.x) + 2.55f), Mathf.RoundToInt(Mathf.Abs(chunkD.y) + 2.55f));
                possibleCollisions = this.GetCollidersInRange(new IntegerRect(halfwayPoint, rangeVector), passThroughMask | haltMask);
                foreach (IntegerCollider collider in collided)
                    possibleCollisions.Remove(collider);
                ++chunksSoFar;
            }

            projected.x += incX;
            projected.y += incY;

            if (projected.magnitude > dMagnitude)
            {
                incX = d.x - soFar.x;
                incY = d.y - soFar.y;
                endReached = true;
            }

            positionModifier.x += incX;
            int move = (int)positionModifier.x;

            positionModifier.x -= move;
            int unitDir = Math.Sign(move);

            while (move != 0)
            {
                IntegerVector checkPos = new IntegerVector(position.X + unitDir, position.Y);
                GameObject collision = this.CollidePointFirst(checkPos, possibleCollisions);
                if (collision)
                {
                    IntegerCollider collider = collision.GetComponent<IntegerCollider>();
                    possibleCollisions.Remove(collider);
                    RaycastCollision hit = new RaycastCollision();
                    hit.CollidedObject = collision;
                    hit.CollisionPoint = position;
                    hit.CollidedX = true;
                    if (((1 << collision.layer) & passThroughMask) != 0)
                    {
                        passThroughCollisions.Add(hit);
                        collided.Add(collider);
                    }
                    else
                    {
                        result.Collisions = new RaycastCollision[1];
                        result.Collisions[0] = hit;
                    }
                }

                position = checkPos;

                if (result.Collided)
                    break;

                move -= unitDir;
            }

            if (result.Collided)
                break;

            positionModifier.y += incY;
            move = (int)positionModifier.y;

            positionModifier.y -= move;
            unitDir = Math.Sign(move);

            while (move != 0)
            {
                IntegerVector checkPos = new IntegerVector(position.X, position.Y + unitDir);
                GameObject collision = this.CollidePointFirst(checkPos, possibleCollisions);
                if (collision)
                {
                    IntegerCollider collider = collision.GetComponent<IntegerCollider>();
                    possibleCollisions.Remove(collider);
                    RaycastCollision hit = new RaycastCollision();
                    hit.CollidedObject = collision;
                    hit.CollisionPoint = position;
                    hit.CollidedY = true;
                    if (((1 << collision.layer) & passThroughMask) != 0)
                    {
                        passThroughCollisions.Add(hit);
                        collided.Add(collider);
                    }
                    else
                    {
                        result.Collisions = new RaycastCollision[1];
                        result.Collisions[0] = hit;
                    }
                }

                position = checkPos;

                if (result.Collided)
                    break;

                move -= unitDir;
            }

            if (result.Collided || endReached)
                break;

            soFar.x = projected.x;
            soFar.y = projected.y;
        }

        result.FarthestPointReached = position;
        return result;
    }
Esempio n. 4
0
    public RaycastResult RaycastUntil(List <RaycastCollision> passThroughCollisions, IntegerVector origin, Vector2 direction, int passThroughMask, int haltMask, float range = 100000.0f)
    {
        passThroughMask &= ~haltMask;
        Vector2 d      = direction * range;
        Vector2 chunkD = range <= this.RaycastChunkSize ? d : direction * this.RaycastChunkSize;

        IntegerVector          halfwayPoint       = new IntegerVector(chunkD / 2.0f) + origin;
        IntegerVector          rangeVector        = new IntegerVector(Mathf.RoundToInt(Mathf.Abs(chunkD.x) + 2.55f), Mathf.RoundToInt(Mathf.Abs(chunkD.y) + 2.55f));
        List <IntegerCollider> possibleCollisions = this.GetCollidersInRange(new IntegerRect(halfwayPoint, rangeVector), passThroughMask | haltMask);

        Vector2       positionModifier = Vector2.zero;
        IntegerVector position         = origin;

        float incX = d.x;
        float incY = d.y;

        if (Mathf.Abs(incX) > RAYCAST_MAX_POSITION_INCREMENT || Mathf.Abs(incY) > RAYCAST_MAX_POSITION_INCREMENT)
        {
            Vector2 dNormalized = d.normalized * RAYCAST_MAX_POSITION_INCREMENT;
            incX = dNormalized.x;
            incY = dNormalized.y;
        }

        Vector2                projected  = Vector2.zero;
        Vector2                soFar      = Vector2.zero;
        float                  dMagnitude = d.magnitude;
        RaycastResult          result     = new RaycastResult();
        List <IntegerCollider> collided   = new List <IntegerCollider>();
        bool endReached  = false;
        int  chunksSoFar = 1;

        while (true)
        {
            if (soFar.magnitude >= this.RaycastChunkSize * chunksSoFar)
            {
                // Recalculate chunk
                halfwayPoint       = new IntegerVector(chunkD / 2.0f) + position;
                rangeVector        = new IntegerVector(Mathf.RoundToInt(Mathf.Abs(chunkD.x) + 2.55f), Mathf.RoundToInt(Mathf.Abs(chunkD.y) + 2.55f));
                possibleCollisions = this.GetCollidersInRange(new IntegerRect(halfwayPoint, rangeVector), passThroughMask | haltMask);
                foreach (IntegerCollider collider in collided)
                {
                    possibleCollisions.Remove(collider);
                }
                ++chunksSoFar;
            }

            projected.x += incX;
            projected.y += incY;

            if (projected.magnitude > dMagnitude)
            {
                incX       = d.x - soFar.x;
                incY       = d.y - soFar.y;
                endReached = true;
            }

            positionModifier.x += incX;
            int move = (int)positionModifier.x;

            positionModifier.x -= move;
            int unitDir = Math.Sign(move);

            while (move != 0)
            {
                IntegerVector checkPos  = new IntegerVector(position.X + unitDir, position.Y);
                GameObject    collision = this.CollidePointFirst(checkPos, possibleCollisions);
                if (collision)
                {
                    IntegerCollider collider = collision.GetComponent <IntegerCollider>();
                    possibleCollisions.Remove(collider);
                    RaycastCollision hit = new RaycastCollision();
                    hit.CollidedObject = collision;
                    hit.CollisionPoint = position;
                    hit.CollidedX      = true;
                    if (((1 << collision.layer) & passThroughMask) != 0)
                    {
                        passThroughCollisions.Add(hit);
                        collided.Add(collider);
                    }
                    else
                    {
                        result.Collisions    = new RaycastCollision[1];
                        result.Collisions[0] = hit;
                    }
                }

                position = checkPos;

                if (result.Collided)
                {
                    break;
                }

                move -= unitDir;
            }

            if (result.Collided)
            {
                break;
            }

            positionModifier.y += incY;
            move = (int)positionModifier.y;

            positionModifier.y -= move;
            unitDir             = Math.Sign(move);

            while (move != 0)
            {
                IntegerVector checkPos  = new IntegerVector(position.X, position.Y + unitDir);
                GameObject    collision = this.CollidePointFirst(checkPos, possibleCollisions);
                if (collision)
                {
                    IntegerCollider collider = collision.GetComponent <IntegerCollider>();
                    possibleCollisions.Remove(collider);
                    RaycastCollision hit = new RaycastCollision();
                    hit.CollidedObject = collision;
                    hit.CollisionPoint = position;
                    hit.CollidedY      = true;
                    if (((1 << collision.layer) & passThroughMask) != 0)
                    {
                        passThroughCollisions.Add(hit);
                        collided.Add(collider);
                    }
                    else
                    {
                        result.Collisions    = new RaycastCollision[1];
                        result.Collisions[0] = hit;
                    }
                }

                position = checkPos;

                if (result.Collided)
                {
                    break;
                }

                move -= unitDir;
            }

            if (result.Collided || endReached)
            {
                break;
            }

            soFar.x = projected.x;
            soFar.y = projected.y;
        }

        result.FarthestPointReached = position;
        return(result);
    }
    public RaycastResult RaycastFirst(IntegerVector origin, Vector2 direction, float range = 100000.0f, int mask = Physics2D.DefaultRaycastLayers, string objectTag = null, List <IntegerCollider> possibleCollisions = null)
    {
        Vector2 d      = direction * range;
        Vector2 chunkD = range <= this.RaycastChunkSize ? d : direction * this.RaycastChunkSize;

        IntegerVector halfwayPoint        = new IntegerVector(chunkD / 2.0f) + origin;
        IntegerVector rangeVector         = new IntegerVector(Mathf.RoundToInt(Mathf.Abs(chunkD.x) + 2.5f), Mathf.RoundToInt(Mathf.Abs(chunkD.y) + 2.5f));
        bool          usingGivenPossibles = possibleCollisions != null;

        if (!usingGivenPossibles)
        {
            possibleCollisions = this.GetCollidersInRange(new IntegerRect(halfwayPoint, rangeVector), mask);
        }

        Vector2       positionModifier = Vector2.zero;
        IntegerVector position         = origin;

        float incX = d.x;
        float incY = d.y;

        if (Mathf.Abs(incX) > RAYCAST_MAX_POSITION_INCREMENT || Mathf.Abs(incY) > RAYCAST_MAX_POSITION_INCREMENT)
        {
            Vector2 dNormalized = d.normalized * RAYCAST_MAX_POSITION_INCREMENT;
            incX = dNormalized.x;
            incY = dNormalized.y;
        }

        Vector2       projected   = Vector2.zero;
        Vector2       soFar       = Vector2.zero;
        float         dMagnitude  = d.magnitude;
        RaycastResult result      = new RaycastResult();
        bool          endReached  = false;
        int           chunksSoFar = 1;

        while (true)
        {
            if (soFar.magnitude >= this.RaycastChunkSize * chunksSoFar)
            {
                // Recalculate chunk
                halfwayPoint = new IntegerVector(chunkD / 2.0f) + position;
                rangeVector  = new IntegerVector(Mathf.RoundToInt(Mathf.Abs(chunkD.x) + 2.55f), Mathf.RoundToInt(Mathf.Abs(chunkD.y) + 2.55f));
                if (!usingGivenPossibles)
                {
                    this.GetCollidersInRange(new IntegerRect(halfwayPoint, rangeVector), mask, null, possibleCollisions);
                }
                ++chunksSoFar;
            }

            projected.x += incX;
            projected.y += incY;

            if (projected.magnitude >= dMagnitude)
            {
                incX       = d.x - soFar.x;
                incY       = d.y - soFar.y;
                endReached = true;
            }

            positionModifier.x += incX;
            int move = (int)positionModifier.x;

            positionModifier.x -= move;
            int unitDir = Math.Sign(move);

            while (move != 0)
            {
                IntegerVector checkPos  = new IntegerVector(position.X + unitDir, position.Y);
                GameObject    collision = this.CollidePointFirst(checkPos, possibleCollisions);
                if (collision)
                {
                    result.Collisions = new RaycastCollision[1];
                    RaycastCollision hit = new RaycastCollision();
                    hit.CollidedObject   = collision;
                    hit.CollisionPoint   = position;
                    hit.CollidedX        = true;
                    result.Collisions[0] = hit;
                }

                position = checkPos;

                if (result.Collided)
                {
                    break;
                }

                move -= unitDir;
            }

            if (result.Collided)
            {
                break;
            }

            positionModifier.y += incY;
            move = (int)positionModifier.y;

            positionModifier.y -= move;
            unitDir             = Math.Sign(move);

            while (move != 0)
            {
                IntegerVector checkPos  = new IntegerVector(position.X, position.Y + unitDir);
                GameObject    collision = this.CollidePointFirst(checkPos, possibleCollisions);
                if (collision)
                {
                    if (result.Collided)
                    {
                        result.Collisions[0].CollidedY = true;
                    }
                    else
                    {
                        result.Collisions = new RaycastCollision[1];
                        RaycastCollision hit = new RaycastCollision();
                        hit.CollidedObject   = collision;
                        hit.CollisionPoint   = position;
                        hit.CollidedY        = true;
                        result.Collisions[0] = hit;
                    }
                }

                position = checkPos;

                if (result.Collided)
                {
                    break;
                }

                move -= unitDir;
            }

            if (result.Collided || endReached)
            {
                break;
            }

            soFar.x = projected.x;
            soFar.y = projected.y;
        }

        result.FarthestPointReached = position;
        return(result);
    }