3d in Macromedia Flash MX

This is one of experiments written in 2003.

Please drag the object or use arrow keys to navigate. Up and Down buttons change the focus.

//--------------------------------------------------
// Rotation of a point around the OZ axis
//--------------------------------------------------
function rotateOZ(point, angle) {
	alpha = angle/180*Math.PI;
	x = point[0];
	y = point[1];
	z = point[2];
	xn = (x-.5)*Math.cos(alpha)-(y-.5)*Math.sin(alpha)+.5;
	yn = (x-.5)*Math.sin(alpha)+(y-.5)*Math.cos(alpha)+.5;
	return Array(xn, yn, z);
}
//--------------------------------------------------
// Rotation of a point around the OX axis
//--------------------------------------------------
function rotateOX(point, angle) {
	alpha = angle/180*Math.PI;
	x = point[0];
	y = point[1];
	z = point[2];
	yn = (y-.5)*Math.cos(alpha)-(z-.5)*Math.sin(alpha)+.5;
	zn = (y-.5)*Math.sin(alpha)+(z-.5)*Math.cos(alpha)+.5;
	return Array(x, yn, zn);
}
//--------------------------------------------------
// Rotation of a point around the OY axis
//--------------------------------------------------
function rotateOY(point, angle) {
	alpha = angle/180*Math.PI;
	x = point[0];
	y = point[1];
	z = point[2];
	xn = (x-.5)*Math.cos(alpha)-(z-.5)*Math.sin(alpha)+.5;
	zn = (x-.5)*Math.sin(alpha)+(z-.5)*Math.cos(alpha)+.5;
	return Array(xn, y, zn);
}
//--------------------------------------------------
// Projection of a point onto the screen
//--------------------------------------------------
function project(point, distance) {
	x = point[0];
	y = point[1];
	z = point[2];
	//
	delete point;
	//
	x = (x*distance)/(distance+z);
	y = (y*distance)/(distance+z);
	return Array(x, y);
}
//--------------------------------------------------
// Setting the initial values
//--------------------------------------------------
deltaX = deltaY=0;
xRot = yRot=zRot=0;
dist = 1;
//--------------------------------------------------
// Setting the coordinates of the corners of the
// cube
//--------------------------------------------------
cube = new Array(new Array(0, 0, 0), new Array(1, 0, 0), new Array(1, 1, 0), new Array(0, 1, 0), new Array(0, 0, 1), new Array(1, 0, 1), new Array(1, 1, 1), new Array(0, 1, 1));
//--------------------------------------------------
// Setting the variable mDown=true if the mouse is 
// pushed
//--------------------------------------------------
_root.onMouseDown = function() {
	mDown = true;
};
//--------------------------------------------------
// Setting the variable mDown=false if the mouse is 
// released
//--------------------------------------------------
_root.onMouseUp = function() {
	mDown = false;
};
//--------------------------------------------------
// This function called in a very small interval of 
// time refreshes the picture.
//--------------------------------------------------
function drawPict() {
	// Changing the distance from the cube
	if (Key.isDown(Key.DOWN)) {
		dist -= .1;
	}
	if (Key.isDown(Key.UP)) {
		dist += .1;
	}
	// The rotation parameters
	if (Key.isDown(Key.LEFT)) {
		zRot = +3;
	} else if (Key.isDown(Key.RIGHT)) {
		zRot = -3;
	} else {
		zRot = 0;
	}
	if (mDown) {
		deltaX = mXLast-_root._xmouse;
		deltaY = mYLast-_root._ymouse;
	}
	mXLast = _root._xmouse;
	mYLast = _root._ymouse;
	xRot = deltaX;
	yRot = deltaY;
	vertex = new Array();
	for (i=0; i<8; i++) {
		coordinates = rotateOY(rotateOX(rotateOZ(cube[i], zRot), yRot), xRot);
		if (zRot) {
			coordinates = rotateOZ(coordinates, zRot);
		}
		cube[i] = coordinates;
		coordinates = project(coordinates, dist);
		vertex[i] = new Array(coordinates[0]*100+100, coordinates[1]*100+100);
	}
	delete coordinates;
	_root.createEmptyMovieClip("mycube", 0);
	with (_root.mycube) {
		lineStyle(1, 0x000000);
		// 1st rectangle
		beginFill(0x0066CC, 30);
		moveTo(vertex[0][0], vertex[0][1]);
		lineTo(vertex[1][0], vertex[1][1]);
		lineTo(vertex[2][0], vertex[2][1]);
		lineTo(vertex[3][0], vertex[3][1]);
		lineTo(vertex[0][0], vertex[0][1]);
		endFill();
		// 2nd rectangle
		beginFill(0xCC0000, 30);
		moveTo(vertex[4][0], vertex[4][1]);
		lineTo(vertex[5][0], vertex[5][1]);
		lineTo(vertex[6][0], vertex[6][1]);
		lineTo(vertex[7][0], vertex[7][1]);
		lineTo(vertex[4][0], vertex[4][1]);
		endFill();
		// 3rd rectangle
		beginFill(0x66CC00, 30);
		moveTo(vertex[0][0], vertex[0][1]);
		lineTo(vertex[4][0], vertex[4][1]);
		lineTo(vertex[5][0], vertex[5][1]);
		lineTo(vertex[1][0], vertex[1][1]);
		lineTo(vertex[0][0], vertex[0][1]);
		endFill();
		// 4th rectangle
		beginFill(0x3333CC, 30);
		moveTo(vertex[1][0], vertex[1][1]);
		lineTo(vertex[5][0], vertex[5][1]);
		lineTo(vertex[6][0], vertex[6][1]);
		lineTo(vertex[2][0], vertex[2][1]);
		lineTo(vertex[1][0], vertex[1][1]);
		endFill();
		// 6th rectangle
		beginFill(0xFF9933, 30);
		moveTo(vertex[2][0], vertex[2][1]);
		lineTo(vertex[6][0], vertex[6][1]);
		lineTo(vertex[7][0], vertex[7][1]);
		lineTo(vertex[3][0], vertex[3][1]);
		lineTo(vertex[2][0], vertex[2][1]);
		endFill();
		// 7th rectangle
		beginFill(0x3399FF, 30);
		moveTo(vertex[3][0], vertex[3][1]);
		lineTo(vertex[7][0], vertex[7][1]);
		lineTo(vertex[4][0], vertex[4][1]);
		lineTo(vertex[0][0], vertex[0][1]);
		lineTo(vertex[3][0], vertex[3][1]);
		endFill();
	}
	delete vertex;
}
setInterval(drawPict, 50);

Powered by PepisCMS