Question

I'm doing a motion detection program and want to determine if a person is moving their hand in a clockwise or counterclockwise motion. Would someone know the best way to do this? (I'm coding in AS3, but any ECMA-like code would help).

I believe one way would be to determine if, for instance, a left-right is followed by a top-bottom or a bottom-top, and so on — is there a better way?

Était-ce utile?

La solution

If you have a point of reference (0,0), you can sample the change in angle over time:

var sprite:Sprite = new Sprite();
var samples:Array = [];
setInterval(function(){
    var len = samples.push(Math.atan2(sprite.mouseY,sprite.mouseX));
    if ( len >=2 ) {
        if ( samples[len-2]-samples[len-1] > 0 ) {
            // clockwise
        } else {
            // counter-clockwise
        }
    }
},1000/stage.frameRate);

The reference is the top-left of the sprite (or its natural 0,0 point by atan2), so this snippet samples the mouse coordinate relative to the sprite.

Also, the more samples you consider (I'm only sampling 2 samples[len-2] and samples[len-1]) the better the decision for cw or ccw.

Lastly, and most importantly, my script does consider negative numbers in the radian calculations, it assumes everything is positive - this would require some tweeking.

PS: You'll want to clear samples every now and again since the array length will max out.

My script is incomplete, and I'd welcome edits, suggestions and elaborations. My hope is that it gives you something to consider and work from.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top