Flash/Filex学習ノート(23):運動学原理

11948 ワード

まず共通のボール類Ballを書きます.
package{
	import flash.display.Sprite;
	
	//   
	public class Ball extends Sprite{
		
		private var radius:Number ;// 
		private var color:uint;// 
		
		public function Ball(r:Number=50,c:uint=0xff0000){
			this.radius = r;
			this.color = c;
			init();
		}
		
		private function init():void{
			graphics.beginFill(color);
			graphics.drawCircle(0,0,radius);
			graphics.endFill();
		}
	}
}

円周動作と楕円運動:
主に三角関数と楕円式を組み合わせてオブジェクトのx,y座標を計算する
var ball:Ball = new Ball(5,0xff0000);
var ball2:Ball = new Ball(8,0x0000ff);
addChild(ball);
addChild(ball2);

var _radius:uint = 75;
var _angle:Number = 0;
var _centerX = stage.stageWidth/2;
var _centerY = stage.stageHeight/2;

var _radius_X = 100;
var _radius_Y = 175;

addEventListener(Event.ENTER_FRAME,EnterFrameHandler);

graphics.lineStyle(1);
graphics.moveTo(_centerX,_centerY);

function EnterFrameHandler(event:Event):void {
	
	ball.x =  _centerX +  _radius * Math.cos(_angle * Math.PI/180);
	ball.y = _centerY + _radius * Math.sin(_angle * Math.PI/180);
	
	ball2.x =  _centerX +  _radius_X * Math.cos(_angle * Math.PI/180);
	ball2.y = _centerY + _radius_Y * Math.sin(_angle * Math.PI/180);
	
	_angle += 1;
	
	if (_angle<=360){		
		graphics.moveTo(ball.x,ball.y);
		graphics.lineStyle(1,_angle * Math.pow(2,24)/360,1);
		//graphics.lineStyle(1,Math.random() * 0xffffff,1);
		graphics.lineTo(ball2.x,ball2.y);
	}	
}

 
均等加速直線運動:速度公式:v=v 0+at、物理学上の公式はそうですが、Flashでは考え方が少し変わります.Flashのデフォルトは毎秒24フレームですが、EnterFrameイベントは新しいフレームに入るたびにトリガーされるので、フレームごとに「単位時間」であり、均一加速の重要な特徴は単位時間速度ごとに一定値を増加させることです.だからFlashではEnterFrameで速度を固定値だけ増やせばいい
var ball:Ball;
var vx:Number = 0;
var ax:Number = 0;
var vy:Number = 0;
var ay:Number = 0;

ball = new Ball  ;
addChild(ball);
ball.x = stage.stageWidth/2;
ball.y = stage.stageHeight/2;
ball.scaleX = 0.1;
ball.scaleY = 0.1;

addEventListener(Event.ENTER_FRAME,EnterFrameHandler);
stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, KeyUpHandler);

graphics.lineStyle(1,0,1);
graphics.moveTo((stage.stageWidth/2)-10,stage.stageHeight/2);
graphics.lineTo((stage.stageWidth/2)+10,stage.stageHeight/2);
graphics.moveTo(stage.stageWidth/2,(stage.stageHeight/2)-10);
graphics.lineTo(stage.stageWidth/2,(stage.stageHeight/2)+10); 

function EnterFrameHandler(event:Event):void {
	vx += ax; // ,x ( x )
	ball.x += vx;
	vy += ay; // ,y ( y )	
	ball.y += vy;
	if (ball.x>=stage.stageWidth-ball.width/2) {
		ball.x=stage.stageWidth-ball.width/2;		
		vx = 0;
	} else if (ball.x <= ball.width/2) {
		ball.x = ball.width/2;		
		vx = 0;
	}	
	if (ball.y>=stage.stageHeight-ball.height/2) {
		ball.y=stage.stageHeight-ball.height/2;		
		vy = 0;
	} else if (ball.y <= ball.height/2) {
		ball.y = ball.height/2;		
		vy = 0;
	}
}

function KeyDownHandler(event:KeyboardEvent):void {
	switch(event.keyCode){
		case Keyboard.RIGHT:
		ax = 0.5;
		break;
		case Keyboard.LEFT:
		ax = -0.5;
		break;
		case Keyboard.DOWN:
		ay = 0.5;
		break;
		case Keyboard.UP:
		ay = -0.5;
		default:
		break;
	}
	
}

