如何实现简单的2d球碰撞?

时间:2014-01-26 22:22:32

标签: c# graphics drawing physics collision

我一直在为这个课程开展这个项目,我只想提出一些如何前进的建议。基本上,我的项目是在按钮点击时在表格上绘制一个球(每个按钮点击添加一个额外的球)。这个球应该从模板的边缘反弹并且还与其他球碰撞。

我已经完成了其他一切;现在我正试图让球碰撞起作用。它不必是物理上准确的或任何东西;现在我很难让他们认识对方。这是我的代码(如果代码很草率,请提前道歉。我最近对我的所有课程都非常分散,而且我几乎没有时间来完成这项工作,所以我的思考过程可能会有所不同碱):

    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;

                            }
                    }

但我还没弄清楚如何避免球检查并“翻转”检测开关。我也不认为我的“检测开关”首先正常工作!任何人都可以伸出援助之手吗?

1 个答案:

答案 0 :(得分:2)

当它们的中心距离是它们的半径之和时,两个圆相互相切。

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

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

如果(r1 + r2)^ 2&lt; = d ^ 2,则发生碰撞。