【问题标题】:How to know if an object will collide with another object before adding it to the panel?在将一个对象添加到面板之前如何知道一个对象是否会与另一个对象发生碰撞?
【发布时间】:2014-01-13 08:38:52
【问题描述】:

请看下面我的代码。

我有一个尚未添加到 WPF 面板中的对象 - 在本例中是一个简单的 Canvas。当我调试“var A1 = Poly.RenderedGeometry”行时,我看到的唯一内容是{}。根本没有数据 - 没有 pathGeometry,什么都没有。

你能解释一下我的代码有什么问题吗?如何知道我要添加到画布的多边形是否不会与其他多边形发生碰撞?

// First and Second are 2 points, CreateNewTriangle adds a random point
// and creates a new triangle polygon
        var Poly = CreateNewTriangle(First, Second);

        if (G1.Children.Count == 0)
        {
            G1.Children.Add(Poly);
        }
        else
        {
         // In debug this row is empty - no actual geometry is present 
            var A1 = Poly.RenderedGeometry;

            foreach (Polygon item in G1.Children)
            {
                if (!item.Equals(Poly))
                {
                    var a2 = item.RenderedGeometry;
                    var col = A1.FillContainsWithDetail(a2);
                    if (!(col == IntersectionDetail.Empty))
                    {
                        IsAllGood = false;
                        break;
                    }
                }

            }
        }

【问题讨论】:

    标签: wpf collision-detection collision


    【解决方案1】:

    您可能不会在元素中获取任何数据,因为该元素要么不存在于屏幕上,因此没有机会渲染,要么渲染过程尚未发生。你应该做的是强制MeasureArrange 调用,之后你应该为RenderedGeometry 填充一些东西

    var Poly = CreateNewTriangle(First, Second);
    Poly.Measure(new Size(double.MaxValue, double.MaxValue));
    Poly.Arrange(new Rect(new Point(0, 0), new Size(double.MaxValue, double.MaxValue)));
    

    【讨论】:

    • 我的主窗口和画布的大小如何?使用你的代码我一直相交。忘了提到另一个对象已经在面板上并且对用户可见......
    • 在我的示例中,我允许 Measure 调用完全不受限制,这应该没问题。有问题的代码可能出现在Arrange 调用中,您应该提供更准确的Rect 与多边形的位置,而不是从具有最大尺寸的原点开始。 @StackTackTack
    猜你喜欢
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-28
    • 2021-07-14
    • 2010-11-05
    • 1970-01-01
    相关资源
    最近更新 更多