Create fluid polygons
Hello everybody.
So heres my situation: I have a bunch of GameObjects with Rigidbodies on them, just flying through space. They should look like water or some other fluid that is beeing shot from some hose.
Kinda like this:
Now I want to connect them, so that I can give the fluid a mesh and a collider. This is all 2D of course.
I have tried a convex polygon which results in the blue polygon.
Thats great and all, but not what im looking for. Notice how the lower side of the fluid is completely ignored. Id like to achieve something like the green one.
Do you guys know any algorithms for this or a place where I can look for them? I have already searched around quite a bit but cant seem to find anything.
Thanks in advance :D
This in not an algorithm per se, just an idea: How about searching for convex hull points not in all points, but just for points close to the current one?
Ye, I tried that.
Problem is that if there is no point close enough, the system just cancels the search and loops back to the first point. So i and up with this:
And if I increase the distance a point can have, so this doesnt happen, I just get a convex polygon again :/
I see... how about first finding the convex hull, then for each edge, find the closest point and see if the angle to this point is close enough to the one to the convex edge. If yes (from both sides) switch the edge with the 2 that connect to the point. Repeat until no point is found that meets the criterium. Depending on the threshold you'd get a very "spiky" shape w/ a high threshold or stay at the convex shape with a low threshold.
Answer by Baroque · Apr 15, 2017 at 06:58 PM
There are a couple of approaches depending on how accurate vs fast you need the algorithm to be. It may not be immediately obvious but there a huge variety of possible bounding polygons: research Alpha Shapes for the full nitty-gritty details.
You could explore algorithms like Delaunay Triangulation (slow!). I would search the edge of the graph for the next vertex that minimizes the angle between the previous generated edge. Consider that the two lines below are perfectly valid "outer edges" for a concave shape:
Alternatively, if you just want something that looks cool then I'd go completely the other direction and abandon polygons entirely. Look into "2D metaballs" for an approach that works well and on modern hardware is very cheap to evaluate.
Essentially you'd render a circular blob at each point using additive blending into a render texture. This results in a blobby-looking field representing a function of the distance to the nearest point. You can then draw that render texture using a threshold filter, giving you solid pixels anywhere the distance is below a certain value.
Here's an example (it uses XNA but the approach would work in Unity): http://nullcandy.com/2d-metaballs-in-xna/
Here's a DirectX example of the same approach: http://developer.download.nvidia.com/presentations/2010/gdc/Direct3D_Effects.pdf
Aaaaaaaaaalright...
So I have already tried multiple aproaches to this issue in the meantime and didnt find anything good so I'll just look into this metaball thing :D
Thanks alot friend, you helped a great deal :)