How to calculate bounds of a rounded rectangle?

Recommended Posts

I'm new to game development (but not new to JavaScript web development). I'm making a 2d game where the area NPCs can move within is a rounded rectangle.

I want to supply the NPC object the bounds it can move within. If the area had straight corners it would have been an easy use of x and y.

But since the corners are rounded, how do I calculate the bounds?


Share this post

Link to post
Share on other sites

Each rounded rectangle is effectively a quarter circle, you could do a per pixel check against a map but you could also use maths to solve it.

If your rounded corners have a radius of 5 then you're going to need to check the 'roundedness' when an entity is within 5 units of each side, i.e. in the corner. This was you use the cheaper rectangle check and only do the 'in-circle' check when you need to.

You can use pythagorus to work out if a point is within a circle. In your case you need to be slightly smarter and check only when a unit is in a particular circle quadrant based on which corner you're in, but thats fairly easy:


If x < 5 && y < 5 then
  perform circle bounds check

This way you'll only get positive results because you're checking against only when [x, y] is in the correct segment (if you performed the checks all the time then a position like [9.9, 9.9] would be outside the circle but should not be blocked).

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.