/// <summary> /// calculates intersection to another rectangle /// </summary> /// <param name="rect">rectangle to check for intersection</param> /// <returns>true if intersection exists</returns> public bool IntersectsRectangle(Rectangle rect) { centerToCenter = rect.center - this.center; //find nearest edge 1 dotVertical = GameVector.Dot(forward, centerToCenter); dotHorisontal = GameVector.Dot(Right, centerToCenter); if (Math.Abs(dotVertical) > Math.Abs(dotHorisontal)) { nearestedgeIsVertical1 = true; nearest1edgeIsPositive1 = dotVertical > 0; nearest2edgeIsPositive1 = dotHorisontal > 0; } else { nearestedgeIsVertical1 = false; nearest1edgeIsPositive1 = dotHorisontal > 0; nearest2edgeIsPositive1 = dotVertical > 0; } //create nearest edge as line if (nearestedgeIsVertical1) { if (nearest1edgeIsPositive1) nearestedge1 = new Line(forwardLeft, forwardRight); else nearestedge1 = new Line(backLeft, backRight); } else { if (nearest1edgeIsPositive1) nearestedge1 = new Line(forwardRight, backRight); else nearestedge1 = new Line(forwardLeft, backLeft); } // cut rect if it is fully on one side of this rectangle Pt111CanItersect = nearestedge1.AreOnOneSide(center, rect.backLeft); Pt121CanItersect = nearestedge1.AreOnOneSide(center, rect.backRight); Pt131CanItersect = nearestedge1.AreOnOneSide(center, rect.forwardLeft); Pt141CanItersect = nearestedge1.AreOnOneSide(center, rect.forwardRight); if (!Pt111CanItersect && !Pt121CanItersect && !Pt131CanItersect && !Pt141CanItersect) return false; //find nearest edge2 dotVertical = -GameVector.Dot(rect.forward, centerToCenter); dotHorisontal = -GameVector.Dot(rect.Right, centerToCenter); if (Math.Abs(dotVertical) > Math.Abs(dotHorisontal)) { nearestedgeIsVertical2 = true; nearest1edgeIsPositive2 = dotVertical > 0; nearest2edgeIsPositive2 = dotHorisontal > 0; } else { nearestedgeIsVertical2 = false; nearest1edgeIsPositive2 = dotHorisontal > 0; nearest2edgeIsPositive2 = dotVertical > 0; } //create nearest edge as line if (nearestedgeIsVertical2) { if (nearest1edgeIsPositive2) nearestedge2 = new Line(rect.forwardLeft, rect.forwardRight); else nearestedge2 = new Line(rect.backLeft, rect.backRight); } else { if (nearest1edgeIsPositive2) nearestedge2 = new Line(rect.forwardRight, rect.backRight); else nearestedge2 = new Line(rect.forwardLeft, rect.backLeft); } // cut this rect if it is fully on one side of rect Pt211CanItersect = nearestedge2.AreOnOneSide(rect.center, backLeft); Pt221CanItersect = nearestedge2.AreOnOneSide(rect.center, backRight); Pt231CanItersect = nearestedge2.AreOnOneSide(rect.center, forwardLeft); Pt241CanItersect = nearestedge2.AreOnOneSide(rect.center, forwardRight); if (!Pt211CanItersect && !Pt221CanItersect && !Pt231CanItersect && !Pt241CanItersect) return false; //form next nearest edge of this rectangle if (!nearestedgeIsVertical1) { if (nearest2edgeIsPositive1) nearestedge1 = new Line(forwardLeft, forwardRight); else nearestedge1 = new Line(backLeft, backRight); } else { if (nearest2edgeIsPositive1) nearestedge1 = new Line(forwardRight, backRight); else nearestedge1 = new Line(forwardLeft, backLeft); } // cut rect if it is fully on one side of this rectangle Pt112CanItersect = nearestedge1.AreOnOneSide(center, rect.backLeft); Pt122CanItersect = nearestedge1.AreOnOneSide(center, rect.backRight); Pt132CanItersect = nearestedge1.AreOnOneSide(center, rect.forwardLeft); Pt142CanItersect = nearestedge1.AreOnOneSide(center, rect.forwardRight); if (!Pt112CanItersect && !Pt122CanItersect && !Pt132CanItersect && !Pt142CanItersect) return false; //create next nearest edge as line if (!nearestedgeIsVertical2) { if (nearest2edgeIsPositive2) nearestedge2 = new Line(rect.forwardLeft, rect.forwardRight); else nearestedge2 = new Line(rect.backLeft, rect.backRight); } else { if (nearest2edgeIsPositive2) nearestedge2 = new Line(rect.forwardRight, rect.backRight); else nearestedge2 = new Line(rect.forwardLeft, rect.backLeft); } // cut this rect if it is fully on one side of rect Pt212CanItersect = nearestedge2.AreOnOneSide(rect.center, backLeft); Pt222CanItersect = nearestedge2.AreOnOneSide(rect.center, backRight); Pt232CanItersect = nearestedge2.AreOnOneSide(rect.center, forwardLeft); Pt242CanItersect = nearestedge2.AreOnOneSide(rect.center, forwardRight); if (!Pt212CanItersect && !Pt222CanItersect && !Pt232CanItersect && !Pt242CanItersect) return false; return ((Pt111CanItersect && Pt112CanItersect) || (Pt121CanItersect && Pt122CanItersect) || (Pt131CanItersect && Pt132CanItersect) || (Pt141CanItersect && Pt142CanItersect) || (Pt211CanItersect && Pt212CanItersect) || (Pt221CanItersect && Pt222CanItersect) || (Pt231CanItersect && Pt232CanItersect) || (Pt241CanItersect && Pt242CanItersect) ); }
public void DrawLine(Line Line,MiniGameInterfaces.Color Color) { if (CRectanglesInBatch < MaxBatchSize / 2) { MiniGameInterfaces.Rectangle rect=new MiniGameInterfaces.Rectangle((Line.pt1+Line.pt2)*0.5f, new GameVector(Line.Length(),0.003f*CameraPosition.Z),Line.pt2-Line.pt1); DrawRectangle(rect, Color); } }