Skip to content

Crane: Flash Motion Capture Experiment

September 30, 2009

Try out Crane here:  http://www.nitinmalik.com/flash/crane/crane.html

The “Crane” flash application is a uses motion capture technology to allow you to move a crane arm in flash by moving in front of a webcam. The motion capture coding that made this application possible is called Ostrich (http://ostrichflash.wordpress.com/)  and was made available by its creator, Dan Zen, for FREE! Ostrich is only one of a number of complex and unique flash solutions that are collectively called Flash Feathers. Some other interesting Flash Feathers that I will be trying out later include Goose and Dodo, which allow you to incorporate the use of multitouch and blob detection with your flash files.

Crane uses Ostrich in a way that is not documented in the files that the Ostrich download comes with and because of this, I found it quite a challenge to do  (I should probably say, I only started using Flash and Actionscript 3 about 4 weeks ago). The challenge of making Crane can from its incorporation of Bones in the flash file. The final goal was to make the “Ostrich cursor” be able to move the Crane which contained an Armature made with the bone tool. Note that the ostrich cursor is created by the code that comes with Ostrich and is internal to your flash file (separate from your mouse cursor).

I will briefly explain where my first attempts when wrong. I began by trying to make the coordinates of the node at the end of my crane equal to the coordinates of the ostrich cursor. This caused the object associated with the this node to separate from the armature and move independently. (fail)

In my second attempt, I realized that when your armature is set to “runtime” instead of “authortime”, you can drag the armature around with your mouse when the flash file is running. This was an important step in the right direction! However, then I tried to you the startDrag function on the object at the end of my crane, thinking that if this object was dragging and moving using the Ostrich Cursor, that the armature had to move with it. This failed as well,  the object still detached, I think possibly moving the armature a little when the object was close to it?

Then an idea struck me, I should probably be using the Classes and Methods  related to bones and inverse kinetics. This are under fl.ik.*   . At this point I finally had search words that would lead me to examples on the internet. That’s where I can across this very helpful blog: http://www.lorenzgames.com/blog/moving-bones-in-as3-and-flash-cs4. Check it out for to find some of the details I may glance over. Note that they coded the ActionScript on the main timeline and I coded in a separate AS file.

To make Crane work, I had to use IKMover to specify where I wanted the end of my crane to go. I made the coordinates used by IKMover equal to the coordinates of my Ostrich Cursor (well actually it was a Point not a coordinate). Thanks to some help from Dan Zen himself, we were able to debug my ActionScript. With so many variable, it seemed like flash was unable to recognize some of them before they were used in the methods or referenced in other variables. Therefore, he added a timer to set off my function related to IKMover after a short delay when the flash file is started.

Future plans for Crane (if I get the time) include making the crane capable of grabbing object (probably clamping using a key press), updating the look of the crane and possibly making object drop when the crane lets go (haven’t figured out that one yet).

I’ve shown my Actionscript below, and I’m writing up a review of Ostrich separately if you are interested to try it out.

If you copy and paste this into an ActionScript file, you can use the Autoformat option to make it more readable.

</pre>
package crane{

import flash.display.Sprite;
import flash.events.*;
import flash.utils.Timer;
import flash.geom.Point;
import fl.ik.*;

import com.danzen.interfaces.ostrich.*;
//make sure to set up the source path of this folder in your preferences

public class Crane extends Sprite {

private var myCamera:OstrichCamera;
private var myCursor:OstrichCursor;

private var myArm:IKArmature;
private var fBone:IKBone;
private var tJ:IKJoint;
private var posT:Point;
private var ikMover:IKMover;

private var myTimer:Timer;

public function Crane() {

myTimer=new Timer(500,1);
myTimer.start();
myTimer.addEventListener(TimerEvent.TIMER,init);

trace("hi from Crane");

myCamera=new OstrichCamera(0,0,640,480);
myCamera.alpha=.5;
addChild(myCamera);
setChildIndex(myCamera,0);

myCursor=new OstrichCursor(myCamera);
addChild(myCursor);

removeChild(myCursor); //takes away the little square that represents myCursor

}

private function init(e:TimerEvent) {
myArm=IKManager.getArmatureByName("myArmature"); //myArmature is the instance name of the Crane's Armature
fBone=myArm.getBoneByName("firstBone"); //firstBone is the instance name of the bone at the moving end of the Crane
tJ=fBone.tailJoint; //tailJoint refers to the end of the bone furthest from the joint
posT=tJ.position;
ikMover=new IKMover(tJ,posT);

addEventListener(Event.ENTER_FRAME,onFrame);
}

private function onFrame(e:Event) {

posT.x=myCursor.x;
posT.y=myCursor.y;
ikMover.moveTo(posT);
}

}

}

Advertisements
One Comment leave one →
  1. justme permalink
    July 23, 2012 17:57

    I was looking at this myself and wondering how to make the cursor into a drawing tool, to allow drawing.
    What you did with the Ostrich is amazing!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: