Flash 9 Motion Detection – Minority Report Style (source code)

by leon on October 11, 2007

I have finally managed to get together the source files for the Motion Detection script. The script allows you to control an interface simply by waving your hands in the air infront of a camera. This is pretty cool if you want to add some extra interactivity to your site. The original post can be found here. This original code was taken from Web Designer Mag. They hold all the copyrights etc

The code is below, I wish I had more time to go through it fully and comment it out but I don’t. If you need any help, add a comment to this post.

import flash.display.*;

import flash.geom.*;

var cam:Camera = Camera.get();

sizeX = cam.width;

sizeY = cam.height;

cam.setMode(544,396,25);

vid.attachVideo(cam);

vid._width = cam.width;

vid._height = cam.height;

mov_pos_array = new Array(“0″, “0″, “-544″, “-1088″, “-1632″);

newX = 0;

Xspeed = 0;

content_mc.onEnterFrame = function() {

Xspeed = ((this._x-newX)/2+Xspeed)/5;

this._x -= Xspeed;

};

active = false;

aCount = 0;

start_btn.onPress = function() {

active = true;

start_btn._visible = false;

start_btn.enabled = false;

};

now = new BitmapData(160, 120);

before = new BitmapData(160, 120);

rect = new Rectangle(0, 0, 160, 120);

pt = new Point(0, 0);

sizeDif = 160/cam.width;

thisFrame1=0;

thisFrame2=0;

thisFrame3=0;

thisFrame4=0;

over1=false;

over2=false;

over3=false;

over4=false;

onEnterFrame = function () {

if (!cam.muted && active == true) {

now.draw(vid);

now.draw(before,new Matrix(),new ColorTransform(),’difference’);

now.threshold(now,rect,pt,’>’,0xff111111,0xffffffff);

before.draw(vid);

for (i=1; i<5; i++) {

_root["ballNew"+i+"Y"] = (_root["impact"+i]._y/cam.height)*120;

_root["ballNew"+i+"X"] = _root["impact"+i]._width/2+_root["impact"+i]._x;

_root["ballNew"+i+"X"] = 160-(_root["ballNew"+i+"X"]*sizeDif);

_root["val"+i] = now.getPixel(_root["ballNew"+i+"X"], _root["ballNew"+i+"Y"]);

if (_root["val"+i]>16700000) {

_root["thisFrame"+i] +=1;

_root["over"+i]=true;

}

if (_root["thisFrame"+i] == 2){

_root["impact"+i]._alpha = 40;

newX = mov_pos_array[i];

_root["thisFrame"+i] = 0;

_root["over"+i]=false;

} else {

_root["impact"+i]._alpha = 100;

}

}

rotated = Xspeed/4;

disk_mc._rotation+=rotated;

}

};

checkglitch = function(){

for (i=1; i<5; i++) {

if (_root["over"+i]==true){

_root["thisFrame"+i] = 0;

_root["over"+i] = false;

}

}

};

inter = setInterval(this, “checkglitch”, 1200);

To see an example, click here.

Finally, what you have been waiting for: the source code.

Here is a PDF of the tutorial. Sorry for the low image quality. Hope it helps

5 comments

Good work! Thanks for sharing this!

by TS on November 3, 2007 at 10:09 am. Reply #

Hey
I love what you have created. The PDF of the tutorial doesnt seem to be here on this page is there any chance it could be fixed so we could have a look?

Cheers! :)

by MR. T on November 5, 2009 at 7:00 pm. Reply #

Thanks !!

Gotcha what i want :P

by amrita on February 27, 2010 at 11:01 am. Reply #

I don’t see what the difference is between what’s posted on the http://www.webdesignermag.co.uk site. Or is it that you did the tut that’s posted there? Trying to work out some of the language but I’m totally stuck on “newBall” and what that’s doing. It’s a great interface.

by Lorelei on April 3, 2010 at 3:58 pm. Reply #

I love what you have created.

by web design on August 13, 2010 at 6:59 am. Reply #

Leave your comment

Required.

Required. Not published.

If you have one.