Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by KOKOStern · Oct 09, 2019 at 10:26 AM · 2dpolygons

How to keep and grow a polygonal area in 2D

Working on a 2D game and I have a player 'base'. Think of a strategy game where you have your buildings in an area. Now I want my NPCs to be able to choose a random location within the confines of this base and walk around it - think of an idle task where they move around.

Here's the problem - I can't just use a rect for this base since the player can construct buildings and essentially extend the 'shape' of the base in any way. I need something more accurate that I can also extend.

A polygon (list of points) seem like a good solution, but I have a bunch of questions on how to do this:

  • I just built a new building, how do I grow my polygon?

I thought about finding the 2 closest points to this new structure and adding a point behind it - thus adding a triangle shape to my polygon. Problem is this can quickly get outta hand and has some crappy edge cases (start with a rect and draw it on a piece of paper using circles as buildings. You'll reach a point where it looks horrible with concave shapes).

  • With any solution for the above problem I will need to find a way to 'trim' the polygon and make sure it's not concave and doesn't grow too big in terms of points (the actual size doesn't matter).

  • In this 'base' there will be unique locations (perimeter is important for defenders, farming structures important for farming NPCs). How should I approach this problem? Should I make the base definition be constructed of smaller bases so that I 'create' bases on the fly in regards to my specific needs? For example I now need my blacksmith area and farming area combined, so I create an area constructed of both. How do I go about doing that?

This gets super complicated and I'm in too deep and wondering if I'm missing a much simpler solution.

Any ideas? How would you approach this?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bonfire-Boy · Oct 09, 2019 at 10:38 AM 0
Share

Sounds like a job for Nav$$anonymous$$eshes to me. Have you looked into them at all?

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by dweiss · Oct 09, 2019 at 12:31 PM

For a single convexed polygon to envelop the base:

I would add the points to the current polygon I have and than create a convex polygon from the new points and save the new Polygon.

https://www.geeksforgeeks.org/convex-hull-set-1-jarviss-algorithm-or-wrapping/amp/

I would wait on reducing the polygon until i see the results of a lot of test cases

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KOKOStern · Oct 09, 2019 at 08:32 PM 0
Share

This looks like a great solution. Each building added will require a recalculation but we're not talking about anything massive and we're not adding a million buildings.

I'm still not sure how to denote specific areas inside but I might just use lists of buildings for every type of area and work something out when it comes to it's 'area'.

Thank you!

avatar image dweiss KOKOStern · Oct 09, 2019 at 09:25 PM 0
Share

You could check distamce from one polygon to another and if it's lower then Delta attach otherwise, create new polygon.

You will need to adjust the algorithm to accumulate multiple polygons and select who (or even how many) to join with the new building

avatar image
0

Answer by dobbersp · Oct 09, 2019 at 11:18 AM

Some thoughts: I'd look to maintain separate rectangle lists. 1 list for each set of important regions, such as a farms, and then another list for the base footprint.

For the base footprint, you'd merge/trim rectangles whenever you add a new structure/zone. You'd need to make design decisions about the rules that govern your establishment. For example, can buildings/zones overlap existing ones? Do new buildings/zones need to be adjacent to existing ones?

  • The new rectangle can be fully overlapped by 1 or more existing rectangles. If this is the case, nothing needs to be added fo the footprint rectangles list.

  • The new rectangle can be partially overlapped by 1 or more existing rectangles. For this case, you would split the new rectangle and only add the portion that is not overlapping.

  • The new rectangle could align with an existing rectangle (if it shares an edge with an existing rectangle) where you would merge it into the other rectangle.

  • The new rectangle could not overlap or share an edge with any existing rectangles, and you can determine if you want to discard it as an invalid base placement (making an exception if it's your first rectangle) or allow for non-adjacent rectangles in your base footprint (probably tricky for perimeter patrols that way though)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by MisterKidX · Oct 09, 2019 at 06:41 PM

'+1 Jarvis

This has a mathematical solution to it, and it is called... Drum roll... Ray Casting! Take the point you wish to test against your polygon. Draw a ray from it in any direction, although for simplicity sake make it horizontal. Check the times it intersects your polygon - this is an easy test done with line segments intersection, like 9th grade. If you are using a horizontal line then you can compare the points' Y values. If it intersects your polygon even times it is outside of it. If odd times then it is inside. If it is outside you can add it to your polygon's list of points in the right location, effectively making it larger.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

228 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2d game performance is bad in mobile, but giving 80 to 130 fps in pc, its physics game, help plz 2 Answers

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

2D Animation does not start 1 Answer

Need help with creating a 2D polygon from a series of GameObjects in C# 3 Answers

Add collision to raycasted object? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges