wpf c#如何进行碰撞检测?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16132903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do c# collision detection?
提问by user1866990
Are there any predefined methods in c# which allow for collision detection?
c# 中是否有任何允许碰撞检测的预定义方法?
I am new to c# and am trying to get collision detection of two ellipses are there any predefined ways collision detection can be implemented?
我是 C# 新手,正在尝试对两个椭圆进行碰撞检测,是否有任何预定义的方法可以实现碰撞检测?
I already have code which draws the ellipses, what would be a good way to start the collision detection?
我已经有了绘制椭圆的代码,启动碰撞检测的好方法是什么?
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
};
}
this is the code to draw an ellipse which then gets removed and appears in another position.
这是绘制椭圆的代码,然后将其删除并出现在另一个位置。
回答by David
I have tested this, it worked, at least for me
我已经测试过了,它有效,至少对我来说


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!");
回答by Lukazoid
Would something like the following work?
会像下面这样工作吗?
var ellipse1Geom = ellipse1.RenderedGeometry;
var ellipse2Geom = ellipse2.RenderedGeometry;
var detail = ellipse1Geom.FillContainsWithDetail(ellipse2Geom);
if(detail != IntersectionDetail.Empty)
{
// We have an intersection or one contained inside the other
}
The Geometry.FillContainsWithDetail(Geometry)method is defined as
该Geometry.FillContainsWithDetail(Geometry)方法定义为
Returns a value that describes the intersection between the current geometry and the specified geometry.
返回一个值,该值描述当前几何图形和指定几何图形之间的交集。
回答by codingadventures
I think you should definitely give a look at the XNA framework, it has loads of method to do collision detection.
我认为你绝对应该看看XNA 框架,它有很多方法来进行碰撞检测。
Check out this other linkon how to implement it manually in c# it might be helpful.
查看有关如何在 c# 中手动实现它的其他链接,它可能会有所帮助。
回答by Clemens
Provided that your Ellipses are always circles (i.e. their Widthand Heightproperties are set to the same value) and they always have the Canvas.Leftand Canvas.Topproperties set, the following helper method checks for a collision:
假设您的椭圆始终是圆形(即它们的Width和Height属性设置为相同的值)并且它们始终设置了Canvas.Left和Canvas.Top属性,以下辅助方法会检查碰撞:
public static bool CheckCollision(Ellipse e1, Ellipse e2)
{
var r1 = e1.ActualWidth / 2;
var x1 = Canvas.GetLeft(e1) + r1;
var y1 = Canvas.GetTop(e1) + r1;
var r2 = e2.ActualWidth / 2;
var x2 = Canvas.GetLeft(e2) + r2;
var y2 = Canvas.GetTop(e2) + r2;
var d = new Vector(x2 - x1, y2 - y1);
return d.Length <= r1 + r2;
}

