# 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 = (x-1)^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^2

I think that's it for the meta-line. For the meta-surface 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^2

Same for meta-cube:

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^2

All pretty simple. Noooow... how to do a meta-cone... hmmm...

• Also the props should be simple. A meta-cube is just the old cube with sides = 0.3 or so

A meta-surface can just be a flattened cube with x/z sides = 0.3, and y sizes = 0.03

The meta-line can be a flattened cube with x sides = 0.3, and y/z sides = 0.03.

• We can add a meta-circle 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, meta-circle on XZ plane with radius = 1

r^2 = max(x^2+z^2 - 1, 0) + y^2

meta-cylinder 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^2-1,0) + y^2 + z^2
plane: r^2 = max(x^2-1,0) + y^2 + max(z^2-1,0)
cube: r^2 = max(x^2-1,0) + max(y^2-1,0) + max(z^2-1,0)
circle: r^2 = max(x^2+z^2-1,0) + y^2
cylinder: r^2 = max(x^2+z^2-1,0) + max(y^2-1,0)
sphere: r^2 = max(x^2+y^2+z^2-1,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(x-1,0)^2 + y^2 + z^2
plane: r^2 = max(x-1,0)^2 + y^2 + max(z^2-1,0)
cube: r^2 = max(x-1,0)^2 + max(y-1,0)^2 + max(z-1,0)^2
circle: r^2 = max(x^2+z^2-1,0) + y^2
cylinder: r^2 = max(x^2+z^2-1,0) + max(y^2-1,0)
sphere: r^2 = max(x^2+y^2+z^2-1,0)

Hmm... not sure if the equations for the meta-cone 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(u-1,0)^2
elif abs(y) < abs(u) - 1 then r^2 = y^2 + (u-1)^2
elif abs(y) > abs(u) + 1 then r^2 = (y-1)^2 + u^2
elif abs(y) < 1 - abs(u) then r^2 = 0
elif r = (u+y-1) / sqrt(2)

• Actually need to put abs in all that stuff to handle negative coordinates:

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^2-1,0) + y^2
cylinder: r^2 = max(x^2+z^2-1,0) + max(y^2-1,0)
sphere: r^2 = max(x^2+y^2+z^2-1,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 + (u-1)^2
elif abs(y) > abs(u) + 1 then r^2 = (y-1)^2 + u^2
elif abs(y) < 1 - abs(u) then r^2 = 0
elif r = (u+y-1) / 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(x-1)....

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^2-1,0) + y^2
cylinder: r^2 = max(x^2+z^2-1,0) + max(y^2-1,0)
sphere: r^2 = max(x^2+y^2+z^2-1,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 + (u-1)^2
elif abs(y) > abs(u) + 1 then r^2 = (y-1)^2 + u^2
elif abs(y) < 1 - abs(u) then r^2 = 0
elif r = (u+y-1) / 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) = (2K-a)*(u-b)*c such that

f(0) = 1
f(K) = 0.5
f(4*K) = 0

Wolfram-Alpha's magic again gives

a = 4K
b = 3K
1/c = (316K^2)/4 = 12K^2

Let'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 meta-line; 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 meta-planes:

• 3 meta-cubes:

• 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) = 0

That 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) = 0

But that will not translate to something like a cube... for points outside a unitary cube the distance to its surface is

d^2 = max((x-1),0)^2 + max((y-1),0)^2 + max((z-1),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((x-1),0)^2 + max((y-1),0)^2 + max((z-1),0)^2 otherwise

So the extension for line, plane, cube and sphere is:

line:
d^2 = 1 + max((x-1),0)^2 + y^2 + Z^2

plane:
d^2 = 1 + max((x-1),0)^2 + y^2 + max((z-1),0)^2

cube:
d^2 = (max(x,y,z))^2 if x < 1 & y < 1 & z < 1, or
d^2 = 1 + max((x-1),0)^2 + max((y-1),0)^2 + max((z-1),0)^2 otherwise

sphere:
d^2 = x^2 + y^2 + z^2

let'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...

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...