Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 nistic · Mar 27, 2014 at 02:04 AM · trailsurround

How to check if an object is surrounded?

alt text

Hi! My team is making a game where you have the ability to lay down a trail, which is the blue line in the image above. We want to implement the ability to complete a level once the trail surrounds an orb (the blue sphere in the middle). Right now the trail is created by constantly instantiating texture planes at the player's position, and so I can check if the player has completed a trail by checking the collision.

I need to check if the trail surrounds the orb, somehow. Any ideas?

I have a solution in mind, but I have no idea how to implement it. I could accomplish this if I could somehow draw out a shape that updates along with the trail, and check if the orb is within that drawn out shape. Like this: alt text

I just can't think of any other solution, or even how to execute this one... help, anyone?

Comment
Add comment · Show 3
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 Crazydadz · Mar 27, 2014 at 03:56 AM 0
Share

$$anonymous$$aybe keep 4 last positions of player each time he changed direction, create a rect with them and check if the position of the orb is inside it with something like Rect.Contains

avatar image robertbu · Mar 27, 2014 at 07:47 AM 1
Share

@Crazydadz's solution is reasonable one given your drawings. If you want something more general, keep track of the point on the path walked. After the path is closed, walk each segment in order. Test which side of the segment the object is on. If for all segments, the object is on the same side, then the point tested is surrounded. You can test which side using the SignedDotProduct() in the $$anonymous$$ath3D script in the Unity Wiki.

avatar image mattyman174 · Mar 27, 2014 at 12:04 PM 0
Share

If the end shape is not a Rectangle and is irregular in some way, try looking into a FloodFill.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by NoseKills · Mar 27, 2014 at 12:30 PM

I once did a game like this, and although it was grid based (which made the problem both easier and more difficult at the same time :P) I found the easiest solution to be the "winding number algorithm". alt text

I.E. isolate the part of the trail that formed the loop. "Draw" a line from outside the trail along the x-axis at the same level as the orb. Check how many rising and falling edges you hit on your way to the orb.

If you need more info, Googling "point inside polygon" and "winding number algorithm" might even lead you to some code samples :)

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 wibble82 · Mar 27, 2014 at 12:36 PM

This is basically an 'is point inside polygon' test as far as I can tell. The polygon is the 2d set of line segments defined by your path.

I couldn't quite tell from description, but depending on your scenario:

  • if you can guaruntee the shape created is convex then the test is very easy. You can find the algorithm for 'is point inside convex polygon' online. It basically consists of taking each line segment defined by your player's path and testing which side of that line the point in question is on. If you're on the same side for every line, then you're inside the polygon

  • if the shape is concave its a bit more complex but various algorithms exist for it. The most basic is to write your self a little ray vs line segment test. Take a point that's definitely outside the polygon (such as the left most point minus a bit), and imagine a line from there to the target point. If it crosses 1 line segment the point is inside. If it crosses 2, its outside. If it crosses 3, its inside... you get the idea :)

-Chris

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

24 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

Related Questions

Drawing 2d line from a point on an object! 1 Answer

How to create Trail Like Angry birds 1 Answer

Trail Renderer which ignores parent movement 0 Answers

Weapon Trails in Local Space 0 Answers

Raycast visible bullets 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