nonzerozen Posted January 8, 2017 Share Posted January 8, 2017 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! Quote Link to comment Share on other sites More sharing options...
mattstyles Posted January 9, 2017 Share Posted January 9, 2017 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). Quote Link to comment Share on other sites More sharing options...
nonzerozen Posted January 14, 2017 Author Share Posted January 14, 2017 Thanks for your reply! I'll give it a go! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.