/*
* 3Dオブジェクトのマウス判定テスト
* drawTrianglesではなく2Dでモデリングした面を3D位置に配置してみた
* 結論:画面に沢山球を配置するのは止めよう!丸でなんとかすべし!!
*/
package {
import flash.display.*;
import flash.events.Event;
[SWF(width=400, height=400, backgroundColor=0x000000, frameRate=24)]
public class BallsMouseTest extends Sprite {
public var _world:Sprite;
public var ballA:Ball;
public var ballB:Ball;
public var ballC:Ball;
public var balls_array:Array;
public function BallsMouseTest():void {
_world = new Sprite();
_world.x = stage.stageWidth/2;
_world.y = stage.stageHeight/2;
addChild(_world);
ballA = new Ball();
ballA.radius = 100;
ballA.split = 6;
ballA.offset = 1;
ballA.color = 0x0000ff;
ballA.x = 97;
ballA.y = 50;
ballA.z = -100;
ballA.render();
ballA.rotationZ = -90;
_world.addChild(ballA);
ballB = new Ball();
ballB.radius = 100;
ballB.split = 6;
ballB.offset = 1;
ballB.color = 0xff0000;
ballB.x = -77;
ballB.y = 50;
ballB.z = -100;
ballB.render();
ballB.rotationZ = 90;
_world.addChild(ballB);
ballC = new Ball();
ballC.radius = 50;
ballC.split = 6;
ballC.offset = 0;
ballC.color = 0x00ff00;
ballC.x = 50;
ballC.y = 100;
ballC.z = 100;
ballC.render();
ballC.rotationZ = 90;
_world.addChild(ballC);
balls_array = new Array(ballA, ballB, ballC);
addEventListener(Event.ENTER_FRAME, rotate);
}
public function rotate(eventObject:Event):void {
_world.rotationY += 2;
_world.rotationZ += 1;
var nLength:int = balls_array.length;
for (var i:int=0; i < nLength; i++) {
balls_array[i].pZ = -balls_array[i].transform.getRelativeMatrix3D(this).position.z;
_world.removeChild(balls_array[i]);
}
balls_array.sortOn("pZ", Array.NUMERIC);
for (i=0; i < nLength; ++i) {
_world.addChild(balls_array[i]);
}
}
}
}
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Vector3D;
import flash.geom.Matrix3D;
class Ball extends Sprite {
public var radius:Number;
public var color:Number;
public var split:Number;
public var offset:Number;
public var apex:Vector.<Number>;
public var apexes:Vector.<Number>;
public var indices:Vector.<int>;
public var pZ:Number;
public function Ball():void {
addEventListener(MouseEvent.MOUSE_OVER, onMouse);
addEventListener(MouseEvent.MOUSE_OUT, outMouse);
}
public function render():void {
var matrix:Matrix3D = new Matrix3D();
var splitR:Number = Math.PI/split;
var apexNum:Number = split - offset;
var i:uint;
var nX:Number;
var nY:Number;
var nZ:Number;
var sR:Number;
var nD:Number;
apex = new Vector.<Number>();
var shiftApex:Vector.<Number> = new Vector.<Number>();
for (i = 0; i <= apexNum; i++) {
sR = splitR*i;
nX = radius * Math.sin(sR);
nY = radius * Math.cos(sR);
apex.push(0,nY,nX);
}
splitR /= 2;
matrix.appendRotation(splitR/Math.PI*180,Vector3D.Y_AXIS);
matrix.transformVectors(apex,shiftApex);
for (i = 0; i < apexNum; i++) {
var p1:Vector3D = new Vector3D(shiftApex[3*i], shiftApex[3*i+1], shiftApex[3*i+2]);
var p2:Vector3D = new Vector3D(shiftApex[3*(i+1)],shiftApex[3*(i+1)+1],shiftApex[3*(i+1)+2]);
var centerTop :Vector3D = new Vector3D(0,p1.y,p1.z);
var centerBottom:Vector3D = new Vector3D(0,p2.y,p2.z);
var nH:Number = Vector3D.distance(centerTop,centerBottom);
for (var s:uint = 0; s < split*2; s++) {
var _sprite:Sprite = new Sprite();
_sprite.graphics.beginFill(color);
_sprite.graphics.moveTo(-p1.x, 0);
_sprite.graphics.lineTo( p1.x, 0);
_sprite.graphics.lineTo( p2.x, nH);
_sprite.graphics.lineTo(-p2.x, nH);
_sprite.graphics.lineTo(-p1.x, 0);
_sprite.graphics.endFill();
_sprite.rotationX = Math.atan2(p2.y-p1.y,p2.z-p1.z)/Math.PI*180-90;
_sprite.rotationY = splitR*s/Math.PI*360;
_sprite.y = p1.y;
_sprite.x = -p1.z*Math.sin(splitR*s*2);
_sprite.z = -p1.z*Math.cos(splitR*s*2);
addChild(_sprite);
}
}
}
private function onMouse(eventObject:MouseEvent):void {
alpha = 0.2;
}
private function outMouse(eventObject:MouseEvent):void {
alpha = 1;
}
}