- Home /
How do I detect all gameobjects inside a defined irregular area defined by other gameobjects?
Hi guys, I am making a snake-like game. I have a head and multiple body parts that move similar to slither.io. The movement of snake is also similar to slither.io.
My game mechanic is different, where i encourage players to collide with their own snake body. When they do so, an irregular circle is made. Is there any possible way to obtain the all the game objects within that "irregular circle?"
I am somehow unable to attach a picture, please view the picture here : https://drive.google.com/drive/folders/1WYj6fcnrsFsrddQaxS-jGzrB3QL7uK9P?usp=sharing
You could try to raycast from each bodypart to it's opposite body part (excluding the bodyparts that are not part of the circle (you can check this by adding all body parts from head to the bodypart it collided with to a list and excluded the once left behind). Then you would technically have the entire circle filled with raycast lines, then u can see if any collided with an object. I don't know how performant this would be, just a concept.
Answer by Bunny83 · Feb 17, 2020 at 03:26 PM
Yes, just use PolyContainsPoint.
What this actually does is doing is testing each triangle that is formed with the origin if it contains the point or not. Since we toggle the inside state each time we find another triangle we get the proper result. This is the same reason why summing the signed area of all those triangles give you the right surface area. See my gif animations
So if a point falls in a region that is actually outside the polygon, the point would be inside two triangles (one positive, one negative) so it is properly detected as being outside.
I've posted those images in a comment on this question about calculating the area of a polygon.
Thank you for your idea :) Although it didn't match 100% (because of me not explaining the question properly), it definitely got me in the right direction! For those wondering, What i did was to use all the body game objects to create a mesh (polygon), where i then attached a mesh collider to detect anything inside that polygon!
(This is my fault because I failed to mention that there are multiple objects inside and outside the polygon, which I believe would be more efficient to use the collider ins$$anonymous$$d of checking each object if they are in or out of the polygon (I may have 100+ objects to check!)
Thank you again :)
This is exactly what i was looking for, i needed to create a abstract area and verify if a given position in world is inside that area and this PolyContainsPoint solve that problem. Thanks for share.
Your answer
