Search code examples
c#timercollision-detectiondrawrectangledrawellipse

Detect Collision between a Rectangle and an Ellipse


hey there I am new to this work! I have successfully create a Rectangle and a Circle. I am moving the circle in my Form and the rectangle with my Arrow Keys(left & right)! The problem is that I just need to know how can detect the collision between these two so that can I print:

  Console.WriteLine("COLLISION OCCURS!");

I only want the detection of the circle and the rectangle. When my circle goes out of bounds Y-axis I show up a Message (CIRCLE WENT OUT OF BOUNDS)! What I have been trying is:

  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();
        }
    }
}

can somebody help me please! Thanks in advance


Solution

  • This is the answer I got myself!

    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();
            }
        }
    }