Ejemplo n.º 1
0
        public static bool Contains3DPoint(Vector3 point, bool checkOnPlane, Vector3 quadCenter, float quadWidth, float quadHeight, Vector3 quadRight,
                                           Vector3 quadUp, QuadEpsilon epsilon = new QuadEpsilon())
        {
            Plane quadPlane = new Plane(Vector3.Cross(quadRight, quadUp).normalized, quadCenter);

            if (checkOnPlane && quadPlane.GetAbsDistanceToPoint(point) > epsilon.ExtrudeEps)
            {
                return(false);
            }

            quadWidth  += epsilon.WidthEps;
            quadHeight += epsilon.HeightEps;

            Vector3 toPoint  = point - quadCenter;
            float   dotRight = toPoint.AbsDot(quadRight);
            float   dotUp    = toPoint.AbsDot(quadUp);

            if (dotRight > quadWidth * 0.5f)
            {
                return(false);
            }
            if (dotUp > quadHeight * 0.5f)
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 2
0
        public static bool Is2DPointOnBorder(Vector2 point, Vector2 quadCenter, float quadWidth, float quadHeight,
                                             float degreeRotation, QuadEpsilon epsilon = new QuadEpsilon())
        {
            Vector2 right, up;

            Calc2DQuadRightUp(degreeRotation, out right, out up);
            return(Is2DPointOnBorder(point, quadCenter, quadWidth, quadHeight, right, up, epsilon));
        }
Ejemplo n.º 3
0
        public static bool Is2DPointOnBorder(Vector2 point, Vector2 quadCenter, float quadWidth, float quadHeight, Vector2 quadRight,
                                             Vector2 quadUp, QuadEpsilon epsilon = new QuadEpsilon())
        {
            var segmentEps = new SegmentEpsilon();

            segmentEps.PtOnSegmentEps = epsilon.WireEps;

            var corners = Calc2DQuadCornerPoints(quadCenter, new Vector2(quadWidth, quadHeight), quadRight, quadUp);

            for (int ptIndex = 0; ptIndex < corners.Count; ++ptIndex)
            {
                Vector2 startPt = corners[ptIndex];
                Vector2 endPt   = corners[(ptIndex + 1) % corners.Count];
                if (SegmentMath.Is2DPointOnSegment(point, startPt, endPt, segmentEps))
                {
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 4
0
        public static bool Contains2DPoint(Vector2 point, Vector2 quadCenter, float quadWidth, float quadHeight, Vector2 quadRight,
                                           Vector2 quadUp, QuadEpsilon epsilon = new QuadEpsilon())
        {
            quadWidth  += epsilon.WidthEps;
            quadHeight += epsilon.HeightEps;

            Vector2 toPoint  = point - quadCenter;
            float   dotRight = toPoint.AbsDot(quadRight);
            float   dotUp    = toPoint.AbsDot(quadUp);

            if (dotRight > quadWidth * 0.5f)
            {
                return(false);
            }
            if (dotUp > quadHeight * 0.5f)
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 5
0
        public static OBB Calc3DQuadOBB(Vector3 quadCenter, Vector2 quadSize, Quaternion quadRotation, QuadEpsilon epsilon = new QuadEpsilon())
        {
            Vector3 size = quadSize + epsilon.SizeEps;

            size.z = epsilon.ExtrudeEps * 2.0f;
            return(new OBB(quadCenter, size, quadRotation));
        }
Ejemplo n.º 6
0
        public static bool RaycastWire(Ray ray, out float t, Vector3 quadCenter, float quadWidth, float quadHeight, Vector3 quadRight, Vector3 quadUp, QuadEpsilon epsilon = new QuadEpsilon())
        {
            t = 0.0f;
            Vector3    quadNormal   = Vector3.Normalize(Vector3.Cross(quadRight, quadUp));
            Plane      quadPlane    = new Plane(quadNormal, quadCenter);
            Vector2    quadSize     = new Vector2(quadWidth, quadHeight);
            Quaternion quadRotation = Quaternion.LookRotation(quadNormal, quadUp);

            float rayEnter;

            if (quadPlane.Raycast(ray, out rayEnter))
            {
                Vector3 intersectPt  = ray.GetPoint(rayEnter);
                var     cornerPoints = Calc3DQuadCornerPoints(quadCenter, quadSize, quadRotation);

                float distFromSegment = intersectPt.GetDistanceToSegment(cornerPoints[(int)QuadCorner.TopLeft], cornerPoints[(int)QuadCorner.TopRight]);
                if (distFromSegment <= epsilon.WireEps)
                {
                    t = rayEnter;
                    return(true);
                }

                distFromSegment = intersectPt.GetDistanceToSegment(cornerPoints[(int)QuadCorner.TopRight], cornerPoints[(int)QuadCorner.BottomRight]);
                if (distFromSegment <= epsilon.WireEps)
                {
                    t = rayEnter;
                    return(true);
                }

                distFromSegment = intersectPt.GetDistanceToSegment(cornerPoints[(int)QuadCorner.BottomRight], cornerPoints[(int)QuadCorner.BottomLeft]);
                if (distFromSegment <= epsilon.WireEps)
                {
                    t = rayEnter;
                    return(true);
                }

                distFromSegment = intersectPt.GetDistanceToSegment(cornerPoints[(int)QuadCorner.BottomLeft], cornerPoints[(int)QuadCorner.TopLeft]);
                if (distFromSegment <= epsilon.WireEps)
                {
                    t = rayEnter;
                    return(true);
                }
            }

            if (epsilon.ExtrudeEps != 0.0f)
            {
                float dot = Vector3Ex.AbsDot(ray.direction, quadPlane.normal);
                if (dot < ExtrudeEpsThreshold.Get)
                {
                    OBB quadOBB = Calc3DQuadOBB(quadCenter, quadSize, Quaternion.LookRotation(quadNormal, quadUp), epsilon);
                    return(BoxMath.Raycast(ray, quadOBB.Center, quadOBB.Size, quadOBB.Rotation));
                }
            }

            return(false);
        }
Ejemplo n.º 7
0
        public static bool Raycast(Ray ray, out float t, Vector3 quadCenter, float quadWidth, float quadHeight, Vector3 quadRight, Vector3 quadUp, QuadEpsilon epsilon = new QuadEpsilon())
        {
            t = 0.0f;
            Vector3 quadNormal = Vector3.Normalize(Vector3.Cross(quadRight, quadUp));
            Plane   quadPlane  = new Plane(quadNormal, quadCenter);

            float rayEnter;

            if (quadPlane.Raycast(ray, out rayEnter) &&
                Contains3DPoint(ray.GetPoint(rayEnter), false, quadCenter, quadWidth, quadHeight, quadRight, quadUp, epsilon))
            {
                t = rayEnter;
                return(true);
            }

            if (epsilon.ExtrudeEps != 0.0f)
            {
                float dot = Vector3Ex.AbsDot(ray.direction, quadPlane.normal);
                if (dot < ExtrudeEpsThreshold.Get)
                {
                    OBB quadOBB = Calc3DQuadOBB(quadCenter, new Vector2(quadWidth, quadHeight), Quaternion.LookRotation(quadNormal, quadUp), epsilon);
                    return(BoxMath.Raycast(ray, quadOBB.Center, quadOBB.Size, quadOBB.Rotation));
                }
            }

            return(false);
        }