Collision detection between a circle and rectangle
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Collision detection between a circle and rectangle

  1. #1
    Join Date
    Apr 2012
    Posts
    7

    Collision detection between a circle and rectangle

    Hi,
    I need to implement collision detection algorithm for the small game I'm developing. The objects i have here is ball/circle and a rectangle. The balls are moving vertically, while the rectangle is moving horizontally. The condition is that if the ball and rectangle touch each other then an event should be raised. I have been trying to do that for a while without success. This is my first program in C# so please bear with me.

    Here is my code for the two objects that i want to check their collision

    Code:
    class Ball
        {        
            public float x, y, yvel, radius;
            public Brush brush;
            public Ball(int gamewidth,int gameHeight,Random r)
            {
               
                x = r.Next(gamewidth);
                y = r.Next(gameHeight);
    
                yvel = r.Next(2) + 5;
    
                radius = r.Next(10) + 5;
                brush = new SolidBrush(Color.Blue);            
            }
          
            public void Move(int gameHeight)
                           
            {               
                if (y + radius >= gameHeight)
                {
                    y =  0;             
                }
                y += yvel;
            }
            public void Draw(Graphics g)
            {
                g.FillEllipse(brush, new RectangleF(x-radius,y - radius, radius * 2, radius * 2));
            }
        }
    
    class Player
        {
            
            public float x, y, xvel;
            public Brush brush;
    
            public Player()
            {
            
            }
    
            public void DrawPlayer(Graphics g)
            {
                
                g.FillRectangle(brush, new RectangleF(x, y, 30,30));       
            }
    
            public void MovePlayerLeft(int gameWidth)
            {
                if (x > 0)
                {
                    x -= xvel;
                }
            }
       
    
        }
    Last edited by JeffB; April 29th, 2012 at 07:26 PM. Reason: Added code tag

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Collision detection between a circle and rectangle

    Well, I don't see any collision logic at all there, but it should be as simple as checking the center of the circle + the radius in relation to the bounds of the rectangle, no? Get the circle's center point and use the distance formula to check a line from the center of teh circle to the edge against the rectangle bounds.
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  3. #3
    Join Date
    Apr 2012
    Posts
    7

    Re: Collision detection between a circle and rectangle

    Quote Originally Posted by BigEd781 View Post
    Well, I don't see any collision logic at all there, but it should be as simple as checking the center of the circle + the radius in relation to the bounds of the rectangle, no? Get the circle's center point and use the distance formula to check a line from the center of the circle to the edge against the rectangle bounds.
    Sorry to bother you again. How is the distance formula like? Can you please show me how the distance is calculated

  4. #4
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: Collision detection between a circle and rectangle

    1) Rectangle or Square?
    2) Are all Rectangles/Squares the same size?
    3) Are the circles always the same size?
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  5. #5
    Join Date
    Apr 2012
    Posts
    7

    Re: Collision detection between a circle and rectangle

    Quote Originally Posted by rliq View Post
    1) Rectangle or Square?
    2) Are all Rectangles/Squares the same size?
    3) Are the circles always the same size?
    Hi,

    Sorry for not making myself clear. Need to check the contact between the square(fixed size) and the circles(varying size) but i can make the circle fixed size if need be. I hope I'm clear. Thank you.

  6. #6
    Join Date
    Feb 2012
    Location
    Strasbourg, France
    Posts
    116

    Re: Collision detection between a circle and rectangle

    Collision is always a funny topic.
    Usally we detect collisins by putting objects into easily comparable shapes. If it were 2 circles it would be just checking if the distance between the 2 center is lower or equal to the sum of their radius.

    You can do it simple by assuming that the rectangle is a circle but that would pose problems on faces.

    Or you can do it the hard way :

    http://stackoverflow.com/questions/4...n-intersection

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center