方法一:
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;
}
}
}
分享到:
相关推荐
Alternativa3D实现红蓝立体代码
alternativa3d 灯光 实现。。源码
alternativa3D 7.5 加载并播放 dae格式的模型文件(完整源代码),附:流星蝴蝶剑 孟星魂模型及贴图、alternativa3D 7.5 swc。
Alternativa3D-master 8.32 Alternativa3D-master 8.32 Alternativa3D-master 8.32
Alternativa3D 8 graphics Flash 3D engine is out in Open Source. 业内著名的A3d开源了,不可错过啊;flash 3d引擎。
Alternativa3D 8的到来意味着 Flash 真正踏入3D时代 。这就是Alternativa3D最新版本的中文帮助文档,非常实用。
Alternativa3d 官方教程中文翻译
Alternativa3D 官方中文翻译api
Alternativa3D7的中文教程5篇,源码无份,以及英文API
Alternativa3d官方教程翻译版本 Alternativa3d官方教程翻译版本
Alternativa3d API Actionscript3
Alternativa3D 开源的3d引擎demo
alternativa3d API希望能为大家提供方便!
3DSmax2011导出Alternativa3D_8,适用32位和64位系统。
Alternativa3D SWC
flash Alternativa 3D 基础 理论flash Alternativa 3D 基础 理论
《创建 Alternativa3D 8 环境》中的示例代码,教程地址:http://www.chenlinsheng.com/?p=136
flash 3d 插件 alternativa3d 5.6.0
东西不错!!用过最好的flash3d引擎
著名的用ActionScript3.0编写的Flash 3D引擎,含语言手册及官方教程中文翻译。