我如何实现简单的2D球碰撞



我一直在这个项目上工作,我只是想要一些有关如何前进的建议。基本上,我的项目是按钮单击在表单上绘制一个球(每个按钮单击都会添加一个额外的球)。该球应该从表格边缘弹起,并与其他球相撞。

我已经完成了其他所有工作;现在,我正试图使球碰撞起作用。它不必身体准确或任何东西;现在,我只是难以让他们互相认识。这是我的代码(如果代码很草率,请提前道歉。最近我的所有课程都非常分散,我几乎无法挤进时间来完成此操作,所以我的思考过程可能会差不多基础):

    namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
            int i = 0;
            int X;
            int Y;
            bool bounceX;
            bool bounceY;
            System.Drawing.Graphics g;
            List<Ball> ball = new List<Ball>();
            System.Timers.Timer timer = new System.Timers.Timer(16);
            public Form1()
            {
                InitializeComponent();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                g = this.CreateGraphics();
                timer.Elapsed += new ElapsedEventHandler(timer_Tick);
            }
            private void button1_Click(object sender, EventArgs e)
            {
                Ball curball = new Ball();
                ball.Add(curball);
                ball[i].defineball(X, Y, bounceX, bounceY);
                Random r = new Random();
                X = r.Next(1, this.Width - 1);
                Y = r.Next(1, this.Height - 1);
                bounceX = r.Next(0, 2) > 0;
                bounceY = r.Next(0, 2) > 0;
                timer.Enabled = true;
                i++;
            }
            private void timer_Tick(object source, ElapsedEventArgs e)
            {
                   g.Clear(this.BackColor);
                   curball.newball(g);
                }
            }
        }
    }
   class Ball : Form1
    {
        int radius = 25;
        int X;
        int Y;
        bool bounceX;
        bool bounceY;
        public bool detect = false;
        public void defineball(int X, int Y, bool bounceX, bool bounceY)
        {   
            this.X = X;
            this.Y = Y;
            this.bounceX = bounceX;
            this.bounceY = bounceY;
    }
        public void newball(System.Drawing.Graphics g)
        {
            if (detect != true)
            {
                if (X >= this.Width - 50)
                {
                    bounceX = true;
                }
                else if (X <= 25)
                {
                    bounceX = false;
                }
                if (Y >= this.Height - 50)
                {
                    bounceY = true;
                }
                else if (Y <= 25)
                {
                    bounceY = false;
                }
            }
            if (bounceX == false)
            {
                X++;
            }
            else if (bounceX == true)
            {
                X--;
            }
            if (bounceY == false)
            {
                Y++;
            }
            else if (bounceY == true)
            {
                Y--;
            }
            Pen clsPen = Pens.Black;
            Color color = Color.Black;
            SolidBrush brush = new SolidBrush(color);
            g.DrawEllipse(clsPen, X - 20, Y - 30, 50, 50);
            g.FillEllipse(brush, X - 20, Y - 30, 50, 50);
            this.detect = false;
        }
    }
}

这是我有点走的方向:

 foreach (var curball in ball)
                {
                  // how do i get these stupid balls to detect each other? 
                  // research the math of circles and such
                    foreach (var checkball in ball)
                    {
                            if (curball.X - checkball.X <= 25
                                && curball.X - checkball.X >= -25
                                && curball.Y - checkball.Y <= 25
                                && curball.Y - checkball.Y >= -25
                                && curball.detect != true)
                            {
                                //bool tempX = curball.bounceX;
                                //bool tempY = curball.bounceY;
                                //curball.bounceX = checkball.bounceX;
                                //curball.bounceY = checkball.bounceY;
                                checkball.bounceX = !checkball.bounceX;
                                checkball.bounceY = !checkball.bounceX;
                                checkball.detect = true;
                                curball.detect = true;
                            }
                    }

但是我还没有真正弄清楚如何避免球检查自己并"翻转"检测开关。我也认为我的"检测开关"首先无法正常工作!谁能伸出援手?

当中心的距离是其半径的总和时,两个圆圈彼此相切。

计算球(3D)或圆(2D)之间的距离。如果两个中心之间的距离等于或小于两个半径的总和,则两个圆圈碰撞并需要相互反弹。

d^2 =(x2-x1)^2 (y2-y1)^2

if(r1 r2)^2&lt; = d^2您有碰撞。

相关内容

  • 没有找到相关文章

最新更新