It seems that the subject of Flash motion tracking has been neglected as of late. Developers who want to develop something that looks cool and responds to motion have opted to play with FLARToolkit/Augmented Reality. The gesture recognition and multi-touch capabilities of Flash Player 10.1 provide a user interface that reliably captures user input better than the difference/threshold method of motion tracking in Flash. So will we ever have a Minority Report-style UI that allows us to fight crime by flailing our arms? If so, it probably won’t be built with Flash. But can we still create some stuff that looks cool using motion tracking in Flash? Absolutely.
This post isn’t meant to be a tutorial, but here’s the quick-and-dirty of how motion tracking with Flash works. In short, we compare the current frame of video with the previous frame, see what’s changed and mark that as motion. Here’s how it happens:
- Get the Camera- No webcam, no fun.
- Create a Matrix to flip the video and subsequent BitmapData. – The video is displayed as it’s captured, so we need to flip it on the y-axis in order to make it display the mirror image of the video.
- Attach the Camera to a Video object – We can’t see the Camera unless it’s attached to a video.
- Create our three BitmapData instances – We use
_previousto calculate motion inside the
- Create a Bitmap from our
_output– This allows us to see the tracked motion.
- Create a Timer to dispatch events at the
- Handle the TimerEvent with the
The magic happens in the
render() method. First we draw the
_video into the
_difference BitmapData. Then we draw the BitmapData stored in
_previous (the previous frame of video) into the
_difference BitmapData in
"difference" mode. This turns any pixels that have changed since the previous frame white and the static pixels black. Then we fill the
_output with transparent pixels and use the
threshold() method of the BitmapData class to color any pixels in the
_difference BitmapData that aren’t black (greater than the
_thresholdMask value). We then draw the current video frame to
_previous for use the next time the TimerEvent is dispatched.
Some of that may be a bit confusing, but it’s not necessary that you understand exactly what’s going on in order to get started experimenting yourself. I’ve created a BasicTracker class for motion detection in Flash in order help you get started. You can easily extend the BasicTracker class to create your own experiments. The attached MotionHitTest class demonstrates how you can perform a
hitTest() between objects on the stage and motion detected by the webcam.
When sub-classing the BasicTracker, override the
render() method as such:
public override function render(e:TimerEvent = null):void
//your code here
Pretty simple, huh?