Metaballs script

Hmm... I can get a new metaball to be created nearby the last created metaball... would that work?

Only spheres have field equations yet; the other types are not ready for testing

The field equations are all based on finding the distance r for the scalar field. The distance is normalized per the prop scales, so the metaprop can have a limit at 1.
So, let's see.... easiest case, a line on X direction, going from 1 to +1. The r is
r^2 = (x1)^2+y^2+z^2 if x>1
r^2 = (x+1)^2+y^2+z^2 if x<1
r^2 = y^2+z^2 if 1<x<1=
r^2 = (abs(x)1)^2+y^2+z^2 if abs(x)>1
r^2 = y^2+z^2 if abs(x)<1=
xp = abs(x) > 1 ? abs(x)  1 : 0
r^2 = xp^2 + y^2 + z^2I think that's it for the metaline. For the metasurface on XZ plane going from (1,0,1) to (+1,0,+1) it's the same:
xp = abs(x) > 1 ? abs(x)  1 : 0
zp = abs(z) > 1 ? abs(z)  1 : 0
r^2 = xp^2 + y^2 + zp^2Same for metacube:
xp = abs(x) > 1 ? abs(x)  1 : 0
yp = abs(y) > 1 ? abs(y)  1 : 0
zp = abs(z) > 1 ? abs(z)  1 : 0
r^2 = xp^2 + yp^2 + zp^2All pretty simple. Noooow... how to do a metacone... hmmm...

Also the props should be simple. A metacube is just the old cube with sides = 0.3 or so
A metasurface can just be a flattened cube with x/z sides = 0.3, and y sizes = 0.03
The metaline can be a flattened cube with x sides = 0.3, and y/z sides = 0.03.

We can add a metacircle too! It should be useful. Also easier to thing the field equations for the other kinds if we start with a circle.
Also can change this construct
b = a > 0 ? a : 0
to be just b = max(a,0)
So, metacircle on XZ plane with radius = 1
r^2 = max(x^2+z^2  1, 0) + y^2
metacylinder on Y direction with height and radius = 1
r^2 = max(x^2+z^2  1, 0) + max(y^2  1, 0)
So all the simple types:
line: r^2 = max(x^21,0) + y^2 + z^2
plane: r^2 = max(x^21,0) + y^2 + max(z^21,0)
cube: r^2 = max(x^21,0) + max(y^21,0) + max(z^21,0)
circle: r^2 = max(x^2+z^21,0) + y^2
cylinder: r^2 = max(x^2+z^21,0) + max(y^21,0)
sphere: r^2 = max(x^2+y^2+z^21,0)All really just variations of the same form. Now, how about a nice little cone on Y direction... hmmm...

Ops... mistake with that mx function; correct forms are
line: r^2 = max(x1,0)^2 + y^2 + z^2
plane: r^2 = max(x1,0)^2 + y^2 + max(z^21,0)
cube: r^2 = max(x1,0)^2 + max(y1,0)^2 + max(z1,0)^2
circle: r^2 = max(x^2+z^21,0) + y^2
cylinder: r^2 = max(x^2+z^21,0) + max(y^21,0)
sphere: r^2 = max(x^2+y^2+z^21,0)Hmm... not sure if the equations for the metacone are worth the trouble; there are 5 different cases, plus needs to calculate one dastardly square root:
So
u = sqrt(x^2+z^2)
if y<0 then r^2 = y^2 + max(u1,0)^2
elif abs(y) < abs(u)  1 then r^2 = y^2 + (u1)^2
elif abs(y) > abs(u) + 1 then r^2 = (y1)^2 + u^2
elif abs(y) < 1  abs(u) then r^2 = 0
elif r = (u+y1) / sqrt(2)

Actually need to put abs in all that stuff to handle negative coordinates:
line: r^2 = max(abs(x1),0)^2 + y^2 + z^2
plane: r^2 = max(abs(x1),0)^2 + y^2 + max(abs(z1),0)^2
cube: r^2 = max(abs(x1),0)^2 + max(abs(y1),0)^2 + max(abs(z1),0)^2
circle: r^2 = max(x^2+z^21,0) + y^2
cylinder: r^2 = max(x^2+z^21,0) + max(y^21,0)
sphere: r^2 = max(x^2+y^2+z^21,0)cone:
u = sqrt(x^2+z^2)
if y<0 then r^2 = y^2 + max(abs(u1),0)^2
elif abs(y) < abs(u)  1 then r^2 = y^2 + (u1)^2
elif abs(y) > abs(u) + 1 then r^2 = (y1)^2 + u^2
elif abs(y) < 1  abs(u) then r^2 = 0
elif r = (u+y1) / sqrt(2)I think that's it; can't think of any other stuffy thingie to put in.

