package { import alternativa.engine3d.containers.BSPContainer; import alternativa.engine3d.core.Camera3D; import alternativa.engine3d.core.Object3DContainer; import alternativa.engine3d.core.View; import alternativa.engine3d.materials.FillMaterial; import alternativa.engine3d.primitives.Box; import alternativa.engine3d.primitives.Plane; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.geom.Matrix3D; import flash.geom.Vector3D; [SWF(width="600",height="400")] public class Alternativa2Correct extends Sprite { private var container:BSPContainer = new BSPContainer(); private var camera:Camera3D; private var plane:Plane; private var box:Box; private var lastX:int; private var lastY:int; private var isDragging:Boolean = false; public function Alternativa2Correct() { super(); camera = new Camera3D(); camera.view = new View(stage.stageWidth, stage.stageHeight); camera.rotationX = 45*Math.PI/180; camera.y = 500; camera.z = -500; // camera.debug = true; addChild(camera.view); //addChild(camera.diagram); container.addChild(camera); plane = new Plane(1000,1000,10,10); plane.setMaterialToAllFaces(new FillMaterial(0xCCCCCC,0.7,1)); container.addChild(plane); box = new Box(200,200,200,2,2,2); box.setMaterialToAllFaces(new FillMaterial(0xFF0000,1,1)); container.addChild(box); stage.addEventListener(Event.ENTER_FRAME, onEnterFrame); stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); camera.render(); } private function onEnterFrame(event:Event):void{ //box.rotationZ += 0.05; } private function onMouseDown(event:MouseEvent):void{ isDragging = true; lastX = event.stageX; lastY = event.stageY; } private function onMouseUp(event:MouseEvent):void{ isDragging = false; } private function onMouseMove(event:MouseEvent):void{ if(!isDragging) return; var deltaX:int = lastX - event.stageX; var deltaY:int = lastY - event.stageY; lastX = event.stageX; lastY = event.stageY; var matrix:Matrix3D = box.matrix; matrix.appendRotation(deltaX,new Vector3D(0,0,1)); matrix.appendRotation(-deltaY,new Vector3D(1,0,0)); box.matrix = matrix; camera.render(); } } }