Ejemplo n.º 1
0
        public bool RayCast(Ray2D ray, out RayCastResult result)
        {
            bool existResult = m_root.RayCast(ray, out result);

            // check leaf not in world
            foreach (var leaf in overFlowLeaf)
            {
                if (ray.Intersects(leaf.Zone, out Vector2 hitPoint) &&
                    (existResult == false || Vector2.DistanceSquared(ray.Original, hitPoint) < Vector2.DistanceSquared(ray.Original, result.Intersection)))
                {
                    existResult = true;
                    result      = new RayCastResult(hitPoint, leaf);
                }
            }

            return(existResult);
        }
Ejemplo n.º 2
0
        public bool RayCast(Ray2D ray, out RayCastResult result)
        {
            bool existResult = false;

            result = new RayCastResult();
            Queue <QuadTreeNode> queue = new Queue <QuadTreeNode>();

            queue.Enqueue(this);

            Vector2 hitPoint;

            while (queue.Count != 0)
            {
                var leaf = queue.Dequeue();

                // 当此节点 Zone 与所给 RectangleF 不相交时返回 null
                if (ray.Intersects(leaf.m_zone, out hitPoint) == false)
                {
                    continue;
                }

                // 查看是否与此节点内的 item 有覆盖
                foreach (var item in leaf.managedItems)
                {
                    if (ray.Intersects(item.Zone, out hitPoint) &&
                        (existResult == false || Vector2.DistanceSquared(ray.Original, hitPoint) < Vector2.DistanceSquared(ray.Original, result.Intersection)))
                    {
                        existResult = true;
                        result      = new RayCastResult(hitPoint, item);
                    }
                }

                // 查找子节点
                for (int i = 0; i < 4; i++)
                {
                    if (leaf.childern[i] != null && ray.Intersects(leaf.childern[i].Zone, out hitPoint))
                    {
                        queue.Enqueue(leaf.childern[i]);
                    }
                }
            }

            return(existResult);
        }