c#中是否有预定义的方法可以进行碰撞检测?如何做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
};
}
这是绘制椭圆然后被移除并出现在另一个位置的代码。
你能发表该代码的例子吗? – 2013-04-21 15:57:13
wpf不是一个游戏框架,它没有类似精灵般的功能,如碰撞检测。你可以做的最好的办法是获得两个椭圆的边界矩形,并使用'RectangleF.IntersectsWith'。否则,您将不得不计算两个椭圆的接近角度,即该角度的半径,然后查看两个半径的长度是否小于或等于两个椭圆的焦点之间的距离。 – 2013-04-21 16:05:42