Test13 - Avatar Demo
From Flare3D - WIKI
package { import flare.basic.*; import flare.collisions.*; import flare.core.*; import flare.materials.*; import flare.materials.filters.*; import flare.physics.core.AvatarController; import flare.physics.core.PhysicsBox; import flare.physics.core.PhysicsMesh; import flare.physics.core.PhysicsSphere; import flare.physics.core.PhysicsSystemManager; import flare.physics.core.RigidBody; import flare.physics.tools.MeshUtils; import flare.system.*; import flare.utils.Mesh3DUtils; import flare.utils.Pivot3DUtils; import flash.display.*; import flash.events.*; import flash.geom.*; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFormat; [SWF(frameRate = 30, width = 800, height = 450, backgroundColor = 0x000000)] public class Test13_AvatarControllerDemo extends Sprite { private var scene:Scene3D; private var player:Pivot3D; private var collisions:SphereCollision; private var light0:Light3D; private var physics:PhysicsSystemManager; private var avatar:AvatarController; public function Test13_AvatarControllerDemo() { scene = new Scene3D(this); scene.pause(); // set the color to clear the back buffer. scene.clearColor.setTo( 0.7, 0.4, 0.2 ); // set the antialias value 1, 2, 3...8. scene.antialias = 0; // deactivate the default lights, we will use the lights included on the map file. scene.defaultLight = null // Config the amount of dynamic lights scene.lights.maxPointLights = 4; // we don't have directional lights, so we set it to 0. scene.lights.maxDirectionalLights = 0; // and set the ambient color. scene.lights.ambientColor = new Vector3D( 0.1, 0.1, 0.1 ); //add a complete event listener scene.addEventListener( Scene3D.COMPLETE_EVENT, completeEvent ); // load the map. scene.addChildFromFile( "../resources/map.f3d" ); //load the visual for the avatar player = scene.addChildFromFile("../resources/robot.f3d"); //load a ball representation for the objects scene.addChildFromFile("../resources/ball.f3d"); //init the physics engine physics = PhysicsSystemManager.getInstance(); //This demo use centimeters, so we set gravity to 981 cms. physics.gravity = new Vector3D(0, -981, 0); physics.accuracy = PhysicsSystemManager.MEDIUM_ACCURACY; //Set thresholds for desactivating objects . These parameters allow user to configurate how fast the objects become inactive. PhysicsSystemManager.orientThreshold = 3; PhysicsSystemManager.posThreshold = 3; PhysicsSystemManager.velThreshold = 3 ; // stage configuration. stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT; stage.addEventListener( Event.RESIZE, resizeEvent ); //Show label for key controls var controls: TextField = new TextField(); controls.autoSize = TextFieldAutoSize.LEFT; controls.defaultTextFormat = new TextFormat( "Arial" , 12, 0xffffff); addChild(controls); controls.htmlText = "W, A, D, S: Move the avatar" + String.fromCharCode(13) + "SHIFT: Run" + String.fromCharCode(13) + "O: Add object" + String.fromCharCode(13) + "F1: Reset position"; } //create the physical representation for the objects in the scene. private function createPhysicsObjects():void { scene.getChildByName("ball.f3d").visible = false; scene.getChildByName("ball.f3d").setScale(3, 3, 3); //create physics meshes for all childrens of 'map.f3d' var tmp : Pivot3D = scene.getChildByName("map.f3d"); //stop any animation tmp.gotoAndStop(0); for (var i:int = 0; i < tmp.children.length; i++ ) { if (!(tmp.children[i] as Mesh3D)) continue; var floorPhysics: RigidBody = new PhysicsMesh(); tmp.children[i].addComponent(floorPhysics); //configure the restitution of scenario floorPhysics.restitution = 0.2; } //set initial position for the avatar player.setPosition( 0, 30, 0 ); //Create the avatar controller instance and link it to the pivot3D avatar = new AvatarController(); player.addComponent(avatar); //Physics needs object to be centered at (0,0,0). Robot Y pos is displaced var mat:Matrix3D = new Matrix3D(); mat.appendTranslation(0, -avatar.collisionPrimitive.boundingSphere, 0); avatar.initialTransformation = mat; } private function resizeEvent(e:Event):void { // if the stage change his size, resize the scene3d too. scene.setViewport( 0, 0, stage.stageWidth, stage.stageHeight, scene.antialias ); } private function completeEvent(e:Event):void { // get access to one of the lights to turn on/off the light on the game. light0 = scene.getChildByName( "Omni032" ) as Light3D; //instanciate all physics objects createPhysicsObjects(); // once the scene is completly loaded, start to update the game!. scene.addEventListener( Scene3D.UPDATE_EVENT, updateEvent ); scene.resume(); } private var speed:Vector3D = new Vector3D(); private function updateEvent(e:Event):void { // simulate the broken light. if ( Math.random() > 0.25 ) light0.visible = true else light0.visible = false; // swith between diferent light configurations. // by default is seted to sampled lights. each method has diferent behaviors and properties we'll detail later. // they are sorted from low to high performance impact. if ( Input3D.keyHit( Input3D.NUMBER_1 ) ) scene.lights.techniqueName = LightFilter.NO_LIGHTS; if ( Input3D.keyHit( Input3D.NUMBER_2 ) ) scene.lights.techniqueName = LightFilter.PER_VERTEX; if ( Input3D.keyHit( Input3D.NUMBER_3 ) ) scene.lights.techniqueName = LightFilter.LINEAR; if ( Input3D.keyHit( Input3D.NUMBER_4 ) ) scene.lights.techniqueName = LightFilter.SAMPLED; //BEGIN : Map events to Avatar methods //Go forward if (Input3D.keyDown(Input3D.W)) { //Manage if the avatar is running or walking if (Input3D.keyDown(Input3D.SHIFT)) avatar.move(300); else avatar.move(160); } //Go backward if (Input3D.keyDown(Input3D.S)) avatar.move(-160); //Turn left if (Input3D.keyDown(Input3D.A)) avatar.turn( -4); //Turn right if (Input3D.keyDown(Input3D.D)) avatar.turn(4); //go to initial position if (Input3D.keyHit(Input3D.F1)) { avatar.setPosition(0, avatar.collisionPrimitive.boundingSphere * 1.25, 0); avatar.setActive(true); } //END:Map events to Avatar methods //Add some objects to the scene if (Input3D.keyHit(Input3D.O)) { addBall(avatar.x + 100 * (Math.random() - 0.5), avatar.collisionPrimitive.boundingSphere * 2, avatar.z + 100 * (Math.random() - 0.5)); } //do a physics step physics.step(); //calculate the speed of the avatar's animation speed.copyFrom(avatar.getVelocity()); speed.y = 0; player.frameSpeed = speed.length / 160; // orientate the camera to player position. Pivot3DUtils.setPositionWithReference( scene.camera, 0, 2.5 * avatar.collisionPrimitive.boundingSphere , -avatar.collisionPrimitive.boundingSphere * 7.5, player, 0.1 ) Pivot3DUtils.lookAtWithReference( scene.camera, 0, avatar.collisionPrimitive.boundingSphere * 1.75, 0, player, new Vector3D(0, 1, 0), 0.5 ); } //Add a ball private function addBall(x : Number, y: Number, z: Number):void { var o3d: Pivot3D = scene.getChildByName("ball.f3d").clone(); o3d.setPosition(x, y, z); o3d.visible = true; scene.addChild(o3d); o3d.addComponent(new PhysicsSphere()); } } }