# Metaballs

This topic is 3068 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Okay, so I'm writing a program to render 2D metaballs, and I've basically used the same algorithm from the tutorial on this site. I'm using the box method to cut down on number of pixels to do the calculation for, but it still runs really slow.

I'm currently using the R/r^2 equation to calculate the force, however this is not much of an improvement and I also don't like how it makes the balls mich less 'fluid'. I also tried using the Gaussian curve a*e^(-b*r^2), playing around with different values for a and b but haven't gotten it to work. Additionally I tried a formula that Wikipedia mentioned, (1-r^2)^2 but that didn't work either.

Does anyone know what I could be doing wrong or a formula that might work better? Any help would be appreciated.

##### Share on other sites
If you're adding up the circle values efficiently(only in the bounding box of the circle and not allowing for negative values), and doing the grid for the pixel drawing efficiently then it might just be that the way you're drawing the pixels to the screen is poorly implemented. I know if I tell java to fillRect on individual pixels it'll crawl, and it works much better to buffer an image as an integer array and work on that and then draw it all at one time. I can't give any useful advice on that without knowing more about the environment you're rendering in.

As far as other functions you could use, any polynomial could work, also cosine. Really any smooth curve that is 1 at a distance of 0 and 0 at 1 can be scaled to the height and range you want. Most recently I've been using 2x^3 - 3x^2 + 1 . It's a 1D cubic hermite spline that has derivatives of 0 at both ends.

##### Share on other sites
Okay, I've got to be missing something here. When you use 2r^3 - 3r^2 + 1, you`ve got to return 0 for all r > 1. But to do that check, you still have to use a squareroot, since r = sqrt((x - x0)^2 + (y - y0)^2), which kind of defeats the purpose, doesn't it? I tried using this anyway just to see if it would work, but it resulted in very small dot-sized balls, and messing around with the threshold and ball strength didn't seem to make any difference.

What you said about rendering the pixels makes sense and I am looking into that now.

##### Share on other sites
sqrt((x - x0)^2 + (y - y0)^2) > 1

iff

(x - x0)^2 + (y - y0)^2 > 1

##### Share on other sites
Alright, so that's why I'm not a mathematician. But I'm still getting tiny dot-sized balls though. Could you tell me what values you're using for the strength and the threshold, Alrecenk? Thanks.

##### Share on other sites
I haven't actually written anything with meta-balls. I use wavelets for procedural terrain and texture generation, and that's where that cubic function comes from. It's quick to calculate and it's differentiable, which was important for my application(and I tend to think is a nice property to have in general). As for finding strength and thresh-hold I would put in keyboard commands to increase and decrease those values and also print them to the console every time they are changed. Then you can fiddle with it until it looks right.

##### Share on other sites
I had some free time today so I threw together a little meta-ball demo to make sure I wasn't full of crap. www.alrecenk.com/stuff/metaballdemo.jar

I used balls with radius 30 pixels and a strength of 255 with the cubic function I mentioned before. I didn't implement all the optimizations and it seems to hold an OK framerate. A threshhold range of 100 to 120 works pretty well. If your balls are too small then your thresh-hold range is probably too high.

Also, to use that cubic function does require a square-root be calculated though it doesn't have to be done until after the distance check and it doesn't seem to effect performance all that much. Though I did come up with another polynomial x^4 - 2x^2 + 1 which does not require that square root (as there are no odd powers of distance) and still has derivatives of zero at the center and edge. Visually I can't tell the difference between the two polynomials, so the quartic is probably a better choice since it's just one multiplication, one addition, and one subtraction. Anywho, if you still need help, I've got more xp and source code now.

1. 1
2. 2
3. 3
Rutin
19
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633659
• Total Posts
3013207
×