Example #1
0
        public static bool      MeshIntersection(Mesh m1, Mesh m2, ref List <ContactPoint> contacts)
        {
            Vector2[] v1 = m1.verticesWorldSpace;
            Vector2[] v2 = m2.verticesWorldSpace;

            v1 = SortPolyClockwise(v1);
            v2 = SortPolyClockwise(v2);

            Vector2 a0, a1, b0, b1;

            bool r = false;

            ContactPoint cp = new ContactPoint();

            for (int i = 0; i < v1.Length; ++i)
            {
                a0 = v1[i];
                a1 = v1[i == v1.Length - 1 ? 0 : i + 1];

                for (int j = 0; j < v2.Length; ++j)
                {
                    b0 = v2[j];
                    b1 = v2[j == v2.Length - 1 ? 0 : j + 1];

                    if (!LineIntersection(a0, a1, b0, b1, ref cp))
                    {
                        continue;
                    }

                    r = true;
                    contacts.Add(cp);
                }
            }

            return(r);
        }
Example #2
0
        public static bool      CircleIntersection(Vector2 center, float radius, Vector2 point, Vector2 dir, ref ContactPoint result)
        {
            //Line start is within circle
            if ((center - point).LengthSquared < radius * radius)
            {
                return(false);
            }

            Vector2 nDir   = dir.Normalized();
            Vector2 nPoint = Vector2.Dot(nDir, point - center) * nDir;
            float   nDist  = (nPoint - center).LengthSquared;
            float   sqrRad = radius * radius;

            if (nDist > sqrRad) //NO INTERSECTION
            {
                return(false);
            }

            nDist = (float)Math.Sqrt(sqrRad - nDist);

            result.point  = nPoint - dir * nDist;
            result.normal = (result.point - center).Normalized();

            return(true);
        }
Example #3
0
        public static bool      LineIntersection(Vector2 a0, Vector2 a1, Vector2 b0, Vector2 b1, ref ContactPoint cp)
        {
            float d0 = DistanceFromPlaneDir(a0 - a1, a0, b0);
            float d1 = DistanceFromPlaneDir(a0 - a1, a0, b1);

            //both points are on either side of the line normal
            if ((d0 > 0 && d1 > 0) || (d0 < 0 && d1 < 0))
            {
                return(false);
            }

            Vector2 x = Lerp(b0, b1, Math.Abs(d0) / (Math.Abs(d0) + Math.Abs(d1)));
            Vector2 c = Lerp(a0, a1, 0.5f);

            d1 = (a0 - a1).Length / 2;

            //Distance from center of line a
            d0 = DistanceFromPlaneNor(a0 - a1, c, x);
            d0 = Math.Abs(d0);

            //If distance from line center is greater than its half extent there was no intersection
            if (d0 > d1)
            {
                return(false);
            }

            cp = new ContactPoint(x, (a1 - a0).GetNormal().Normalized());

            return(true);
        }