Jump to content

How to calculate bounds of a rounded rectangle?


nonzerozen
 Share

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?

Thanks!

Link to comment
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:

i.e.

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

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).

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...