Example #1
0
        public override bool Raycast(Ray2 ray, float distance, out RaycastResult result)
        {
            result = new RaycastResult();

            Vec2 delta = ray.origin - transform.position;

            // Since  length of ray direction is always 1, therefore a = 1
            float b = 2 * Vec2.Dot(ray.direction, delta);
            float c = delta.SqrMagnitude - radius * radius;

            float d = b * b - 4 * c;

            if (d < 0)
            {
                return(false);
            }

            float t;

            if (d < Mathf.Epsilon)
            {
                t = -b / 2;
            }
            else
            {
                d = (float)Math.Sqrt(d);
                t = (-b - d) / 2;
            }

            result.point    = ray.origin + ray.direction * t;
            result.distance = t;
            result.normal   = (result.point - transform.position).Normalized;

            return(t <= distance);
        }
Example #2
0
        public override bool Raycast(Ray2 ray, float distance, out RaycastResult result)
        {
            result = new RaycastResult();

            float tmin      = Ray2.Tmax;
            int   crossings = 0;

            for (int i = 0; i < VertexCount; i++)
            {
                float t;
                int   j = (i + 1) % VertexCount;

                Vec2 a = transform.LocalToWorldPosition(vertices[i]);
                Vec2 b = transform.LocalToWorldPosition(vertices[j]);

                if (ray.IntersectSegment(a, b, distance, out t))
                {
                    crossings++;
                    if (t < tmin && t <= distance)
                    {
                        tmin = t;

                        result.point    = ray.origin + ray.direction * tmin;
                        result.normal   = transform.LocalToWorldDirection(normals[i]);
                        result.distance = tmin;
                    }
                }
            }

            // Point in polygon test, to make sure that origin isn't inside polygon
            return(crossings > 0 && crossings % 2 == 0);
        }
Example #3
0
        bool IBroadphase.Raycast(Ray2 ray, float distance, out RaycastResult result)
        {
            result = new RaycastResult();
            float tmin = Ray2.Tmax;

            Queue <Node> q = new Queue <Node>();

            if (root != null)
            {
                q.Enqueue(root);
            }

            while (q.Count > 0)
            {
                Node node = q.Dequeue();

                if (node.bounds.Raycast(ray, distance))
                {
                    if (node.IsLeaf)
                    {
                        RaycastResult tempResult;
                        if (node.body.shape.Raycast(ray, distance, out tempResult))
                        {
                            if (tempResult.distance < tmin)
                            {
                                result = tempResult;
                                tmin   = tempResult.distance;
                            }
                        }
                    }
                    else
                    {
                        q.Enqueue(node.child0);
                        q.Enqueue(node.child1);
                    }
                }
            }

            return(tmin < Ray2.Tmax);
        }
Example #4
0
        public bool Raycast(Ray2 ray, float distance = Ray2.Tmax)
        {
            float tminX = (min.x - ray.origin.x) / ray.direction.x;
            float tmaxX = (max.x - ray.origin.x) / ray.direction.x;

            float tminY = (min.y - ray.origin.y) / ray.direction.y;
            float tmaxY = (max.y - ray.origin.y) / ray.direction.y;

            float tmin = Math.Max(Math.Min(tminX, tmaxX), Math.Min(tminY, tmaxY));
            float tmax = Math.Min(Math.Max(tminX, tmaxX), Math.Max(tminY, tmaxY));

            if (tmax < 0)
            {
                return(false);
            }

            if (tmax < tmin)
            {
                return(false);
            }

            return(true);
        }
Example #5
0
 bool IBroadphase.Raycast(Ray2 ray, float distance, out RaycastResult result)
 {
     result = new RaycastResult();
     return(false);
 }
Example #6
0
 public bool Raycast(Ray2 ray, float distance, out RaycastResult result)
 {
     return(broadphase.Raycast(ray, distance, out result));
 }
Example #7
0
 public bool Raycast(Ray2 ray, out RaycastResult result)
 {
     return(Raycast(ray, Ray2.Tmax, out result));
 }