Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7507941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:35:13+00:00 2026-05-29T22:35:13+00:00

I have a ball bouncing on my screen and there is a static rectangle

  • 0

I have a ball bouncing on my screen and there is a static rectangle that it can collide with and should bounce off of. I have already figured out how to test if the ball has collided with the rectangle and that works great. Now I need to determine which side of the rectangle that the ball has hit. I am currently trying this code (which works for testing the four sides but seems to have problems with the corners)…

if(Math.abs(ball.centerY-boundingBox.top) < ball.radius) {
    // Hit the top

}
else if(Math.abs(ball.centerY-boundingBox.bottom) < ball.radius) {
    // Hit the bottom

}
else if(Math.abs(ball.centerX-boundingBox.left) < ball.radius) {
    // Hit the left

}
else if(Math.abs(ball.centerX-boundingBox.right) < ball.radius) {
    // Hit the right

}

… Does anyone have any ideas how I can improve this? Or come up with a better solution for that matter?

I just basically need to determine which side a circle has hit on a rectangle after they collide. And I have already figured out how to test whether they collide or not.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:35:14+00:00Added an answer on May 29, 2026 at 10:35 pm

    It presumably doesn’t work for corners because when the ball hits a corner, it hits two sides simultaneously. And if you’re looking to make it bounce accurately, the relevant normal vector is that from the centre of the ball to the corner, which is going to be some diagonal between horizontal and vertical.

    Assuming you always detect overlap while the centre of the ball is outside the rectangle, what you probably want to do is something like:

    // is the ball above the box?
    if(Math.abs(ball.ballCenterY-boundingBox.top) < ball.radius)
    {
        if(ball.ballCentreX >= boundingBox.left)
        {
             if(ball.ballCentreY <= boundingBox.right)
             {
                 // ball hit the top edge
             }
             else
             {
                 // ball hit the top right corner
             }
        }
        else
        {
            // hit top left corner
        }
    }
    

    A better test — to handle both inside and outside collisions — would be to find distance to the closest point on each side, pick the smallest distance, then if closest point is a corner then its a corner collision, otherwise it’s a side collision.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written an app that basically shows a bouncing ball on the screen.
I have an object tree that looks something like Ball / \ LegalBall IllegalBall
I have an Object, Ball, which belongs_to a Girl, which can have_many balls. Everything
I have set of players, and a ball, the players can kick the ball
Ok, i made a small application with a small ball bouncing on the screen.
I'm building a game in as3 that has balls moving and bouncing off the
i have been trying to make a bouncing ball animation.I have got everything right
I have coded an animation (in python) for a beach ball to bounce around
In chipmunk/cocos2d, I have the classic bouncing ball demo happily running with an arbitrary
I have a ball that triggers an action when it collides with a sprite.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.