Grrr... wrong on the abs... it's abs(x)1, not abs(x1)....
line: r^2 = max(abs(x)1,0)^2 + y^2 + z^2
plane: r^2 = max(abs(x)1,0)^2 + y^2 + max(abs(z)1,0)^2
cube: r^2 = max(abs(x)1,0)^2 + max(abs(y)1,0)^2 + max(abs(z)1,0)^2
circle: r^2 = max(x^2+z^21,0) + y^2
cylinder: r^2 = max(x^2+z^21,0) + max(y^21,0)
sphere: r^2 = max(x^2+y^2+z^21,0)cone:
u = sqrt(x^2+z^2)
if y<0 then r^2 = y^2 + max(abs(u)1,0)^2
elif abs(y) < abs(u)  1 then r^2 = y^2 + (u1)^2
elif abs(y) > abs(u) + 1 then r^2 = (y1)^2 + u^2
elif abs(y) < 1  abs(u) then r^2 = 0
elif r = (u+y1) / sqrt(2)Now, all these guys are for a field equation such that
f(0) = 1
Meanwhile I've been using f(1) = 1 over an d^2 = (dist^2/str^2). Also, in the current implementation both Strength and Scale do exactly the same thing... so maybe I should get rid of Strength and use Scale only; it has the advantage that the size of the handler will correspond to how strong it is.
So let's seek a quadratic form f(r) = (2Ka)*(ub)*c such that
f(0) = 1
f(K) = 0.5
f(4*K) = 0WolframAlpha's magic again gives
a = 4K
b = 3K
1/c = (316K^2)/4 = 12K^2Let's try those and see what happens, haha...

Oh, I can keep the Strength, but just change it's purpose.
We can use Strength to make the ball active/inactive; I've been moving them out of the field for that, but that's cumbersome. So Strength = 1.0 means it's fully active, and Strength = 0.0 it's inactive. Any other value is just a multiplier, and Strength = 1.0 is a negative ball.
So
field = Str_n * f(r)

Tests with the metaline; this is the setup; notice the distance between the top sticks  that's to control the bulding that happens when these guys come too close together. Maybe a better field equation will control these guys better, but I'm setting with this for now; left stick has stickness = 1.5, strength = 0.01, horizontal sticks have stickness = 1.0, strength = 0.01:
result is


3 metaplanes:

3 metacubes:

Hmm... back to blackboard for the field equations.. the new field equations don't produce a new smooth sphere, for some reason:

Hmm... I know why, I think... the new field equation has
f(0) = 1
f(K) = 0.5
f(3K) = 0That means that inside the sphere all points have the same potential, so all points in the surface interpolate with mu=0.5, giving that stepped look as all the angles are at 45 degrees...
Oh well, so much for the idea of f(0) = 1; I got that from some paper, but it's a bad bad idea.
Much better idea is my initial field
f(0) >> 1
f(1) = 1
f(K) = 0.5
f(2*K) = 0But that will not translate to something like a cube... for points outside a unitary cube the distance to its surface is
d^2 = max((x1),0)^2 + max((y1),0)^2 + max((z1),0)^2
So can add 1 to that. But how to get a distance < 1 for points inside the cube... hmmm... can do a test, uh?
d^2 = (max(x,y,z))^2 if x < 1 & y < 1 & z < 1, or
d^2 = max((x1),0)^2 + max((y1),0)^2 + max((z1),0)^2 otherwiseSo the extension for line, plane, cube and sphere is:
line:
d^2 = 1 + max((x1),0)^2 + y^2 + Z^2plane:
d^2 = 1 + max((x1),0)^2 + y^2 + max((z1),0)^2cube:
d^2 = (max(x,y,z))^2 if x < 1 & y < 1 & z < 1, or
d^2 = 1 + max((x1),0)^2 + max((y1),0)^2 + max((z1),0)^2 otherwisesphere:
d^2 = x^2 + y^2 + z^2let's see what happens

That did it; back to the smooth world: 3 spheres, 3 sticks and 1 plane:
Oddly enough the sticks became rather spheroidical, and the plane became a perfect circle. I have to check what's up with that, but at least I know I can use the current equation for the plane (which is obviously wrong) as the equation for the circle.

Brrr... ellipsoidical, not spheroidical... haha...

This is latest version, 0.03:
It has line, surface, cube and sphere (although the line shows ellipsoidic and the surface shows as a circle).
I've been using strength from 0.05 to 0.10 in tests, and stickness from 1.5 to 2.0. I'll scale strength later to something more decent.

for the plane, if z = 0 and x < 1 then same potential C is given by
C = 1 + y^2
so should be a perfect straight line on x for 1 < x < 1... why isn't it... hmm don't get it...

New version ready, 0.03, download from
 Has meta line, surface, cube and sphere
 Added a parameter Length (by default 0.1 = 10% of the iso field size)
 Made Strength to affect field potential only; default = 1.0
 Equations have been adjusted so that the iso surfaces are located at 0.01 away from the handler when Strength = 1.0; twice of that when strength is 2.0, and so forth
 Length is the size of the straight sides in meta line, surface and cube; no effect for sphere
In this demo, the first part changes length from 0.1 to 0.2 then back to 0.1; second part changes strength from 1.0 to 2.0 then back to 1.0. Notice that changing length keeps the same fatness of meta lines and meta surfaces, but changing strength makes meta lines and meta surfaces fatter. A metasphere is only controlled by strength; no need to control by length.