如何进行c#碰撞检测?
c#中是否有允许碰撞检测的预定义方法如何进行c#碰撞检测?,c#,wpf,ellipse,C#,Wpf,Ellipse,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(); /
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
};
}
我是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
};
}
这是绘制椭圆的代码,然后椭圆被删除并显示在另一个位置。我已经测试过了,至少对我来说是有效的
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中手动实现它,这可能会有所帮助。如果您的椭圆始终是圆(即它们的
宽度和高度属性设置为相同的值),并且它们始终设置了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;
}
公共静态bool CheckCollision(椭圆e1、椭圆e2)
{
var r1=e1.实际宽度/2;
var x1=Canvas.GetLeft(e1)+r1;
var y1=Canvas.GetTop(e1)+r1;
var r2=e2.实际宽度/2;
var x2=Canvas.GetLeft(e2)+r2;
var y2=Canvas.GetTop(e2)+r2;
var d=新向量(x2-x1,y2-y1);
返回d.长度是否需要以下工作
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
}
该方法定义为
返回一个值,该值描述当前几何图形与指定几何图形之间的交点
你能举个例子吗?wpf不是一个游戏框架,它没有像碰撞检测这样类似精灵的功能。你最好得到两个椭圆的边界矩形并使用RectangleF.IntersectsWith
。否则,你必须计算两个椭圆的接近角,半径at这个角度,然后看看两个半径的长度加起来是否小于或等于椭圆的两个焦点之间的距离。xna太过分了,因为OP没有开发游戏。你帮了我一个主意!从你的回答中