function KeyUpHandler(event:KeyboardEvent):void {
	ax=0;
	ay=0;
}

自由落体運動:実は直線運動を均一に加速させる特例で、上のコードを少し修正すればいいです.
function EnterFrameHandler(event:Event):void {
	vx += ax; 
	ball.x += vx;
	vy += ay; 	
	vy += 0.45; // , , 
	ball.y += vy;
	if (ball.x>=stage.stageWidth-ball.width/2) {
		ball.x=stage.stageWidth-ball.width/2;		
		vx = 0;
	} else if (ball.x <= ball.width/2) {
		ball.x = ball.width/2;		
		vx = 0;
	}	
	if (ball.y>=stage.stageHeight-ball.height/2) {
		ball.y=stage.stageHeight-ball.height/2;		
		vy *= -0.7; // 70%
	} else if (ball.y <= ball.height/2) {
		ball.y = ball.height/2;		
		vy = 0;
	}
}

バウンス:上の例を変更するだけでいいです.
var ball:Ball;
var vx:Number = 0;
var ax:Number = 0;
var vy:Number = 0;
var ay:Number = 0;

ball = new Ball  ;
addChild(ball);
ball.x = stage.stageWidth/2;
ball.y = stage.stageHeight/2;
ball.scaleX = 0.1;
ball.scaleY = 0.1;

addEventListener(Event.ENTER_FRAME,EnterFrameHandler);
stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, KeyUpHandler);

graphics.lineStyle(1,0,1);
graphics.moveTo((stage.stageWidth/2)-10,stage.stageHeight/2);
graphics.lineTo((stage.stageWidth/2)+10,stage.stageHeight/2);
graphics.moveTo(stage.stageWidth/2,(stage.stageHeight/2)-10);
graphics.lineTo(stage.stageWidth/2,(stage.stageHeight/2)+10); 

var _bounce:Number = -0.5;// 

function EnterFrameHandler(event:Event):void {
	vx += ax; 
	ball.x += vx;
	vy += ay; 	
	
	ball.y += vy;
	if (ball.x>=stage.stageWidth-ball.width/2) {
		ball.x=stage.stageWidth-ball.width/2;		
		vx *= _bounce;
	} else if (ball.x <= ball.width/2) {
		ball.x = ball.width/2;		
		vx *= _bounce;
	}	
	if (ball.y>=stage.stageHeight-ball.height/2) {
		ball.y=stage.stageHeight-ball.height/2;		
		vy *= _bounce; 
	} else if (ball.y <= ball.height/2) {
		ball.y = ball.height/2;		
		vy *= _bounce; 
	}
}

function KeyDownHandler(event:KeyboardEvent):void {
	switch(event.keyCode){
		case Keyboard.RIGHT:
		ax = 0.5;
		break;
		case Keyboard.LEFT:
		ax = -0.5;
		break;
		case Keyboard.DOWN:
		ay = 0.5;
		break;
		case Keyboard.UP:
		ay = -0.5;
		default:
		break;
	}	
}

function KeyUpHandler(event:KeyboardEvent):void {
	ax=0;
	ay=0;
}

往復直線運動:
graphics.lineStyle(1,0xff0000,0.5);
graphics.moveTo(0,0);
graphics.lineTo(stage.stageWidth,stage.stageHeight);
graphics.lineStyle(1,0x00ff00,0.5);
graphics.moveTo(stage.stageWidth,0);
graphics.lineTo(0,stage.stageHeight);

var ball:Ball=new Ball(10,0xff0000);
ball.x=0;
ball.y=0;
var _seedX=-90;
var _seedY=0;
var angle:Number = 45* Math.PI/180;
addChild(ball);

var ball2:Ball = new Ball(10,0x00ff00);
ball2.x = stage.stageWidth;
var speed = 2;
var isDown = true;
addChild(ball2);

addEventListener(Event.ENTER_FRAME,EnterFrameHandler);

function EnterFrameHandler(e:Event):void {	
	ball.x = Math.cos(_seedX*Math.PI/180)*stage.stageWidth;	
	ball.y = Math.sin(_seedY*Math.PI/180)*stage.stageHeight;

	_seedX+=1;
	_seedY+=1;

	if (_seedX > 90){
		_seedX = -90;
	}

	if (_seedY > 180){
		_seedY = 0;
	}	
	
	// 
	if (isDown){
		ball2.x -= speed;
		ball2.y += speed;
		if (ball2.x < 0){
			isDown = false;
		}
	}
	else{// 
		ball2.x -= -speed;
		ball2.y += -speed;
		if (ball2.x > stage.stageWidth){
			isDown = true;
		}
	}
	
}

