Particles based on Motion

Here an experiment which shows 256 particles on screen. They get actived by the motion of the user (Webcam needed). This experiment does not show the full power of the routine because there are only the particles flying around. You can (and that is the nice thing) calculate and interpolate motion vectors. I did this in Flash8 with a 160×120 px webcam image which has been transformed to 320×240 px and the user was able to punch a ball around.
The routine is not very hard to understand. You have an image of the current webcam and greyscale it. Then you substract the last image, which you stored in a BitmapData from the current. The BlendMode.DIFFERENCE uses the formula newl = abs(old - new) and is exactly what we need. When I worked with Aibo’s camera I used some screen division method. You can also use scanlines which do the job for you. I use another method to get more inaccuracy (yes). I divide the screen in for example 16×16 parts and then I look onyl vor every 2nd pixel for example. This gives good results and keeps the number of total iterations low.
Anyway you know how much motion has been recognized in a given field.
If you want to punch a ball you would look for the best motion you can find around the ball and then look for the last motion. If you use pythagoras you get the distance between theese two points which is an indicator for the strength.
I had to interpolate the points a little bit to get this working with a ball in Flash8. Now have a look at theese nice particles. The performance depends on the number of particles and their size. There are currently 256 particles on screen and they have a size of 8×8 px. All particles get a different color at runtime and they fade using a ColorTransform. This experiment is very simple if you don’t think about the particles. It is only some sort this code:

[as] search.analyze( screen );

var m: Array = search.motion;
var n: int = 0×100;
var x: int;
var y: int;

while ( –n > -1 )
{
x = n & 0xf;
y = (n & 0xff) >> 4;

if ( m[ x ][ y ] > 0 )
addParticle( x * 20 , y * 15 );
}[/as]

I have to optimize this more and more. The array where the motion gets stored could have only one dimension. But the full 320×240 px webcam gets analyzed. There are 256 particles using blendmodes and colortransforms. If you do this with one ball only you could use a higher detail level for the analyzation.

Related Posts

Leave a Reply






Close
E-mail It