3Dの回転 + 線
/**
* Copyright _wonder ( http://wonderfl.net/user/_wonder )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/iUF3
*/
// forked from _wonder's 3Dの回転
// forked from _wonder's 複数のボール跳ね返り3D
// forked from _wonder's 跳ね返り3D
// forked from _wonder's ボールを動かす3D
// forked from _wonder's 遠ざかるボール
// forked from _wonder's base
package {
import flash.display.Sprite;
import flash.events.Event;
public class MultiBound extends Sprite {
private var balls:Array;
private var numBalls:Number = 100;
private var fl:Number = 250;
private var vpX:Number = stage.stageWidth / 2;
private var vpY:Number = stage.stageHeight / 2;
public function MultiBound() {
balls = new Array();
for( var i:uint = 0; i < numBalls; i++ ){
var ball:Ball3D = new Ball3D( 5, 0 );
balls.push( ball );
ball.xpos = Math.random() * 200 - 100;
ball.ypos = Math.random() * 200 - 100;
ball.zpos = Math.random() * 200 - 100;
addChild( ball );
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(e:Event):void {
for( var i:uint = 0; i < numBalls; i++ ){
var angleY:Number = ( mouseX - vpX ) * 0.0005;
var angleX:Number = ( mouseY - vpY ) * 0.0005;
var ball:Ball3D = balls[i];
rotateX( ball, angleX );
rotateY( ball, angleY );
doPerspective( ball );
}
graphics.clear();
graphics.lineStyle( 0 );
graphics.moveTo( balls[0].x, balls[0].y );
for( i = 0; i < numBalls; i++ ){
graphics.lineTo( balls[i].x, balls[i].y );
}
sortZ();
}
private function rotateY(ball:Ball3D, angleY:Number):void {
var cosY:Number = Math.cos( angleY );
var sinY:Number = Math.sin( angleY );
var x1:Number = ball.xpos * cosY - ball.zpos * sinY;
var z1:Number = ball.zpos * cosY + ball.xpos * sinY;
ball.xpos = x1;
ball.zpos = z1;
}
private function rotateX(ball:Ball3D, angleX:Number):void {
var cosX:Number = Math.cos( angleX );
var sinX:Number = Math.sin( angleX );
var y1:Number = ball.ypos * cosX - ball.zpos * sinX;
var z1:Number = ball.zpos * cosX + ball.ypos * sinX;
ball.ypos = y1;
ball.zpos = z1;
}
private function doPerspective(ball:Ball3D):void {
if( ball.zpos > -fl ){
var scale:Number = fl / ( fl + ball.zpos );
ball.scaleX = ball.scaleY = scale;
ball.x = vpX + ball.xpos * scale;
ball.y = vpX + ball.ypos * scale;
ball.visible = true;
} else {
ball.visible = false;
}
}
private function sortZ():void {
balls.sortOn( "zpos", Array.DESCENDING | Array.NUMERIC );
for( var i:uint = 0; i < numBalls; i++ ){
var ball:Ball3D = balls[i];
setChildIndex( ball, i );
}
}
}
}
import flash.display.Sprite;
class Ball3D extends Sprite {
public var radius:Number;
public var color:uint;
public var xpos:Number = 0;
public var ypos:Number = 0;
public var zpos:Number = 0;
public var vx:Number = 0;
public var vy:Number = 0;
public var vz:Number = 0;
public var mass:Number = 1;
public function Ball3D(radius:Number=40, color:uint=0Xff0000){
this.radius = radius;
this.color = color;
init();
}
public function init():void {
graphics.lineStyle( 0 );
graphics.beginFill(color);
graphics.drawCircle(0, 0, radius);
graphics.endFill();
}
}