package { import away3d.containers.ObjectContainer3D; import away3d.containers.View3D; import away3d.debug.AwayStats; import away3d.primitives.WireframePlane; import away3d.primitives.WireframeSphere; import awayphysics.collision.shapes.AWPBoxShape; import awayphysics.collision.shapes.AWPSphereShape; import awayphysics.dynamics.AWPDynamicsWorld; import awayphysics.dynamics.AWPRigidBody; import flash.display.Sprite; import flash.events.Event; import flash.geom.Vector3D; import flash.utils.getTimer; [SWF(backgroundColor = "#000000", frameRate = "60", width = "720", height = "480")] public class SphereVer extends Sprite { //---------------------------------------------------------- // // Constructor // //---------------------------------------------------------- public function SphereVer() { if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } //---------------------------------------------------------- // // Property // //---------------------------------------------------------- private var view3D:View3D; private var cnt:int = 0; private var collisions:Vector.; private var physicsWorld:AWPDynamicsWorld; private var timeStep:Number = 1.0 / 60; //---------------------------------------------------------- // // Function // //---------------------------------------------------------- private function init(e:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); view3D = new View3D(); view3D.antiAlias = 4; view3D.camera.lens.far = 20000; this.addChild(view3D); this.addChild(new AwayStats(view3D)); // init the physics world physicsWorld = AWPDynamicsWorld.getInstance(); physicsWorld.initWithDbvtBroadphase(); physicsWorld.gravity = new Vector3D(0, -40, 0); // create the terrain mesh var earth:ObjectContainer3D = new ObjectContainer3D(); var wire:WireframePlane = new WireframePlane(40000, 40000, 40, 40, 0x888888); wire.rotationZ = 90; earth.addChild(wire); view3D.scene.addChild(earth); // create the terrain shape and rigidbody var earthShape:AWPBoxShape = new AWPBoxShape(40000, 100, 40000); var earthBody:AWPRigidBody = new AWPRigidBody(earthShape, earth, 0); earthBody.friction = 0.1; earthBody.restitution = 1; physicsWorld.addRigidBody(earthBody); reset(); addEventListener(Event.ENTER_FRAME, enterFrameHandler); } private function enterFrameHandler(e:Event):void { if (cnt++ % 600 == 599) reset(); view3D.camera.x=2000 * Math.sin(getTimer() / 7000); view3D.camera.y=1200 + 500 * Math.sin(getTimer() / 3000); view3D.camera.z=2000 * Math.cos(getTimer() / 7000); view3D.camera.lookAt(new Vector3D(0, 400, 0)); physicsWorld.step(timeStep); view3D.render(); } private function reset():void { var i:int; if (collisions) { for (i = 0; i < collisions.length; i++) { view3D.scene.removeChild(collisions[i].skin); physicsWorld.removeRigidBody(collisions[i]); } } // create rigidbody shapes var sphereShape:AWPSphereShape = new AWPSphereShape(100); // create rigidbodies var mesh:ObjectContainer3D; // var shape:AWPShape; var body:AWPRigidBody; collisions = new Vector.(); for (i = 0; i < 200; i++) { // create cylinders mesh = new WireframeSphere(100, 12, 8, 0xFF0000, 1); view3D.scene.addChild(mesh); body = new AWPRigidBody(sphereShape, mesh, 1); body.position = resetPosition(); body.restitution = 0.7; physicsWorld.addRigidBody(body); collisions.push(body); } collisions.fixed = true; } private function resetPosition():Vector3D { const ROUND:int = 1000; return new Vector3D(ROUND * (Math.random() - 0.5), 1000 + 5000 * Math.random(), ROUND * (Math.random() - 0.5)) } } }