注:往復均一速度の直線運動に対して、最も簡単な方法はx,y軸方向速度に固定値を増加させることである.均一速度が要求されなければsin,cos関数の方が簡単かもしれません
宇宙船キーボード制御のデモ:(ActionScript 3.0 Animationの例から)
宇宙船類
package {
	import flash.display.Sprite;
	public class Ship extends Sprite {
		public function Ship() {
			draw(false);
		}
		public function draw(showFlame:Boolean):void {
			graphics.clear();
			graphics.lineStyle(1,0xffffff);
			graphics.moveTo(10,0);
			graphics.lineTo(-10,10);
			graphics.lineTo(-5,0);
			graphics.lineTo(-10,-10);
			graphics.lineTo(10,0);
			if (showFlame) {
				graphics.moveTo(-7.5,-5);
				graphics.lineTo(-15,0);
				graphics.lineTo(-7.5,5);
			}
		}
	}
}

メインアニメーション
package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	public class ShipSim extends Sprite {
		
		private var ship:Ship;
		private var vr:Number=0;
		private var thrust:Number=0;
		private var vx:Number=0;
		private var vy:Number=0;
		public function ShipSim() {
			init();
		}
		private function init():void {
			ship=new Ship  ;
			ship.scaleX = ship.scaleY = 1.5;
			addChild(ship);
			ship.x=stage.stageWidth/2;
			ship.y=stage.stageHeight/2;
			addEventListener(Event.ENTER_FRAME,EnterFrameHandler);
			stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyDownHandler);
			stage.addEventListener(KeyboardEvent.KEY_UP,KeyUpHandler);
		}
		private function KeyDownHandler(event:KeyboardEvent):void {			
			switch (event.keyCode) {
				case Keyboard.LEFT :
					vr=-5;
					break;
				case Keyboard.RIGHT :
					vr=5;
					break;
				case Keyboard.UP :
					thrust=0.2;
					ship.draw(true);
					break;
				default :
					break;
			}
		}
		private function KeyUpHandler(event:KeyboardEvent):void {
			vr=0;
			thrust=0;
			ship.draw(false);
		}
		private function EnterFrameHandler(event:Event):void {
			ship.rotation+=vr;
			var angle:Number=ship.rotation*Math.PI/180;
			var ax:Number=Math.cos(angle)*thrust;
			var ay:Number=Math.sin(angle)*thrust;			
			vx+=ax;
			vy+=ay;
			ship.x+=vx;
			ship.y+=vy;
			if (ship.x < ship.width/2){
				ship.x = ship.width/2;				
				vx = 0;
			}
			if (ship.x>stage.stageWidth -ship.width/2){
				ship.x = stage.stageWidth -ship.width/2;
				vx = 0;
			}
			if (ship.y > stage.stageHeight - ship.height/2){
				ship.y = stage.stageHeight - ship.height/2;
				vy=0;
			}
			if (ship.y < ship.height/2){
				ship.y = ship.height/2;
				vy=0;
			}
			
		}
	}
}

摩擦による加速回転:
addEventListener(Event.ENTER_FRAME,EnterFrameHandler);
stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP,KeyUpHandler);

var ar:Number=0;
var vr:Number=0;

var _isStart:Boolean=false;// ( )
var _isRight:Boolean=true;// 

function EnterFrameHandler(e:Event):void {

	vr+=ar;
	obj.rotation+=vr;//obj 

	if (!_isStart) {
		if (_isRight) {
			if (vr<=0) {
				vr=0;
				ar=0.0;
			}
		} else {
			if (vr>=0) {
				vr=0;
				ar=0.0;
			}
		}
	}
}

function KeyDownHandler(e:KeyboardEvent):void {
	_isStart=true;

	if (e.keyCode==Keyboard.RIGHT) {
		ar=0.2;
		_isRight=true;
	} else if (e.keyCode == Keyboard.LEFT) {
		ar=-0.2;
		_isRight=false;
	}
}

function KeyUpHandler(e:KeyboardEvent):void {
	if (e.keyCode==Keyboard.RIGHT) {
		ar=-0.1;
	} else if (e.keyCode == Keyboard.LEFT) {
		ar=0.1;
	}
	_isStart=false;
}