Ejemplo n.º 1
0
        // Check raycast collision

        public override RaycastHit CheckRay(Vector2 start, Vector2 direction)
        {
            Vector2[] points = GetPoints();

            Vector2 end = start + direction;

            LineHit closest = PhysicsUtils.CheckLineCollision(points[3], points[0], start, end);

            for (int i = 0; i <= 2; i++)
            {
                Vector2 p1 = points[i];
                Vector2 p2 = points[i + 1];

                LineHit hit = PhysicsUtils.CheckLineCollision(p1, p2, start, end);

                if (hit.hit)
                {
                    if (!closest.hit || Vector2.Distance(hit.hitPoint, start) < Vector2.Distance(closest.hitPoint, start))
                    {
                        closest = hit;
                    }
                }
            }

            if (!closest.hit)
            {
                return(null);
            }

            RaycastHit raycastHit = new RaycastHit(closest.hitPoint, entity, Vector2.Distance(closest.hitPoint, start));

            return(raycastHit);
        }
Ejemplo n.º 2
0
        // Find the hitpoints of two colliding polygons  via Line collision

        public Vector2[] FindPolygonHitpoints(Line[] poly1, Line[] poly2)
        {
            List <Vector2> collisionPoints = new List <Vector2>();

            foreach (Line line1 in poly1)
            {
                foreach (Line line2 in poly2)
                {
                    LineHit lineHit = PhysicsUtils.CheckLineCollision(line1.p1, line1.p2, line2.p1, line2.p2);

                    if (lineHit.hit)
                    {
                        collisionPoints.Add(lineHit.hitPoint);
                    }
                }
            }

            return(collisionPoints.ToArray());
        }