回転 : 3D

Pocket

3次元の回転の覚書。

3次元の回転は1軸を固定して2次元の回転と考えることができる。

X軸に沿ったに回転を考える。
YZ平面の座標を極形式で求める。

y = r•cosθ
z = r•sinθ

r = √x2+y2+z2

θ = β回転。

y' = ycosβ - zsinβ
z' = ysinβ + zcosβ

// Point
function Ball(x, y, z) {
	var fl = 250;
	// 3D空間座標	原点=(vpx, vpy, 0)
	var px = x;
	var py = y;
	var pz = z;
	// 3D空間中心座標
	this.pcx;
	this.pcy;
	this.pcz;
	// 消失点
	this.vpx;
	this.vpy;
	// 関数
	this.setVanishingPoint = function(vpx, vpy) {
		this.vpx = vpx;
		this.vpy = vpy;
	};
	this.setCenter = function(pcx, pcy, pcz) {
		this.pcx = pcx;
		this.pcy = pcy;
		this.pcz = pcz;
	};
	this.getscreenX = function() {

		var scale:Number = fl/(fl+pz+this.pcz);
		this._xscale = this._yscale = scale*100;
		return this.vpx+(this.pcx+px)*scale;

	};
	this.getscreenY = function() {
		var scale:Number = fl/(fl+pz+this.pcz);
		return this.vpy+(this.pcy+py)*scale;
	};
	this.rotateX = function(angleX) {
		var cosX:Number = Math.cos(angleX);
		var sinX:Number = Math.sin(angleX);
		var y1:Number = py*cosX-pz*sinX;
		var z1:Number = pz*cosX+py*sinX;
		py = y1;
		pz = z1;
	};
}
// Model
function Model() {
	var ball:MovieClip;
	var vpx:Number = 100;
	var vpy:Number = 200;
	this.init = function() {
		var o = new Ball(100, 100, 0);
		ball = _root.attachMovie('ball', 'ball', _root.getNextHighestDepth(), o);
		ball.setVanishingPoint(vpx, vpy);
		ball.setCenter(0, 0, 0);
		onEnterFrame = moving;
	};
	function moving() {
		_root.clear();
		var angleX:Number = 0.05;
		ball.rotateX(angleX);
		ball._x = ball.getscreenX();
		ball._y = ball.getscreenY();
		_root.lineStyle(1, 0x000000, 100);
		_root.moveTo(vpx, vpy);
		_root.lineTo(ball._x, ball._y);
	}
}
var o = new Model();
o.init();

コメント

No comments yet.

コメントの投稿

改行と段落タグは自動で挿入されます。
メールアドレスは表示されません。