# I have an “amoeba” game mechanic. Any idea on how to implement it?

Outside of a tetris clone, a crappy 2D top-down shooter, and some messing around with stuff like Unity and Flixel, I realize that I have yet to complete a single, polished, bells-and-whistles game. I want to change this, and I have an idea for my next project.

The idea is that you're an amoeba. Amoebas have these eye-like cores (or something like that, I don't know biology), and you have two of 'em. You control one with WASD and the other with IJKL. There has to be a constant radius of stuff around each of the cores:

And the area of the amoeba has to stay constant. So if you move a core in one direction, you increase the amoeba's area, but that increase is compensated by a decrease somewhere else:

Aaaaaand I'd like to implement a vagination mechanic. You absorb things by engulfing them, like a boss. Maybe even an extra core, or a needle that pops you and causes all your inner stuff to start gushing out:

But here's the problem: I don't know how to make this. However, I would like some ideas on how to implement it. Should I explore physics libraries like Box2D? Or maybe something involving fluid physics? Any help would be much appreciated.

P.S. Feel free to steal this idea. I have plenty of ideas. If you do, please tell me how you made it so I can try it myself.

• Noun- and verb-forms are invagination and invaginate respectively, FYI. (I did do biology.) Nice pics. – Engineer Jul 13 '12 at 14:10
• where'd you get that sweet black paper? – brendan Jul 27 '12 at 9:25
• @brendan: If you swap black with white, red with blue and white with black, it becomes a lot more apparent where he got this "black paper". ;) – knight666 Jul 27 '12 at 9:38
• You guys are too clever :D – Jason Tu Jul 27 '12 at 13:16

Off the top of my head, the clause that sticks out to me is that you want the area to be constant. That strikes me as the pain in all of this, so let's try and come up with a easy solution to that.

Take a piece of string and tie the ends together to make a circle. I may be wrong, but my intuition says that the inside of that string has a constant area, or would act an awful lot like your amoeba boundary.

So I would say: implement the string. ;) Most physics libraries can do springs (in Box2D this looks like the distance joint), and the easiest way to make a string is to connect lots of strings together. Join the last one up to the first to create a loop. Make sure the springs in the string have a high enough spring constant to not get stretched apart - you want them to be quite rigid and move in unison, not to expand out.

Next, create forces to act on the string. Trivially, your two cores/eyes/control points create forces onto the points that make up the string. Make the force fall off with an inverse square function:

float forceOnPoint(point, ball)
{
float d = distance(point, ball);
return K / (d*d);
}


And make the direction of the force directly away from the control eye/ball, like a repellent force emanating from the ball. Tweak K to control the strength of the force. Make sure that K isn't too high - you don't want it to just be pushed out massively by the force, just keep it away from the balls. Leave plenty of slack in your string.

With just those two you will probably end up with quite a uniform shape, so I'd also suggest you create some weaker forces around in the surrounding 'soup' to change the shape of the amoeba. You can make these random places, change their force regularly (you could make it a sinusoid function for nice movements) and make them work in the same way as the balls.

Vagination(fnarr): this is the difficult one. You cause it to envelop an object by checking to see if two non-adjacent points on the string have managed to get close enough, and if so you need to:

Measure the distance between the points on the string. Count the number of spring sections. Find the 'shorter' path from A to B.

Break the string and re-join it with the two points adjacent. In other words, directly join point A to point B and discard all the stuff inbetween.

Re-insert the missing number of points (plus any bonus points if you want to expand in size by eating something) into the string in the other side - i.e, the side from B to A. If you insert them equally along the length and make them shorter than the desired distance between points, you should get a nice springy-expandy effect.

I think that covers everything. By the way: idea sounds awesome.

• FWIW, while I like this solution on the whole, your intuition about the interior of the string having constant area is wrong - the circle has maximal area out of any shape with that tied-together string as its boundary. The easiest way to see that it varies is to imagine 'squishing' the string by pulling two ends taut; then the shape you get (whether rectangle or ellipse or what-have-you) can never get wider than half the perimeter, but its height shrinks to zero so the area (which is bounded by width*height) also shrinks to zero. – Steven Stadnicki Jul 26 '12 at 23:01
• I think you meant "...to make a string is to connect lots of springs together." – Richard Marskell - Drackir Dec 14 '12 at 19:09

In order for you to simulate endocytosis (the process of the cell eating external cores), each core, both inside and outside of the amoeba, would repel the boundary of the amoeba. The cores inside of the amoeba will have a moderately strong repulsive effect, while the ones outside will have a small repulsive effect. This is illustrated below.

As the outside core (red) approaches the amoeba, it's repulsive field will create a circular indentation in the amoeba. Eventually, the two sides (highlighted green) will touch each other. When a collision like this is detected, the circular indentation is erased and whatever inside of it is consumed.

This mimics the process of endocytosis quite well, and gives a nice visual effect.

As for simulating the boundary of the amoeba, I like Matt Kemp's idea of using a very large number of connected springs. This does not maintain a constant area, but a constant perimeter. In real life, water can flow freely in and out of a cell membrane (the amoeba boundary), so cells do not have a constant volume.

Some other ideas to make the game realistic would be being able to modify the salt content of cells somehow. When the salt concentration of the inside and outside of a cell do not match up, water rushes through the cell membrane towards the place with the higher salt content in a process called osmosis. If the amoeba has a high salt content, it would have a larger volume, but the perimeter would not change. In the game, this could be simulated by giving the inside cores a higher repulsive effect if the salt content is higher.

Since a higher salt content would give the cores a higher repulsive effect, it have the advantage of making the amoeba easier to control. Having a lower salt content would cause the amoeba to flex and bend under the influence of the environment easier. This has the advantage of being able to eat other cores easier, since the amoeba would be able to surround it easier.

Well to do the graphical effect you would most likely use metaballs (theres plenty of tutorials online for them)

Well I know of an educational game called CellCraft (no relation to Minecraft though) which involves controlling cell-like creature that has very similar movements to what you need. The game is free and probably more important is that they stated on their site clearly that they intend to make the source code available for free and it was made in ActionScript 3 and is a flash based game.

http://www.cellcraftgame.com/Home.html

The way I would go about this is to simulate a string loop similar to the way Matt Kemp suggested, but fill the interior with particles that repel each other and the string. this way you will ensure approximate volume conservation. You will have to take care to ensure large forces do not knock the particles outside.

Look up "smoothed particle hydrodynamics" for simulation of the particles, and "verlet integration" for simulating them (and the string). Verlet is a position-based technique so it is very simple and stable.

To keep things efficient I would suggest a "uniform grid spatial partitioning" system. Again, easy to implement and yeilds great speedups.

You could also use metaballs to visualise the object instead of using the string. But then you have the problem that particles might separate into separate clumps. Look up "Van der Waals forces" for attraction/repulsion balance leading to clumping.

Another approach would be to provide a small 'inflate' force to the string particles in the direction of their 'outward' normals. This will simulate a kind of internal pressure. The result is that the loop will try to maintain a circle shape. That's what I used for this.

Hope it helps.