hyades.physics.Body.Contains C# (CSharp) 메소드

Contains() 공개 메소드

public Contains ( Vector2 &point ) : bool
point Vector2
리턴 bool
        public bool Contains(ref Vector2 point)
        {
            // basic idea: draw a line from the point to a point known to be outside the body.  count the number of
            // lines in the polygon it intersects.  if that number is odd, we are inside.  if it's even, we are outside.
            // in this implementation we will always use a line that moves off in the positive X direction from the point
            // to simplify things.
            Vector2 endPt = new Vector2();
            endPt.X = aabb.max.X + 0.1f;
            endPt.Y = point.Y;

            // line we are testing against goes from pt -> endPt.
            bool inside = false;
            Vector2 edgeSt = pointmass_list[0].position;
            Vector2 edgeEnd = new Vector2();
            for (int i = 0; i < count; i++)
            {
                // the current edge is defined as the line from edgeSt -> edgeEnd.
                if (i < (count - 1))
                    edgeEnd = pointmass_list[i + 1].position;
                else
                    edgeEnd = pointmass_list[0].position;

                // perform check now...
                if (((edgeSt.Y <= point.Y) && (edgeEnd.Y > point.Y)) || ((edgeSt.Y > point.Y) && (edgeEnd.Y <= point.Y)))
                {
                    // this line crosses the test line at some point... does it do so within our test range?
                    float slope = (edgeEnd.X - edgeSt.X) / (edgeEnd.Y - edgeSt.Y);
                    float hitX = edgeSt.X + ((point.Y - edgeSt.Y) * slope);

                    if ((hitX >= point.X) && (hitX <= endPt.X))
                        inside = !inside;
                }
                edgeSt = edgeEnd;
            }

            return inside;
        }

Usage Example

예제 #1
0
        public bool IsPointInsideAnyBody(Vector2 point)
        {
            for (int i = 0; i < body_list.Count; i++)
            {
                Body body = body_list[i];

                // broad-phase collision via AABB.
                if (!body.aabb.Contains(point.X, point.Y))
                {
                    continue;
                }

                if (body.Contains(ref point))
                {
                    return(true);
                }
            }

            return(false);
        }
All Usage Examples Of hyades.physics.Body::Contains