方法一:
方法二:用容器把模型和相机绑定起来
package
{
import alternativa.engine3d.controllers.SimpleObjectController;
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.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;
public class TestCameraController extends Sprite
{
private var rootContainer:Object3DContainer = new Object3DContainer();
private var view:View;
private var camera:Camera3D;
private var box:Box;
private var nextX:Number = 0;
private var nextY:Number = 0;
private static const speed:uint = 10;
private static const RADIAN:Number = Math.PI/180;
private var currenRadian:Number = 90*RADIAN;
private var cameraController:CameraControllerAlternativa3d7;
public function TestCameraController()
{
if (this.stage)
this.init();
else
this.addEventListener(Event.ADDED_TO_STAGE, init);
this.stage.addEventListener(Event.ENTER_FRAME, onRenderTick);
this.stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDown);
this.stage.addEventListener(KeyboardEvent.KEY_UP,keyUp);
this.stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMove);
}
private function init(e:Event=null):void
{
this.removeEventListener(Event.ADDED_TO_STAGE,init);
this.stage.align = StageAlign.TOP_LEFT;
this.stage.scaleMode = StageScaleMode.NO_SCALE;
this.view = new View(this.stage.width,this.stage.height);
this.camera = new Camera3D();
this.camera.y = -300;
this.camera.z = 100;
this.camera.rotationX = -100*RADIAN;
this.camera.view = view;
this.addChild(camera.view);
this.addChild(camera.diagram);
this.rootContainer.addChild(camera);
//plane
var materialPlane:FillMaterial = new FillMaterial(0x598FBB, 1, 1);
var plane:Plane = new Plane(4000,4000,10,10);
plane.setMaterialToAllFaces(materialPlane);
this.rootContainer.addChild(plane);
//box
var materialBox:FillMaterial = new FillMaterial(0x0000FF, 1, 1);
box = new Box(50,100,25);
box.setMaterialToAllFaces(materialBox);
this.rootContainer.addChild(box);
// cameraController = new CameraControllerAlternativa3d7(this.stage,box,10,5);
// cameraController.bindKey(Keyboard.RIGHT, SimpleObjectController.ACTION_YAW_LEFT);
// cameraController.bindKey(Keyboard.LEFT, SimpleObjectController.ACTION_YAW_RIGHT);
// cameraController.mouseSensitivity = 0;
}
private function keyDown(e:KeyboardEvent):void{
switch(e.keyCode)
{
case "W".charCodeAt():
case Keyboard.UP:
nextX = this.box.x + speed * Math.cos(this.currenRadian);
nextY = this.box.y + speed * Math.sin(this.currenRadian);
break;
case "S".charCodeAt():
case Keyboard.DOWN:
nextX = this.box.x - speed * Math.cos(this.currenRadian);
nextY = this.box.y - speed * Math.sin(this.currenRadian);
break;
case "A".charCodeAt():
case Keyboard.LEFT://逆时钟为负
//box.rotationZ += RADIAN;
box.rotationZ += RADIAN;
this.currenRadian += RADIAN;
if(this.box.rotationZ > 360*RADIAN) box.rotationZ = 360*RADIAN - box.rotationZ;
//camera
//this.camera.rotationZ += RADIAN;
//if(this.camera.rotationZ > 360*RADIAN) this.camera.rotationZ = 360*RADIAN - this.camera.rotationZ;
break;
case "D".charCodeAt():
case Keyboard.RIGHT://顺时钟为正
//box.rotationZ -= RADIAN;
box.rotationZ -= RADIAN;
this.currenRadian -= RADIAN;
if(box.rotationZ > 360*RADIAN) box.rotationZ = 360*RADIAN + box.rotationZ;
//camera
//this.camera.rotationZ -= RADIAN;
//if(this.camera.rotationZ > 360*RADIAN) this.camera.rotationZ = 360*RADIAN + this.camera.rotationZ;
break;
}
}
private function keyUp(e:KeyboardEvent):void{
switch(e.keyCode)
{
case "W".charCodeAt():
case Keyboard.UP:
break;
case "S".charCodeAt():
case Keyboard.DOWN:
break;
case "A".charCodeAt():
case Keyboard.LEFT:
break;
case "D".charCodeAt():
case Keyboard.RIGHT:
break;
}
}
private function mouseMove(evt:MouseEvent):void
{
}
private function onRenderTick(e:Event=null):void
{
this.cameraUpdate();
this.boxUpdate();
//this.cameraController.update();
}
private function cameraUpdate():void
{
this.camera.view.width = this.stage.stageWidth;
this.camera.view.height = this.stage.stageHeight;
this.camera.render();
//追尾
this.camera.rotationZ = this.box.rotationZ;
this.camera.x = this.box.x - 300 * Math.sin(this.box.rotationZ + 180 * Math.PI / 180);
this.camera.y = this.box.y + 300 * Math.cos(this.box.rotationZ + 180 * Math.PI / 180);
//this.camera.z = 100;
}
private function boxUpdate():void
{
box.x = this.nextX;
box.y = this.nextY;
}
}
}
方法二:用容器把模型和相机绑定起来
package
{
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.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;
public class BoxCameraController extends Sprite
{
private var rootContainer:Object3DContainer = new Object3DContainer();
private var boxCameraContainer:Object3DContainer = new Object3DContainer();
private var view:View;
private var camera:Camera3D;
private var box:Box;
private var nextX:Number = 0;
private var nextY:Number = 0;
private static const speed:uint = 10;
private static const RADIAN:Number = Math.PI/180;
private var currenRadian:Number = 90*RADIAN;
public function BoxCameraController()
{
if (this.stage)
this.init();
else
this.addEventListener(Event.ADDED_TO_STAGE, init);
this.stage.addEventListener(Event.ENTER_FRAME, onRenderTick);
this.stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDown);
}
private function init(e:Event = null):void
{
this.removeEventListener(Event.ADDED_TO_STAGE,init);
this.stage.align = StageAlign.TOP_LEFT;
this.stage.scaleMode = StageScaleMode.NO_SCALE;
this.view = new View(this.stage.width,this.stage.height);
this.camera = new Camera3D();
this.camera.y = -250;
this.camera.z = 100;
this.camera.rotationX = -100*RADIAN;
this.camera.view = view;
this.addChild(camera.view);
this.addChild(camera.diagram);
this.boxCameraContainer.addChild(camera);
//plane
var materialPlane:FillMaterial = new FillMaterial(0x598FBB, 1, 1);
var plane:Plane = new Plane(4000,4000,10,10);
plane.setMaterialToAllFaces(materialPlane);
this.rootContainer.addChild(plane);
//box
var materialBox:FillMaterial = new FillMaterial(0x0000FF, 1, 1);
box = new Box(50,100,25);
//box.z = 200;
box.setMaterialToAllFaces(materialBox);
this.boxCameraContainer.addChild(box);
this.rootContainer.addChild(this.boxCameraContainer);
}
private function keyDown(e:KeyboardEvent):void{
switch(e.keyCode)
{
case "W".charCodeAt():
case Keyboard.UP:
nextX = this.boxCameraContainer.x + speed * Math.cos(this.currenRadian);
nextY = this.boxCameraContainer.y + speed * Math.sin(this.currenRadian);
break;
case "S".charCodeAt():
case Keyboard.DOWN:
nextX = this.boxCameraContainer.x - speed * Math.cos(this.currenRadian);
nextY = this.boxCameraContainer.y - speed * Math.sin(this.currenRadian);
break;
case "A".charCodeAt():
case Keyboard.LEFT://逆时钟为负
boxCameraContainer.rotationZ += RADIAN;
this.currenRadian += RADIAN;
if(this.boxCameraContainer.rotationZ > 360*RADIAN) boxCameraContainer.rotationZ = 360*RADIAN - boxCameraContainer.rotationZ;
break;
case "D".charCodeAt():
case Keyboard.RIGHT://顺时钟为正
boxCameraContainer.rotationZ -= RADIAN;
this.currenRadian -= RADIAN;
if(boxCameraContainer.rotationZ > 360*RADIAN) boxCameraContainer.rotationZ = 360*RADIAN + boxCameraContainer.rotationZ;
break;
}
}
private function onRenderTick(e:Event):void
{
this.camera.view.width = this.stage.stageWidth;
this.camera.view.height = this.stage.stageHeight;
camera.render();
boxCameraContainer.x = this.nextX;
boxCameraContainer.y = this.nextY;
}
}
}