首页 > 解决方案 > UWP 非矩形对象对撞机检测器

问题描述

我需要知道两个通用对象是否相交。我尝试创建两个相同大小的对象的矩形,并使用它工作的相交方法,但是,这种方法在椭圆等对象上并不准确。

有没有一种方法可以告诉我两个非矩形物体是否相交?

GeneralTransform t1 = this.object1.TransformToVisual(this);
GeneralTransform t2 = this.object2.TransformToVisual(this);

Rect r1 = t1.TransformBounds(new Rect() { X = 0, Y = 0, Width = this.object1.ActualWidth, Height = this.object1.ActualHeight });
Rect r2 = t2.TransformBounds(new Rect() { X = 0, Y = 0, Width = this.object2.ActualWidth, Height = this.object2.ActualHeight });

bool result = r1.IntersectsWith(r2);

标签: c#uwpuwp-xaml

解决方案


您可以使用HitTestCore. 请参阅该方法的文档,您需要接受的重载GeometryHitTestParameters

编辑:你得到第二个对象的几何形状,并用它调用'HitTestCore'。


推荐阅读