Creating terrain with jiglib and alternativa3d 7

If you aren’t familiar with JigLibFlash it’s a great open source actionscript 3D Physics Engine and is setup to integrate with different 3d engines in flash. Whilst alternativa3d isn’t officially supported yet the guys over at glasses factory have created some classes that connect both the libraries together. To see what possibilities this offers for alternativa3d users check out the video on their blog here. Whilst I’m still new to jiglibflash i thought i would put together how to create terrain based upon the papervision implementation examples i found with the download, before we begin you will need to download the jiglibflash from here and the glasses factory classes from here or here. jiglib What this example does is take the heightmap2.jpg file from the jiblibflash examples and creates molds the terrain based on this image from a plane. Here is the code i created.

package
{
        import alternativa.engine3d.containers.ConflictContainer;
        import alternativa.engine3d.controllers.SimpleObjectController;
        import alternativa.engine3d.core.Camera3D;
        import alternativa.engine3d.core.View;
        import alternativa.engine3d.materials.FillMaterial;
        import flash.display.Bitmap;
        import flash.display.BitmapData;
        import flash.display.Sprite;
        import flash.display.StageAlign;
        import flash.display.StageScaleMode;
        import flash.events.Event;
        import flash.geom.Vector3D;
        import jiglib.geometry.*;
        import jiglib.physics.*;
        import net.glassesfactory.alt3dkit.jiglib.*;
        [SWF(width="800", height="600", backgroundColor="#000000", frameRate="60")]
        public class test extends Sprite
        {
    		[Embed(source="hightmap2.jpg")]
    		public var TERRIAN_MAP:Class;
    		public var container:ConflictContainer;
                    public var view:View;
                    public var camera:Camera3D;
                    public var cameraController:SimpleObjectController;
    		private var terrain:GFAlt3dTerrain;
    		public function test():void {
    			stage.scaleMode = StageScaleMode.NO_SCALE;
                            stage.align = StageAlign.TOP_LEFT;
                            container = new ConflictContainer();
                            view = new View( stage.stageWidth, stage.stageHeight );
                            addChild( view );
                            camera = new Camera3D();
                            camera.view = view;
                            container.addChild( camera );
                            camera.x = 0;
                            camera.y = 0;
                            camera.z = 0;
    			cameraController = new SimpleObjectController( stage, camera, 500 );
                            cameraController.lookAtXYZ( 0, 0, 0 );
    			//create terrain
    			var terrainBMD:Bitmap = new TERRIAN_MAP;
    			terrain = new GFAlt3dTerrain( terrainBMD.bitmapData, new FillMaterial( 0xFF0000, 1, 1, 0xffffff ), 10000, 10000, 800, 30, 30);
    			terrain.rotationX = -90*Math.PI/180;
    			container.addChild(terrain);
    			addEventListener( Event.ENTER_FRAME, enter );
    		}
    		protected function enter( e:Event ):void
                    {
    			cameraController.update();
    			camera.render();
                    }
    	}
}

You can download the example files here EDIT: Also note that you need to edit the GFAlt3dTerrain class change

this.calculateNormals();

to

this.calculateFacesNormals();
this.calculateVerticesNormals();

Downloads

Comments

  • avatar-fire
    # fire
    hi, you don’t take the jiglib , i try it , but fail! don’t have collision, there is code :

    • avatar-davidejones
      # davidejones

      try using something like this:

      private var terrain:JTerrain; private var physics:GFAlternativa3DPhysics; physics = new GFAlternativa3DPhysics(container, 8); var terrainBMD:Bitmap = new TERRIAN_MAP; terrain = physics.createTerrain(terrainBMD.bitmapData,new FillMaterial( 0xFF0000, 1, 1, 0xffffff ), 10000, 10000, 800, 30, 30); GFAlt3dTerrain(terrain.terrainMesh).rotationX = -90*Math.PI/180;

  • avatar-fire
    # fire

    hi, still not, jbox and jterrain does not collide , jbox across directly the jterrain , please have a look my code :

    package { import alternativa.engine3d.containers.ConflictContainer; import alternativa.engine3d.controllers.SimpleObjectController; import alternativa.engine3d.core.Camera3D; import alternativa.engine3d.core.View; import alternativa.engine3d.materials.FillMaterial; import alternativa.engine3d.objects.Mesh; import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.Event; import flash.geom.Vector3D; import jiglib.geometry.JTerrain; import jiglib.physics.RigidBody; import net.glassesfactory.alt3dkit.jiglib.GFAlt3dTerrain; import net.glassesfactory.alt3dkit.jiglib.GFAlternativa3DPhysics; [SWF(width="800”, height="600”, backgroundColor=”#000000”, frameRate="60”)] public class test2 extends Sprite { [Embed(source="hightmap2.jpg”)] public var TERRIAN_MAP:Class; public var container:ConflictContainer; public var view:View; public var camera:Camera3D; public var cameraController:SimpleObjectController; private var jterrain:JTerrain; private var physics:GFAlternativa3DPhysics; public function test2():void { stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT; container = new ConflictContainer(); view = new View( stage.stageWidth, stage.stageHeight ); addChild( view ); camera = new Camera3D(); camera.view = view; container.addChild( camera ); camera.z = 200; camera.y = -400; camera.lookAt(0,0,0); cameraController = new SimpleObjectController( stage, camera, 500 ); cameraController.lookAtXYZ( 0, 0, 0 ); physics = new GFAlternativa3DPhysics(container,8); physics.engine.setGravity(JNumber3D.getScaleVector(Vector3D.Z_AXIS, -10)); var terrainBMD:Bitmap = new TERRIAN_MAP; jterrain = physics.createTerrain(terrainBMD.bitmapData, new FillMaterial( 0xFF0000, 1, 1, 0xffffff ), 10000, 10000, 800, 30, 30); GFAlt3dTerrain(jterrain.terrainMesh).rotationX = -90*Math.PI/180; var box1:RigidBody = physics.createBox(150,150,150,new FillMaterial(Math.random() * 0xFFFF00, 1, 1)); box1.moveTo(new Vector3D(100, 0, 400)); box1.friction = 0.2; box1.restitution = 2; var box2:RigidBody = physics.createBox(150,150,150,new FillMaterial(Math.random() * 0xFFFF00, 1, 1)); box2.moveTo(new Vector3D(0, 0, 500)); addEventListener( Event.ENTER_FRAME, enter ); } protected function enter( e:Event ):void { cameraController.update(); camera.render(); physics.step(); } } }

  • avatar-fmaxx
    # fmaxx
    Hi The problem a little deeper, than “FAlt3dTerrain(terrain.terrainMesh).rotationX = -90*Math.PI/180;” JTerrain.setOrientation not working correctly.
  • avatar-davidejones
    # davidejones

    yea i’ve been playing with jiglib again recently and tried setting the gravity and orientation to what i want but no collisions occur so i think its something to do with jiglib library.

    They have a newer version for fp11 which may have fixed these problems i haven’t tried it yet.

    http://code.google.com/p/jiglibflash/source/browse/#svn%2Ftrunk%2Ffp11%2Fsrc

Comments are currently closed