Heyhey, been a little busy and forgot to check back.. sorry 'bout that!
Would love to know how the alternating neighbours idea has worked out. I can't see things getting too wobbly, over the course of one frame anyway. I was about to give it a shot, but plain can't be bothered right now :p
After the last post I got a bit curious about the speed of function calls and stuff, and did this wee example:
http://sicklebrick.com/?p=291 I'm thinking that maybe inlining the hashX, hashY and map functions could speed things up a little (at the cost of readablilty).. but again, lack of time!
If I have some time this afternoon, I'll have a bash at it, but for now, here's a few of the changes:
(My full source is a mess from declaring most of the variables private, so a few chunks will have to do.)
Replacing arrays with typed vectors. (slightly faster according to some articles).
Code: Select all
//sexy 3d vectors
hash = new Vector.< Vector.< Vector.<int> > >();
for (var i:Number = 0; i < 40; ++i)
{
hash.push( new Vector.< Vector.<int> >() );
for (var j:Number = 0; j < 40; ++j)
{
hash[i].push( new Vector.<int>() )
}
}
Setting the body's numNeighbours in the applyLiquidConstraNumber function:
Code: Select all
private function applyLiquidConstraNumber(deltaT)
..
body.neighboursTarg. = neighbours.length;
The cheap ass way of drawing metaballs.
Kinda sucks how the marching squares algorithm could almost use the same structure as the liquid if there were enough cycles left. (fluidblob is a circle I drew in the flash IDE).
Code: Select all
private var spriteSource:fluidblob = new fluidblob();
private function drawBlobs():void {
var scaleVar:Number = 1;
var bx:Number;
var by:Number;
fluidData.fillRect( new Rectangle(0, 0, fluidData.width, fluidData.height), 0x00000000);
secondData.fillRect( new Rectangle(0, 0, fluidData.width, fluidData.height), 0x00000000);
for each ( var b:b2Body in liquid ) {
bx = b.GetPosition().x * 30;
by = b.GetPosition().y * 30;
//grow or shrink slowly over several frames
b.neighbours += (b.neighboursTarg - b.neighbours) / 100;
scaleVar = (b.neighbours / 3) + 1;
if (scaleVar < 0.5 ) scaleVar = 0.5;
if (scaleVar > 1.7) scaleVar = 2;
spriteSource.scaleX = scaleVar;
spriteSource.scaleY = scaleVar;
fluidBitmap.bitmapData.draw( spriteSource, new Matrix(scaleVar, 0, 0, scaleVar, bx, by ) );
}
//blur the whole thing
fluidData.applyFilter( fluidData, new Rectangle(0, 0, fluidData.width, fluidData.height), new Point(0, 0), new BlurFilter(18, 18, 1) );
fluidBitmap.visible = false;
//then set the threshhold in a second bitmapdata
secondData.threshold( fluidData, new Rectangle(0, 0, fluidData.width, fluidData.height), new Point(0, 0), ">=", 0xFF009090, 0xFF0000CC, 0x00FFFFFF, false );
}
Here it is running 150 particles:
http://sicklebrick.com/wp-stuff/fluid/(link subject to change as I get around to writing an article about it)
Oh, almost forgot, if you avoid setting LIQUID_Number, you can still get a decent simulation with a bunch more particles.