JigLib Flash + Papervision3D Demo

Posted on January 13, 2009
Filed Under Actionscript 3.0, experiments, Opensource | 2 Comments

I had to play immediately with the JigLibFlash library. Like always there is a learning curve, but the syntax is at least better written/understandable than Box2DFlash, which doesn’t mean that I don’t like Box2DFlash, cause I do!

In this example you use the mouse to move the magenta ball, when you keep your mouse at the center of the stage you don’t apply any speed to the magenta ball. All the balls can drop of the floor on the front side. Have FUN!

  1. /**
  2.  * @author janwillemwisman
  3.  *
  4.  * positioning 3d papervision objects, only via the jBox.moveTo,
  5.  * otherwise object will be centered when added to the PhysicsSystem.
  6.  *
  7.  * when you dont use physicsStep, jBoxes will not be place correctly. nothing will happen basically
  8.  */
  9. package
  10. {
  11.  // flash
  12.  import flash.utils.getTimer;
  13.  import flash.events.Event;
  14.  import flash.display.Sprite;
  15.  
  16.  // wismanas3lib
  17.  import nl.wisman.utils.MathExt;
  18.  
  19.  // papervision
  20.  import org.papervision3d.cameras.Camera3D;
  21.  import org.papervision3d.lights.PointLight3D;
  22.  import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
  23.  import org.papervision3d.materials.utils.MaterialsList;
  24.  import org.papervision3d.objects.DisplayObject3D;
  25.  import org.papervision3d.objects.primitives.Cube;
  26.  import org.papervision3d.objects.primitives.Sphere;
  27.  import org.papervision3d.render.BasicRenderEngine;
  28.  import org.papervision3d.scenes.Scene3D;
  29.  import org.papervision3d.view.Viewport3D;
  30.  import org.papervision3d.view.layer.ViewportLayer;
  31.  import org.papervision3d.view.layer.util.ViewportLayerSortMode;
  32.  
  33.  // jiglibflash
  34.  import jiglib.physics.PhysicsSystem;
  35.  import jiglib.math.JMatrix3D;
  36.  import jiglib.math.JNumber3D;
  37.  import jiglib.geometry.JSphere;
  38.  import jiglib.geometry.JBox;
  39.  
  40.  public class JigLibDemo extends Sprite
  41.  {
  42.   //
  43.   // private  properties
  44.   //
  45.   private var _scene:Scene3D;
  46.   private var _viewport:Viewport3D;
  47.   private var _renderer:BasicRenderEngine;
  48.   private var _sceneLight:PointLight3D;
  49.   private var _camera:Camera3D;
  50.   private var _cameraTarget:DisplayObject3D;
  51.   private var _newTime:int;
  52.   private var _deltaTime:Number;
  53.   private var _currentTime:Number;
  54.   private var _physicsSpeed:Number = 10;
  55.   private var _speedBall:int = 10;
  56.   private var _maxBall:Number = 10;
  57.   private var _jBalls:Array;
  58.  
  59.   //////////////////////////////////////////////////////////////////////////////////////
  60.   //
  61.   // Constructor
  62.   //
  63.   //////////////////////////////////////////////////////////////////////////////////////
  64.  
  65.   // ———————————————————————————–
  66.   public function JigLibDemo()
  67.   {
  68.    addEventListener(Event.ADDED_TO_STAGE, initialize);
  69.   }
  70.  
  71.   //////////////////////////////////////////////////////////////////////////////////////
  72.   //
  73.   // Public methods
  74.   //
  75.   //////////////////////////////////////////////////////////////////////////////////////
  76.  
  77.   // ———————————————————————————–
  78.   // none
  79.  
  80.   //////////////////////////////////////////////////////////////////////////////////////
  81.   //
  82.   // Private methods
  83.   //
  84.   //////////////////////////////////////////////////////////////////////////////////////
  85.  
  86.   // ———————————————————————————–
  87.   private function initialize(event:Event):void
  88.   {
  89.    removeEventListener(Event.ADDED_TO_STAGE, initialize);
  90.  
  91.    // setup
  92.    _currentTime = getTimer();
  93.  
  94.    initPapervision3D();
  95.    initArea();
  96.    init3DObjects();
  97.    setupEventListeners();
  98.   }
  99.  
  100.   // ———————————————————————————–
  101.   private function initPapervision3D():void
  102.   {
  103.    _scene = new Scene3D();
  104.    _viewport = new Viewport3D(0,0,true);
  105.    addChild(_viewport);
  106.  
  107.    // set _viewport sortmode to index
  108.    _viewport.containerSprite.sortMode = ViewportLayerSortMode.INDEX_SORT;
  109.  
  110.    _renderer = new BasicRenderEngine();
  111.  
  112.    // light settings
  113.    _sceneLight = new PointLight3D(true, true);
  114.    _sceneLight.x = 0;
  115.    _sceneLight.y = 400;
  116.    _sceneLight.z = -300;
  117.  
  118.    // _camera settings
  119.    _camera = new Camera3D();
  120.    _camera.x = 0;
  121.    _camera.y = 450;
  122.    _camera.z = -1000;
  123.    _camera.zoom = 5;
  124.    _camera.focus = 100;
  125.  
  126.    // _camera target, helper object to make the _camera a target type
  127.    _cameraTarget=new DisplayObject3D();
  128.    _cameraTarget.x=0;
  129.    _cameraTarget.y=250;
  130.    _cameraTarget.z=0;
  131.    // set the _camera target
  132.    _camera.target = _cameraTarget;
  133.   }
  134.  
  135.   // ———————————————————————————–
  136.   private function initArea():void
  137.   {
  138.    // general material
  139.    var materialGround:MaterialsList = new MaterialsList();
  140.    materialGround.addMaterial(new FlatShadeMaterial(_sceneLight, 0×999999), "all");
  141.  
  142.    // create back wall
  143.    var backWall3D:Cube = new Cube(materialGround, 1000, 50, 1000, 2, 2, 2);
  144.    _scene.addChild(backWall3D);
  145.  
  146.    var vplBackWall3D:ViewportLayer = _viewport.getChildLayer(backWall3D);
  147.    vplBackWall3D.layerIndex = 1;
  148.  
  149.    var jBackWall:JBox = new JBox(backWall3D, false, 1000, 50, 1000);
  150.    jBackWall.MoveTo( new JNumber3D(0, 0, 500), JMatrix3D.IDENTITY);
  151.    PhysicsSystem.getInstance().AddBody(jBackWall);
  152.  
  153.    // create left wall
  154.    var leftWall3D:Cube = new Cube(materialGround, 50, 1000, 1000, 2, 2, 2);
  155.    _scene.addChild(leftWall3D);
  156.  
  157.    var vplLeftWall3D:ViewportLayer = _viewport.getChildLayer(leftWall3D);
  158.    vplLeftWall3D.layerIndex = 2;
  159.  
  160.    var jLeftWall:JBox = new JBox(leftWall3D, false, 50, 1000, 1000);
  161.    jLeftWall.MoveTo( new JNumber3D(500, 0, 0), JMatrix3D.IDENTITY);
  162.    PhysicsSystem.getInstance().AddBody(jLeftWall);
  163.  
  164.    // create right wall
  165.    var rightWall3D:Cube = new Cube(materialGround, 50, 1000, 1000, 2, 2, 2);
  166.    _scene.addChild(rightWall3D);
  167.  
  168.    var vplRightWall3D:ViewportLayer = _viewport.getChildLayer(rightWall3D);
  169.    vplRightWall3D.layerIndex = 3;
  170.  
  171.    var jRightWall:JBox = new JBox(rightWall3D, false, 50, 1000, 1000);
  172.    jRightWall.MoveTo( new JNumber3D(-500, 0, 0), JMatrix3D.IDENTITY);
  173.    PhysicsSystem.getInstance().AddBody(jRightWall);
  174.  
  175.    // Ground pv3d
  176.    var pv3dGround:Cube = new Cube(materialGround,1000,1000,50,2,2,2);
  177.    _scene.addChild(pv3dGround);
  178.  
  179.    // Viewport layer ground
  180.    var vplGround:ViewportLayer = _viewport.getChildLayer(pv3dGround);
  181.    vplGround.layerIndex = 4;
  182.  
  183.    // Ground physics
  184.    var jGround:JBox = new JBox(pv3dGround,false,1000,1000,50);
  185.    PhysicsSystem.getInstance().AddBody(jGround);
  186.   }
  187.  
  188.   // ———————————————————————————–
  189.   private function init3DObjects():void
  190.   {
  191.    // VIEWPORT LAYER OBJECTS
  192.    // balls boxes chain sphere will be placed on it
  193.    var vplObjects:ViewportLayer = new ViewportLayer(_viewport,null);
  194.    vplObjects.layerIndex = 4;
  195.    vplObjects.sortMode = ViewportLayerSortMode.Z_SORT; // sort mode for this viewportlayer based on z
  196.    _viewport.containerSprite.addLayer(vplObjects);
  197.  
  198.    _jBalls = new Array();
  199.    for (var i:int = 0; i < _maxBall; i++)
  200.    {
  201.     // create sphere
  202.     var colorSphere:uint = (i == 0)?0xe20074:0xeeee00;
  203.     var pv3dBall:Sphere = new Sphere( new FlatShadeMaterial(_sceneLight, colorSphere) ,50,20,16);
  204.  
  205.     // add sphere to viewportlayer for objects
  206.     vplObjects.addDisplayObject3D(pv3dBall);
  207.  
  208.     // add to _scene
  209.     _scene.addChild(pv3dBall);
  210.  
  211.     // add papervision sphere to jSphere
  212.     _jBalls[i] = new JSphere(pv3dBall,true,50);
  213.     _jBalls[i].MoveTo(new JNumber3D(MathExt.random(-100, 250), MathExt.random(400, 650), MathExt.random(100, 350)), JMatrix3D.IDENTITY); // start position x,y,z
  214.  
  215.     // add to physicsSystem
  216.     PhysicsSystem.getInstance().AddBody(_jBalls[i]);
  217.    }
  218.   }
  219.  
  220.   // ———————————————————————————–
  221.   private function setupEventListeners():void
  222.   {
  223.    addEventListener(Event.ENTER_FRAME, update);
  224.   }
  225.  
  226.   // ———————————————————————————–
  227.   private function update(event:Event):void
  228.   {
  229.    moveBall();
  230.    physicsStep();
  231.    checkFallenObjects();
  232.    _renderer.renderScene( _scene, _camera, _viewport );
  233.   }
  234.  
  235.   // Calculate the deltaTime
  236.   // ————————————————————————
  237.   private function physicsStep():void
  238.   {
  239.             _newTime = getTimer();
  240.             _deltaTime = ((_newTime – _currentTime) / 1000) * _physicsSpeed;
  241.             _currentTime = _newTime;
  242.           PhysicsSystem.getInstance().Integrate(_deltaTime); // phyisc step 5 is the speed
  243.   }
  244.  
  245.   // ———————————————————————————–
  246.   private function checkFallenObjects():void
  247.   {
  248.    for (var i:String in _jBalls)
  249.    {
  250.     if (_jBalls[i].CurrentState.Position.y < -50)
  251.     {
  252.      _jBalls[i].MoveTo(new JNumber3D(MathExt.random(-100, 250), MathExt.random(400, 650), MathExt.random(100, 350)), JMatrix3D.IDENTITY); // reposition balls
  253.     }
  254.    }
  255.   }
  256.  
  257.   // ———————————————————————————–
  258.   private function moveBall():void
  259.   {
  260.    // area left
  261.    if (mouseX < 350)
  262.    {
  263.     _jBalls[0].AddWorldForce(new JNumber3D(-_speedBall,0,0), _jBalls[0].CurrentState.Position);
  264.    }
  265.    // area right
  266.    if (mouseX > 450)
  267.    {
  268.     _jBalls[0].AddWorldForce(new JNumber3D(_speedBall,0,0), _jBalls[0].CurrentState.Position);
  269.    }
  270.    // top area
  271.    if (mouseY < 250)
  272.    {
  273.     _jBalls[0].AddWorldForce(new JNumber3D(0,0,_speedBall), _jBalls[0].CurrentState.Position);
  274.    }
  275.    // bottom area
  276.    if (mouseY > 350)
  277.    {
  278.     _jBalls[0].AddWorldForce(new JNumber3D(0,0,-_speedBall), _jBalls[0].CurrentState.Position);
  279.    }
  280.   }
  281.  }
  282. }

Comments

2 Responses to “JigLib Flash + Papervision3D Demo”

  1. Amazed on March 18th, 2009 10:40 am

    Wow. You even left some of the original coder’s comments in there, not to mention their code.

    Classy.

  2. Michal Sajban on November 26th, 2009 4:05 pm

    Hi, great stuff! This is my new game based on papervision + jiglib. Its helicopter simulation http://www.littlebigplay.com/game/548

-->