我有一些检测碰撞的代码;
public bool DetectCollision(ContentControl ctrl1, ContentControl ctrl2)
{
Rect ctrl1Rect = new Rect(
new Point(Convert.ToDouble(ctrl1.GetValue(Canvas.LeftProperty)),
Convert.ToDouble(ctrl1.GetValue(Canvas.TopProperty))),
new Point((Convert.ToDouble(ctrl1.GetValue(Canvas.LeftProperty)) + ctrl1.ActualWidth),
(Convert.ToDouble(ctrl1.GetValue(Canvas.TopProperty)) + ctrl1.ActualHeight)));
Rect ctrl2Rect = new Rect(
new Point(Convert.ToDouble(ctrl2.GetValue(Canvas.LeftProperty)),
Convert.ToDouble(ctrl2.GetValue(Canvas.TopProperty))),
new Point((Convert.ToDouble(ctrl2.GetValue(Canvas.LeftProperty)) + ctrl2.ActualWidth),
(Convert.ToDouble(ctrl2.GetValue(Canvas.TopProperty)) + ctrl2.ActualHeight)));
ctrl1Rect.Intersect(ctrl2Rect);
return !(ctrl1Rect == Rect.Empty);
}它会检测到两个矩形何时结束。在给定的参数ContentControls中有图像。我希望能够检测这些图像是否与矩形相交。下面的图片显示了我想要的东西;


发布于 2014-05-07 22:13:21
那么你要找的不是矩形碰撞检测,而是像素级的碰撞检测,这将是更多的处理密集型。
在已经实现的矩形碰撞检测的基础上,您必须检查重叠矩形区域中两个图像的每个像素。
在最简单的情况下,如果两个重叠的像素都有不透明的颜色,那么就会发生碰撞。
如果要使事情复杂化,您可能需要添加阈值,例如:要求重叠像素的百分比才能触发冲突;或者为像素的组合alpha级别设置阈值,而不是使用任何非零值。
发布于 2014-05-07 22:16:57
可以尝试将图像转换为几何体对象,然后可以检查它们是否正确碰撞。但这些图像应该是矢量图像。要将图像转换为矢量图像,您可以查看这个开源project.
public static Point[] GetIntersectionPoints(Geometry g1, Geometry g2)
{
Geometry og1 = g1.GetWidenedPathGeometry(new Pen(Brushes.Black, 1.0));
Geometry og2 = g2.GetWidenedPathGeometry(new Pen(Brushes.Black, 1.0));
CombinedGeometry cg = new CombinedGeometry(GeometryCombineMode.Intersect, og1, og2);
PathGeometry pg = cg.GetFlattenedPathGeometry();
Point[] result = new Point[pg.Figures.Count];
for (int i = 0; i < pg.Figures.Count; i++)
{
Rect fig = new PathGeometry(new PathFigure[] { pg.Figures[i] }).Bounds;
result[i] = new Point(fig.Left + fig.Width / 2.0, fig.Top + fig.Height / 2.0);
}
return result;
}https://stackoverflow.com/questions/23520077
复制相似问题