Exemple #1
0
        public static bool raycastCapsule(VInt3 from, VInt3 to, VInt3 p0, VInt3 p1, VFixedPoint radius, ref VInt3 hitNormal, ref VFixedPoint t)
        {
            t = VFixedPoint.One;
            VInt3       capPos  = (p0 + p1) * VFixedPoint.Half;
            VInt3       capsDir = p0 - p1;
            VFixedPoint kW      = capsDir.sqrMagnitude;

            if (kW < Globals.EPS2)
            {
                t         = VFixedPoint.Zero;
                hitNormal = VInt3.zero;
                if (SphereRaytestAlgorithm.rayTestSphere(from, to, capPos, radius, ref hitNormal, ref t))
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }

            VFixedPoint tmp = VFixedPoint.Zero;

            if (Distance.distancePointSegmentSquared(p0, p1, from, ref tmp) < radius * radius)
            {
                return(false);
            }

            VFixedPoint tmpT         = VFixedPoint.One;
            VInt3       tmpNormal    = VInt3.zero;
            bool        hitCyclinder = IntersectSegmentCylinder(from, to, p0, p1, radius, ref tmpNormal, ref tmpT);

            if (hitCyclinder)
            {
                hitNormal = tmpNormal;
                t         = tmpT;
            }

            bool hitSphere1 = SphereRaytestAlgorithm.rayTestSphere(from, to, p0, radius, ref tmpNormal, ref tmpT);

            if (hitSphere1 && tmpT < t)
            {
                t         = tmpT;
                hitNormal = tmpNormal;
            }
            bool hitSphere2 = SphereRaytestAlgorithm.rayTestSphere(from, to, p1, radius, ref tmpNormal, ref tmpT);

            if (hitSphere2 && tmpT < t)
            {
                t         = tmpT;
                hitNormal = tmpNormal;
            }

            return(hitCyclinder || hitSphere1 || hitSphere2);
        }
Exemple #2
0
        static bool testRayVsSphereOrCapsule(ref VFixedPoint impactDistance, bool testSphere, VInt3 center, VFixedPoint radius, VInt3 dir, VFixedPoint length, VInt3[] verts, int e0, int e1)
        {
            if (testSphere)
            {
                VFixedPoint t = VFixedPoint.Zero; VInt3 tmp = VInt3.zero;
                if (SphereRaytestAlgorithm.rayTestSphere(center, dir * length + center, verts[e0], radius, ref tmp, ref t))
                {
                    impactDistance = t;
                    return(true);
                }
            }
            else
            {
                VFixedPoint t = VFixedPoint.Zero; VInt3 tmp = VInt3.zero;
                if (CapsuleRaytestAlgorithm.raycastCapsule(center, dir * length + center, verts[e0], verts[e1], radius, ref tmp, ref t))
                {
                    impactDistance = t;
                    return(true);
                }
            }

            return(false);
        }