等候已久的Flash 3D physics Engine出現啦
名稱為:jiglibflash
http://code.google.com/p/jiglibflash/
結合PV3D直接使用, 開心, 不需要再重學。
使用方法也很簡單
在建立PV3D物件後, 再建立對映的Physics物件
如:
new Cube之後
再建立一個JBox來對映
目前支援的碰撞物件有,球體、方塊、Plane。
也支援鏈結的效果。
奶小茶將官方的Demo改寫成自已的Demo。
原始碼:
名稱為:jiglibflash
http://code.google.com/p/jiglibflash/
結合PV3D直接使用, 開心, 不需要再重學。
使用方法也很簡單
在建立PV3D物件後, 再建立對映的Physics物件
如:
new Cube之後
再建立一個JBox來對映
目前支援的碰撞物件有,球體、方塊、Plane。
也支援鏈結的效果。
奶小茶將官方的Demo改寫成自已的Demo。
原始碼:
/* Example Flash 3D Physics version 0.25 with timestep Control by : arrow keys and space bar 3D physics used : jiglibflash (http://code.google.com/p/jiglibflash/) 3D renderer used : Papervision 3D (http://code.google.com/p/papervision3d/) Credits : Muzer Demo by : milkmidi */ package { import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageQuality; import flash.display.StageScaleMode; import flash.events.Event; import flash.events.MouseEvent; import flash.utils.getTimer; import jiglib.geometry.JBox; import jiglib.geometry.JSphere; import jiglib.math.JMatrix3D; import jiglib.math.JNumber3D; import jiglib.physics.PhysicsSystem; import jiglib.physics.constraint.JConstraintPoint; import org.papervision3d.lights.PointLight3D; import org.papervision3d.materials.shadematerials.FlatShadeMaterial; import org.papervision3d.materials.special.ParticleMaterial; import org.papervision3d.materials.utils.MaterialsList; import org.papervision3d.objects.DisplayObject3D; import org.papervision3d.objects.primitives.Cube; import org.papervision3d.objects.primitives.Sphere; import org.papervision3d.objects.special.ParticleField; import org.papervision3d.render.BasicRenderEngine; import org.papervision3d.scenes.Scene3D; import org.papervision3d.view.Viewport3D; import org.papervision3d.view.layer.ViewportLayer; import org.papervision3d.view.layer.util.ViewportLayerSortMode; import org.papervision3d.view.stats.StatsView; import org.papervision3d.materials.ColorMaterial; import org.papervision3d.view.BasicView; [SWF(width = "950", height = "500", frameRate = "41", backgroundColor = "#000000", pageTitle = "milkmidi.com")] public class MyDemo extends BasicView { // ------------------------------------------------------------------------ // DECLARE VARIABLES // ------------------------------------------------------------------------ // Settings private var amountBalls : uint = 3; // default 3, amount of balls in the scene the first will be the one you can control with the arrows keys private var amountBoxes : uint = 3; // default 3, number of boxes in the scene private var amountSphereInChain : uint = 5; // default 5, number of sphere's in the chain private var speedBall : int = 50; // defailt 50, speed of the ball which you can control with the arrow keys private var physicsSpeed : Number = 5; // default 5, speed * deltatime private var sceneLight : PointLight3D; private var jBoxArray : Array = new Array(); // timer private var deltaTime:Number = 0; private var currentTime:uint; private var newTime:uint; private var vplObjects:ViewportLayer; // ------------------------------------------------------------------------ // MAIN CONSTRUCTOR // ------------------------------------------------------------------------ public function MyDemo() { super(0, 0, true, false); init(); startRendering(); } // ------------------------------------------------------------------------ // INIT // ------------------------------------------------------------------------ private function init():void { init3DEngine(); initJiglib(); init3DObject(); init2DObject(); createLotOfBox(); } // ------------------------------------------------------------------------ // SETUP STAGE // ------------------------------------------------------------------------ private function setupStage():void { stage.align = StageAlign.TOP_LEFT; stage.scaleMode = StageScaleMode.NO_SCALE; stage.quality = StageQuality.MEDIUM; stage.frameRate = 60; } // ------------------------------------------------------------------------ // INIT PAPERVISION 3D // ------------------------------------------------------------------------ private function initJiglib():void { //PhysicsSystem.getInstance().SetSolverType("FAST"); currentTime = getTimer(); // set time for physic loop } // ------------------------------------------------------------------------ // INIT PAPERVISION 3D // ------------------------------------------------------------------------ private function init3DEngine():void { viewport.containerSprite.sortMode = ViewportLayerSortMode.INDEX_SORT; // set viewport sortmode to index sceneLight = new PointLight3D(true, true); sceneLight.x = 0; sceneLight.y = 400; sceneLight.z = -300; camera.x = 0; camera.y = 450; camera.z = -700; camera.zoom = 10; camera.focus = 100; this.addEventListener(Event.ENTER_FRAME, onEnterFrame); } private function init3DObject():void { // SETUP GROUND var materia1Ground:MaterialsList = new MaterialsList(); materia1Ground.addMaterial(new FlatShadeMaterial(sceneLight, 0x77ee77), "all"); var pv3dGround:Cube = new Cube(materia1Ground, 600, 600, 50, 2, 2, 2) scene.addChild(pv3dGround); // Viewport layer ground var vplGround:ViewportLayer = viewport.getChildLayer(pv3dGround); vplGround.layerIndex = 1; // set index to 1 // Ground physics var jGround:JBox = new JBox(pv3dGround, false, 600, 600, 50); PhysicsSystem.getInstance().AddBody(jGround); // SETUP WALL var materia1Wall:MaterialsList = new MaterialsList(); materia1Wall.addMaterial(new FlatShadeMaterial(sceneLight, 0x77ee77), "all"); var pv3dWall:Cube = new Cube(materia1Wall, 600, 50, 250); scene.addChild(pv3dWall); // viewport layer wall var vplWall:ViewportLayer = viewport.getChildLayer(pv3dWall); vplWall.layerIndex = 2; // set index to 1 // Ground physics var jWall:JBox = new JBox(pv3dWall,false,600,50,250); jWall.MoveTo(new JNumber3D(0,150,275), JMatrix3D.IDENTITY); // position wall north: y = wall.height/2 + wall.depth/2, z = ground.depth/2 + wall.depth/2 PhysicsSystem.getInstance().AddBody(jWall); vplObjects = new ViewportLayer(viewport,null); vplObjects.layerIndex = 3; vplObjects.sortMode = ViewportLayerSortMode.Z_SORT; // sort mode for this viewportlayer based on z viewport.containerSprite.addLayer(vplObjects); } private function createLotOfBox():void { if (jBoxArray.length > 0) { for (var i:int = 0; i < jBoxArray.length; i++) { scene.removeChildByName("cube" + i); PhysicsSystem.getInstance().RemoveBody(jBoxArray[i]); } } jBoxArray = new Array(); var cube:Cube; for (i = 0; i < 12; i++) { var flatMat:FlatShadeMaterial = new FlatShadeMaterial(sceneLight, Math.random() * 0xffffff, 0xffffff); var ml:MaterialsList = new MaterialsList( { all:flatMat } ); var _scale:Number = Math.random() * 50 + 10; cube = new Cube(ml, _scale, _scale, _scale); cube.name = "cube" + i; vplObjects.addDisplayObject3D(cube); scene.addChild(cube); var _jBox:JBox = new JBox(cube, true, _scale, _scale, _scale); var _targetX:Number = Math.random() * 400 - 200; var _targetY:Number = Math.random() * 500 + 50; var _targetZ:Number = Math.random() * 400 - 200; var _position:JNumber3D = new JNumber3D(_targetX, _targetY, _targetZ); _jBox.MoveTo(_position, JMatrix3D.rotationX(Math.random() * 360 * 180 / Math.PI)); // start position _jBox.setMass(_scale); jBoxArray.push(_jBox); PhysicsSystem.getInstance().AddBody(_jBox); } } private function init2DObject():void { var stats:StatsView = new StatsView(renderer); addChild(stats); stage.addEventListener(MouseEvent.CLICK, onStageClick); } private function onStageClick(e:MouseEvent = null):void { createLotOfBox(); } private function onEnterFrame(e:Event ):void { physicsStep(); camera.x = Math.sin(newTime / 1000) * 200; singleRender(); } private function physicsStep():void { newTime = getTimer(); deltaTime = ((newTime - currentTime) / 1000) * physicsSpeed; currentTime = newTime; PhysicsSystem.getInstance().Integrate(deltaTime); // phyisc step 5 is the speed } } }
留言
不過它應該還有很大的開發空間!!這個雛型應該是繼Box2D後很令人興奮的事了~~