DiffCameraWithBall
DiffCamera.as
動体検知&衝突判定
// forked from kadal's DiffCamera
// forked from kadal's MosaicCamera
// forked from kadal's BlurCamera
// forked from kadal's BasicCamera
/* DiffCamera.as */
//
// 動体検知&衝突判定
//
package {
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.media.Camera;
import flash.media.Video;
import flash.events.Event;
import flash.filters.ColorMatrixFilter;
import flash.geom.Point;
import flash.geom.Rectangle;
import net.hires.debug.Stats;
[SWF(backgroundColor="#000000", frameRate=15)]
public class DiffCamera extends Sprite {
private var SW:Number = stage.stageWidth;
private var SH:Number = stage.stageHeight;
private var myCamera:Camera;
private var myVideo:Video;
private var myStage:Sprite = new Sprite();
private var myDraw:Sprite = new Sprite();
private var samplingSize:Number = 5;
private var samplesArray:Array = new Array();
private var rectArray:Array = new Array();
private var sensitive:Number = 3000000;
private var myBall:Ball = new Ball();
public function DiffCamera():void {
myCamera = Camera.getCamera();
myCamera.setMode(500, 500, 15);
myVideo = new Video(stage.stageWidth, stage.stageHeight);
myVideo.attachCamera(myCamera);
myBall.graphics.beginFill(0xFFFFFF, 1);
myBall.graphics.lineStyle(1, 0x000000, 1)
myBall.graphics.drawCircle(0, 0, 10);
myBall.graphics.endFill()
addChild(myStage);
myStage.addChild(myVideo);
for (var iX:int=0; iX<SW/samplingSize; iX++) {
samplesArray.push(new Array());
rectArray.push(new Array());
for (var iY:int=0; iY<SH/samplingSize; iY++) {
samplesArray[iX].push(0);
var myRect:Sprite = new Sprite();
myRect.graphics.beginFill(0xFF0000, 1);
myRect.graphics.drawRect(iX*samplingSize, iY*samplingSize, samplingSize, samplingSize)
myRect.graphics.endFill();
myRect.visible = false;
rectArray[iX].push(myRect);
myStage.addChild(rectArray[iX][iY]);
}
}
myStage.addChild(myBall);
myBall.x = SW/2;
myBall.y = 10;
myBall.vX = Math.random() * 5 + 5;
myBall.vY = Math.random() * 5 + 5;
addChild(new Stats());
addEventListener(Event.ENTER_FRAME, xDraw);
}
public function xDraw(evt:Event):void{
myBall.x += myBall.vX;
myBall.y += myBall.vY;
if(myBall.x - myBall.width/2 < 0){
myBall.x = myBall.width/2;
myBall.vX *= -1;
}
if(myBall.x + myBall.width/2 > SW){
myBall.x = SW - myBall.width/2;
myBall.vX *= -1;
}
if(myBall.y - myBall.height/2 < 0){
myBall.vY *= -1;
myBall.y = myBall.height/2;
}
if(myBall.y + myBall.height/2 > SH){
myBall.vY *= -1;
myBall.y = SH - myBall.height/2;
}
var myBmd:BitmapData = new BitmapData(SW, SH);
myBmd.draw(myVideo);
myBmd.applyFilter(myBmd, new Rectangle(0, 0, SW, SH), new Point(0, 0),
new ColorMatrixFilter([1/3, 1/3, 1/3, 0, 0, 1/3, 1/3, 1/3, 0, 0, 1/3, 1/3, 1/3, 0, 0, 0, 0, 0, 255, 0]));
for (var iX:int=0; iX<SW/samplingSize; iX++) {
for (var iY:int=0; iY<SH/samplingSize; iY++) {
rectArray[iX][iY].visible = false;
}
}
var refFlg:Boolean = false;
for (iX=0; iX<SW/samplingSize; iX++) {
for (iY=0; iY<SH/samplingSize; iY++) {
var pick:int = myBmd.getPixel(iX*samplingSize, iY*samplingSize);
if(Math.abs(samplesArray[iX][iY]-pick) > sensitive){
if(rectArray[iX][iY].hitTestObject(myBall)){
refFlg = true;
//rectArray[iX][iY].visible = true;
}
}
samplesArray[iX][iY] = pick;
}
}
if(refFlg){
myBall.vX *= -1;
myBall.vY *= -1;
}
}
}
}
import flash.display.Sprite;
class Ball extends Sprite {
public var vX:Number;
public var vY:Number;
public function Ball(){
}
}