/// <summary> /// Draw an arbitary 4 sided figure from a rect 4 structure /// </summary> /// <param name="sb"></param> /// <param name="r"></param> /// <param name="c"></param> static public void drawRect4(SpriteBatch sb, Rect4 r, Color c) { drawLine(sb, c, new Vector2(r.point[0].X, r.point[0].Y), new Vector2(r.point[1].X, r.point[1].Y), 0); drawLine(sb, c, new Vector2(r.point[1].X, r.point[1].Y), new Vector2(r.point[2].X, r.point[2].Y), 0); drawLine(sb, c, new Vector2(r.point[2].X, r.point[2].Y), new Vector2(r.point[3].X, r.point[3].Y), 0); drawLine(sb, c, new Vector2(r.point[3].X, r.point[3].Y), new Vector2(r.point[0].X, r.point[0].Y), 0); }
/// <summary> /// Copy constructor /// </summary> /// <param name="r"></param> public Rect4(Rect4 r) // Copy Constructor { point = new Vector2[4]; point[0].X = r.point[0].X; point[0].Y = r.point[0].Y; point[1].X = r.point[1].X; point[1].Y = r.point[1].Y; point[2].X = r.point[2].X; point[2].Y = r.point[2].Y; point[3].X = r.point[3].X; point[3].Y = r.point[3].Y; }
/// <summary> /// A fairly fast but not totally accurate collision system for rect 4 structures /// Simply checks that the corner points in one are inside the outline of the other /// </summary> /// <returns></returns> public static bool collisionRect4Rect4Points(Rect4 r0, Rect4 r1) { // bool rc = false; Rectangle rr0 = r0.getAABoundingRect(); Rectangle rr1 = r1.getAABoundingRect(); if (!rr0.Intersects(rr1)) { return(false); // they cant colide the aa's dont collide } // if (rr0.Intersects(rr1)) return true; // they cant colide the aa's dont collide if (insidePoly(r1.point[0], r0.point, 4)) { return(true); } if (insidePoly(r1.point[1], r0.point, 4)) { return(true); } if (insidePoly(r1.point[2], r0.point, 4)) { return(true); } if (insidePoly(r1.point[3], r0.point, 4)) { return(true); } if (insidePoly(r0.point[0], r1.point, 4)) { return(true); } if (insidePoly(r0.point[1], r1.point, 4)) { return(true); } if (insidePoly(r0.point[2], r1.point, 4)) { return(true); } if (insidePoly(r0.point[3], r1.point, 4)) { return(true); } return(false); }
/// <summary> /// Construct from Rect4 clockwise winding /// </summary> /// <param name="r"></param> public Polygon12(Rect4 r) { point = new Vector2[maxNumOfPoints]; numOfPoints = 4; point[0].X = r.point[0].X; point[0].Y = r.point[0].Y; point[1].X = r.point[1].X; point[1].Y = r.point[1].Y; point[2].X = r.point[2].X; point[2].Y = r.point[2].Y; point[3].X = r.point[3].X; point[3].Y = r.point[3].Y; }
/// <summary> /// A Slower accurate collision system for rect 4 structures /// checks for line intersections and point containment which i think is totally accurate /// </summary> /// <returns></returns> public static bool collisionRect4Rect4Lines(Rect4 r0, Rect4 r1) { // bool rc = false; Rectangle rr0 = r0.getAABoundingRect(); Rectangle rr1 = r1.getAABoundingRect(); if (!rr0.Intersects(rr1)) { return(false); // they cant colide the aa's dont collide } if (insidePoly(r1.point[0], r0.point, 4)) { return(true); } if (insidePoly(r0.point[0], r1.point, 4)) { return(true); } for (int i = 0; i < 3; i++) { // construct a line for r0 point i to j int j = i + 1; if (j > 3) { j = 0; } Vector2 cp = new Vector2(0, 0); int rc0 = intersect2D(ref cp, r0.point[i], r0.point[j], r1.point[0], r1.point[1]); if (rc0 != 3) { return(true); } int rc1 = intersect2D(ref cp, r0.point[i], r0.point[j], r1.point[1], r1.point[2]); if (rc1 != 3) { return(true); } int rc2 = intersect2D(ref cp, r0.point[i], r0.point[j], r1.point[2], r1.point[3]); if (rc2 != 3) { return(true); } int rc3 = intersect2D(ref cp, r0.point[i], r0.point[j], r1.point[3], r1.point[0]); if (rc3 != 3) { return(true); } } return(false); }