如何从形式的边界进行碰撞。 [英] how to make collision from boundaries of form.
问题描述
我在Windows窗体应用程序中制作了两个相同大小的圆圈,然后以一种永远不会碰撞的方式移动它们,现在我想让圆圈从Form的边界反映出如何以简单的方式完成。
private void button11_Click(object sender,EventArgs e)
{
Pen myPen = new Pen(Color) .Black);
Pen mypen2 =新笔(Color.Black);
图形formGraphics;
formGraphics = this.CreateGraphics();
int x = 100,y = 100,x1 = 200,y1 = 200;
for(int i = 2; i< ; 200; i ++)
{
this.Refresh();
formGraphics = this.CreateGraphics();
formGraphics.DrawEllipse(myPen,new Rectangle(x + i,y / i,100,100));
formGraphics.DrawEllipse(myPen,new Rectangle(x1 / i,y1 + i, 100,100));
System.Threading.Thread.Sleep(22);
}
参考:在c#中弹跳球[ ^ ]
检测边界碰撞和反射的代码如下:xC + = xDelta; // move ball
yC + = yDelta;
if ((xC + radius > = ClientSize.Width)||(xC - radius < = 0 )) // 检查墙壁命中
xDelta = -xDelta;
if ((yC + radius > = ClientSize.Height)||(yC - radius < = 0 ))
yDelta = -yDelta;
I have made two circles of same size on windows form application and then moved them in such a way that they will never be collided and now i want circles to reflect from boundaries of Form how this could be done in a easy way.
private void button11_Click(object sender, EventArgs e)
{
Pen myPen = new Pen(Color.Black);
Pen mypen2 = new Pen(Color.Black);
Graphics formGraphics;
formGraphics = this.CreateGraphics();
int x=100, y=100,x1=200,y1=200;
for (int i = 2; i < 200; i++)
{
this.Refresh();
formGraphics = this.CreateGraphics();
formGraphics.DrawEllipse(myPen, new Rectangle(x + i, y/ i, 100, 100));
formGraphics.DrawEllipse(myPen, new Rectangle(x1 / i, y1 + i, 100, 100));
System.Threading.Thread.Sleep(22);
}
Refer:bouncing ball in c#[^]
The code for detecting boundary collision and reflection is reproduced as follows:xC += xDelta; //move ball yC += yDelta; if ((xC+radius >= ClientSize.Width) || (xC - radius <= 0)) //check for wall hits xDelta = -xDelta; if ((yC+radius >= ClientSize.Height) || (yC - radius <= 0)) yDelta = -yDelta;
这篇关于如何从形式的边界进行碰撞。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!