Visual Studio 中 WPF C# 的用户控件移动期间的碰撞检测 [英] Collision detection during user control movement for WPF C# in Visual Studio

查看:49
本文介绍了Visual Studio 中 WPF C# 的用户控件移动期间的碰撞检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在 C# 中移动两个 UserControl(Visual Studio 中的 WPF 应用程序),我希望能够检测到两者之间的碰撞.

If I am making two UserControls move in C# (WPF application in Visual Studio), I want to be able to detect collision between the two of them.

我目前正在使用doesIntersect"方法,用一个 if 语句来显示一个消息框,上面写着碰撞!".

I currently am using the "doesIntersect" method, with an if statement to show a message box that says "Collision!."

它工作正常,但是,直到代码执行完毕,消息框才会显示,这对我的编码方式很有意义.有没有办法让消息框在它们发生碰撞时立即显示,而不是在它们已经相互经过之后?我是否必须在这里实现异步编程?

It works fine, however, the message box doesn't show until the code is done executing, which makes sense for how I have it coded. Is there any way to get the message box to show as soon as they collide and not after they have already passed each other? Would I have to implement asynchronous programming here?

我目前正在使用一种方法来移动一个对象:

I currently am using a method for movement of the one object:

    public static void MoveTo(UserControl FoodX, double X, double Y)
    {
        Vector offset = VisualTreeHelper.GetOffset(FoodX);
        var top = offset.Y;
        TranslateTransform trans = new TranslateTransform();
        FoodX.RenderTransform = trans;
        DoubleAnimation anim1 = new DoubleAnimation(0, 200, TimeSpan.FromSeconds(2));
        trans.BeginAnimation(TranslateTransform.YProperty, anim1);
    }

然后我有我的碰撞代码,我在图像后面放置了一个不可见的矩形,以便我可以使用 Rect 方法:

Then I have my collision code, where I put an invisible rect behind the image so I can use the Rect method:

    public static Rect rectbox;
    public bool Collision()
    {
        rectbox = new Rect(Canvas.GetLeft(rect1), Canvas.GetTop(rect1), rect1.Width, rect1.Height);
        bool doesIntersect = rect1.RenderedGeometry.Bounds.IntersectsWith(FoodImage.rectfood);
        return doesIntersect;
    }

推荐答案

我远不是 WPF 动画方面的专家,但由于到目前为止没有人回复......

I'm far from being an expert on WPF animations, but since nobody replied so far...

TranslateTransform 有一个 Changed 事件,当动画移动 UI 元素时会触发该事件.您可以使用事件处理程序方法订阅此事件,在您的事件处理程序中检查碰撞,如果检测到碰撞,则可以执行您想要的操作.

TranslateTransform has a Changed event that fires while the animation is moving the UI element. You can subscribe to this event with an event handler method, check for collision in your event handler, and if you detect collision, you can execute what you want.

以下代码对我有用,即当移动元素与另一个元素碰撞时立即显示消息框(我使用包含移动 UserControlCanvas 对其进行了测试)和另一个静态元素):

The following code worked for me, i.e. the message box was shown instantly when the moving element collided with another element (I tested it with a Canvas containing a moving UserControl and another, static element):

public void MoveTo(UserControl FoodX, double X, double Y)
{
    Vector offset = VisualTreeHelper.GetOffset(FoodX);
    var top = offset.Y;
    TranslateTransform trans = new TranslateTransform();
    FoodX.RenderTransform = trans;
    DoubleAnimation anim1 = new DoubleAnimation(0, 200, TimeSpan.FromSeconds(2));
    trans.BeginAnimation(TranslateTransform.YProperty, anim1);

    //Added line
    trans.Changed += RecalculateCollision;
}

public void RecalculateCollision(object sender, EventArgs e)
{            
    Rect r1 = BoundsRelativeTo(MovingBox, Canvas);
    Rect r2 = BoundsRelativeTo(StaticBox, Canvas);

    if (r1.IntersectsWith(r2))
    {
        MessageBox.Show("Collided");
    }
}

public static Rect BoundsRelativeTo(FrameworkElement element, Visual relativeTo)
{
    return element.TransformToVisual(relativeTo).TransformBounds(new Rect(element.RenderSize));
}

这篇关于Visual Studio 中 WPF C# 的用户控件移动期间的碰撞检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