public static bool CheckCollision(UiChasseurModel ctl1, UiCibleModel ctl2, double detectX, double detectY) { bool retval = false; //Point ptTopLeft = new Point(ctl1.X, ctl1.Y); // TODO : changer ! enlever la moitier de la taille ! //Point ptBottomRight = new Point(ctl1.X + ctl1.Width - 175.0, ctl1.Y + ctl1.Height - 175.0); // 90 //Rect r1 = new Rect(ptTopLeft, ptBottomRight); Size sizeR1 = new Size(20.0, 20.0); Point centerR1 = new Point(ctl1.X + ((ctl1.Width / 2.0) - (sizeR1.Width / 2.0)), ctl1.Y + ((ctl1.Height / 2.0) - (sizeR1.Height / 2.0))); Rect r1 = new Rect(centerR1, sizeR1); //System.Diagnostics.Debug.WriteLine("+++x:" + ptTopLeft.X.ToString() + " Y " + ptTopLeft.Y.ToString() + " " + ptBottomRight.X.ToString() + " " + ptBottomRight.Y.ToString()); //Point ptTopLeft2 = new Point(ctl2.X, ctl2.Y); // TODO : changer ! enlever la moitier de la taille ! //Point ptBottomRight2 = new Point(ctl2.X + ctl2.Width - 175.0, ctl2.Y + ctl2.Height - 175.0); //Rect r2 = new Rect(ptTopLeft2, ptBottomRight2); Size sizeR2 = new Size(detectX, detectY); // 40.0 Point centerR2 = new Point(ctl2.X + ((ctl2.Width / 2.0) - (sizeR2.Width / 2.0)), ctl2.Y + ((ctl2.Height / 2.0) - (sizeR2.Height / 2.0))); Rect r2 = new Rect(centerR2, sizeR2); r1.Intersect(r2); if (!r1.IsEmpty) { retval = true; } return(retval); }
public static bool TestCollision(UiChasseurModel element1, UiCibleModel element2) { Geometry intersect = Geometry.Combine(element1.Geometry, element2.Geometry, GeometryCombineMode.Intersect, null); if (intersect.IsEmpty() == true) { // no collision - GET OUT! return(false); // return !intersect.IsEmpty(); } else { //bool bCollision = false; Point ptCheck = new Point(); // now we do a more accurate pixel hit test for (int x = Convert.ToInt32(element1.X); x < Convert.ToInt32(element1.X + element1.Width); x++) { for (int y = Convert.ToInt32(element1.Y); y < Convert.ToInt32(element1.Y + element1.Height); y++) { ptCheck.X = x; ptCheck.Y = y; //List<UIElement> hits = (List<UIElement>)System.Windows.Media.VisualTreeHelper.FindElementsInHostCoordinates(ptCheck, control1); //HitTestResult hits = System.Windows.Media.VisualTreeHelper.HitTest(element1, ptCheck); //if (hits.VisualHit == controlElem1) //{ // // we have a hit on the first control elem, now see if the second elem has a similar hit // //List<UIElement> hits2 = (List<UIElement>)System.Windows.Media.VisualTreeHelper.FindElementsInHostCoordinates(ptCheck, control2); // HitTestResult hits2 = System.Windows.Media.VisualTreeHelper.HitTest(element2, ptCheck); // if (hits.VisualHit == controlElem2) // { // //bCollision = true; // break; // } //} } //if (bCollision) // break; } //return bCollision; return(true); } }
public static bool CheckCollisionEva(UiChasseurModel ctl1, UiCibleModel ctl2) { bool retval = false; Point ptTopLeft = new Point(ctl1.X, ctl1.Y); Point ptBottomRight = new Point(ctl1.X + ctl1.Width - 45, ctl1.Y + ctl1.Height - 45); Rect r1 = new Rect(ptTopLeft, ptBottomRight); //System.Diagnostics.Debug.WriteLine("+++x:" + ptTopLeft.X.ToString() + " Y " + ptTopLeft.Y.ToString() + " " + ptBottomRight.X.ToString() + " " + ptBottomRight.Y.ToString()); Point ptTopLeft2 = new Point(ctl2.X, ctl2.Y); Point ptBottomRight2 = new Point(ctl2.X + ctl2.Width - 45, ctl2.Y + ctl2.Height - 45); Rect r2 = new Rect(ptTopLeft2, ptBottomRight2); r1.Intersect(r2); if (!r1.IsEmpty) { retval = true; } return(retval); }