Homegrown Tentacles
I had been scurrying about over on the Soulwire blog, again, and looking at the tentacles. I did some experiments of
my own, but the tentacles never came out right, not organic enough. I wrote Justin and asked him how he did it and he was really cool about helping out with a code example of how he did that part.
The Soulwire Tentacles was an experiment in PixelBender speed and usability, so he makes it look as if the tentacles are really growing, i.e. starts out small and each “joint” grows as the tentacles grows. Mine is just a look into the random-organic way they grow. Also I was trying to see what kinds of speed I would get by using the Bitmap approach.
Here are the Soulwire Tentacles
And here are my Tentacles… that came out wrong…
The code:
/**
*
* @BitmapSpore.as : inspired by http://blog.soulwire.co.uk/flash/actionscript-3/pixel-bender-growing-tentacles/
* @Date 08/06-09
* @Ricki Gregersen
*/
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.filters.BlurFilter;
import flash.geom.ColorTransform;
import flash.geom.Point;
[SWF( width="512", height = "512", backgroundColor = "0x222222", frameRate = "30" )]
public class BitmapSpore extends Sprite
{
private static var fading : ColorTransform = new ColorTransform( 1, 1, 1, 0.99999 );
private static var _blur : BlurFilter = new BlurFilter( 1, 1, 2 );
private var _bmd:BitmapData;
private var _bmp:Bitmap;
private var _container:Sprite;
public function BitmapSpore()
{
_bmd = new BitmapData( 512, 512, true, 0x222222 );
_bmp = new Bitmap( _bmd );
addChild( _bmp );
var s:Spore;
addChild( _container = new Sprite() );
for( var i:int = 0; i < 500; i++ )
{
s = new Spore( 256, 256, range( 0.01, 0.05 ), range( 2.0, 3.0 ), range( 0.2, 1.0 ));
s.scaleX = s.scaleY = Math.random() * 2;
_container.addChild( s );
}
addEventListener( Event.ENTER_FRAME, update );
}
private function update( event:Event ):void
{
var n:int = _container.numChildren;
var s:Spore;
for( var i:int = 0; i < n; i++ )
{
s = Spore( _container.getChildAt( i ));
s.move();
s.x = s.loc.x;
s.y = s.loc.y;
}
_bmp.filters = [ _blur ];
_bmd.colorTransform( _bmd.rect, fading );
_bmd.draw( _container );
}
private function range(min:Number, max:Number = NaN):Number
{
if(isNaN(max)) {max = min; min = 0}
return min + (Math.random() * (max - min));
}
}
}
import flash.geom.Point;
import flash.display.Shape;
class Spore extends Shape
{
public var loc:Point;
private var angStep:Number;
private var posStep:Number;
private var curl:Number;
private var n:Number = 0;
private var a:Number = Math.random() * Math.PI * 2;
function Spore( xPos:Number, yPos:Number, aStep:Number, pStep:Number, c:Number )
{
loc = new Point( xPos, yPos );
angStep = aStep;
posStep = pStep;
curl = c;
graphics.lineStyle( 0, 0x222222, 0.7 );
graphics.beginFill( 0xFF6600, 0.7 );
graphics.drawCircle( 0, 0, 5 );
graphics.endFill();
}
public function move():void
{
n += Math.random() * (angStep - -angStep) + -angStep;
n *= 0.9 + curl * 0.1;
a += n;
loc.x += Math.cos( a ) * posStep;
loc.y += Math.sin( a ) * posStep;
this.scaleX = this.scaleY *= 0.98;
var s:Number = this.scaleX = this.scaleY;
if( s < 0.1 )
{
loc.x = loc.y = 256;
this.scaleX = this.scaleY = Math.random() * 2;
}
}
}