gpt4 book ai didi

c# - 如何进行 C# 碰撞检测?

转载 作者:行者123 更新时间:2023-11-30 19:28:17 27 4
gpt4 key购买 nike

C# 中是否有任何预定义的方法可以进行碰撞检测?

我是 c# 的新手,正在尝试对两个椭圆进行碰撞检测,是否有任何预定义的方法可以实现碰撞检测?

我已经有了绘制椭圆的代码,开始碰撞检测的好方法是什么?

private void timer1_Tick(object sender, EventArgs e)
{
//Remove the previous ellipse from the paint canvas.
canvas1.Children.Remove(ellipse);

if (--loopCounter == 0)
timer.Stop();

//Add the ellipse to the canvas
ellipse = CreateAnEllipse(20, 20);
canvas1.Children.Add(ellipse);

Canvas.SetLeft(ellipse, rand.Next(0, 500));
Canvas.SetTop(ellipse, rand.Next(0, 310));
}

// Customize your ellipse in this method
public Ellipse CreateAnEllipse(int height, int width)
{
SolidColorBrush fillBrush = new SolidColorBrush() { Color = Colors.Yellow};
SolidColorBrush borderBrush = new SolidColorBrush() { Color = Colors.Black };

return new Ellipse()
{
Height = height,
Width = width,
StrokeThickness = 1,
Stroke = borderBrush,
Fill = fillBrush
};
}

这是绘制一个椭圆然后被移除并出现在另一个位置的代码。

最佳答案

我已经测试过了,它有效,至少对我来说是这样

enter image description here

var x1 = Canvas.GetLeft(e1);
var y1 = Canvas.GetTop(e1);
Rect r1 = new Rect(x1, y1, e1.ActualWidth, e1.ActualHeight);


var x2 = Canvas.GetLeft(e2);
var y2 = Canvas.GetTop(e2);
Rect r2 = new Rect(x2, y2, e2.ActualWidth, e2.ActualHeight);

if (r1.IntersectsWith(r2))
MessageBox.Show("Intersected!");
else
MessageBox.Show("Non-Intersected!");

关于c# - 如何进行 C# 碰撞检测?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16132903/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com