检测矩形和椭圆之间的碰撞

本文关键字:之间 碰撞 检测 | 更新日期: 2023-09-27 17:53:52

大家好,我是新手!我已经成功地创建了一个矩形和一个圆。我用方向键(左&右)!问题是,我只需要知道如何检测这两者之间的冲突,以便我可以打印:

  Console.WriteLine("COLLISION OCCURS!");

我只想检测圆和矩形。当我的圆越过y轴时,我显示一条消息(circle WENT out of bounds)!我一直在尝试的是:

  using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Paddle_Test
{
    public partial class Form1 : Form
    {
        Rectangle rec;
        Rectangle circle;
        int wLoc=0;
            int hLoc=0;
            int eWL;
            int eHL;
            int dx = 4;
            int dy = 4;
        public Form1()
        {
            InitializeComponent();
            wLoc=(this.Width) - 100;
            hLoc=(this.Height) - 100;
            eWL = 10;
            eHL = 10;
            rec = new Rectangle(wLoc,hLoc , 60, 10);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Refresh();
        }
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.FillRectangle(new SolidBrush(Color.Blue), rec);
            g.FillEllipse(new SolidBrush(Color.Red), eWL, eHL, 40, 40);
        }
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Left)
            {
                rec.X -= 30;
                this.Refresh();
            }
            if (e.KeyCode==Keys.Right)
            {
                rec.X += 30;
                this.Refresh();
            }
        }
        private void timer_Tick(object sender, EventArgs e)
        {
            eWL = eWL + dx;
            eHL = eHL + dy;
            if (eWL >= (this.Width) - 100)
            {
                dx = dx * (-1);
            }
            else if (eHL >= (this.Height) - 100)
            {
                dy = dy * (-1);
                //timer.Enabled = false;
                //MessageBox.Show("Game Over!");
            }
            else if (eWL<=0)
            {
                 dx = dx * (-1);
            }
            else if (eHL <= 0)
            {
                dy = dy * (-1);
            }
            else if (eWL == rec.X || eHL == rec.Y)   //here I'm trying to detect the collision!
            {
                dx = dx * (-1);//invert the direction x-axis
                dy = dy * (-1);//invert the direction y-axis

            }
            this.Refresh();
        }
    }
}
谁能帮帮我?提前感谢

检测矩形和椭圆之间的碰撞

这是我自己得到的答案!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Paddle_Test
{
    public partial class Form1 : Form
    {
        Rectangle rec;
        Rectangle circle;
        int wLoc=0;
            int hLoc=0;
            int eWL;
            int eHL;
            int dx = 2;
            int dy = 2;
        public Form1()
        {
            InitializeComponent();
            wLoc=(this.Width) - 100;
            hLoc=(this.Height) - 100;
            eWL = 10;
            eHL = 10;
            rec = new Rectangle(wLoc,hLoc , 100, 10);
            circle = new Rectangle(eWL, eHL, 40, 40);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Refresh();
        }
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.FillRectangle(new SolidBrush(Color.Blue), rec);
            g.FillEllipse(new SolidBrush(Color.Red), circle);
        }
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Left)
            {
                rec.X -= 30;
                this.Refresh();
            }
            if (e.KeyCode==Keys.Right)
            {
                rec.X += 30;
                this.Refresh();
            }
        }
        int count=0;
        private void timer_Tick(object sender, EventArgs e)
        {
            circle.X += dx;
            circle.Y += dy;
            count += 1;
            if (circle.X >= (this.Width) - 100)
            {
                dx = dx * (-1);
            }
            else if (circle.Y >= (this.Height))
            {
                //dy = dy * (-1);
                timer.Enabled = false;
                MessageBox.Show("Game Over!");
            }
            else if (circle.X <= 0)
            {
                dx = dx * (-1);
            }
            else if (circle.Y <= 0)
            {
                dy = dy * (-1);
            }
            else if (rec.IntersectsWith(circle))//detects collision!
            {
                //dx = dx * (-1);
                dy = dy * (-1);
                Console.WriteLine("hello");
            }
            this.Refresh();
        }
    }
}