Archive

Archive for February, 2009

Box2Dwrapper – Box2DFlashAS3 made simple!

February 28th, 2009

Hi guys:

So, about a week ago, a friend of mine asked me if I could give him a quick explanation as to how to use/setup Box2DFlashAS3. Honestly, I didn’t have the time as I was swamped with work, so I directed him over to Emanuele’s site which is chock full on Box2DFlashAS3 tutorials. After going through his posts for a couple days, my buddy said that he was able to figure it out but that the syntax was ridiculous! I don’t blame him either; the syntax is very unorthodox for a developer that has only develops in AS3.

That got me to thinking and I decided to create a wrapper class to try and make it simpler to use. Now, keep in mind that I really never dug into this particular physics engine. I quickly saw that a simple wrapper class was not going to be enough, but I started anyway and ended up with the Box2Dwrapper library.

The Box2Dwrapper library is modeled after Papervision3D’s BasicView and is extremely easy to use. Please keep in mind that the library is still under development and probably will be for some time since I will be busy with JigLibFlash refactoring and other work, never the less, I will update it often so keep tuned.

Enough gibberish, on to the code! Remember, it’s still in its infant stage and undocumented, but I’m still gonna let you guys check it out.

This is how simple it is to use:

package
{
    import com.box2dwrapper.object.Box;
    import com.box2dwrapper.object.Circle;
    import com.box2dwrapper.view.Box2DView;

    public class Box2Dwrapper_Test extends Box2DView
    {
        public function Box2Dwrapper_Test()
        {
            super(2000, 2000, 0, 9.8, true);
            showDebug = true;
            allowDragging = true;
            initialize();
        }
       
        private function initialize():void
        {
            createFloor();
            createFace();
            startPhysicsRender();
        }
       
        private function createFloor():void
        {
            var floor:Box = new Box();
            floor.position(stage.stageWidth * 0.5, stage.stageHeight);
            floor.scale(stage.stageWidth * 0.5, 10);
            floor.type = "static";
            addBody(floor);
        }
       
        private function createFace():void
        {
            var face:Circle = new Circle();
            face.radius = 20;
            face.position(stage.stageWidth * 0.5, 25);
            face.type = "dynamic";
            face.restitution = 0.5;
            addBody(face);
        }
       
    }
}

IN THE CONSTRUCTOR

- The Box2DView class accepts 4 parameters passed in via the “super” method. They are:

  • world width
  • world height
  • horizontal force
  • vertical force
  • allow sleep


- showDebug (internal) allows you to see the debug drawn physics objects.

- allowDragging (internal) makes the objects automatically draggable.

IN THE INITIALIZE METHOD

- startPhysicsRender() (internal) starts the render ticker.

IN THE createFloor and createFace METHODS

Here you can see how easy it is to create a box/circle with physics properties. Definitely a syntax we can relate to. Here are their properties and methods:

  • radius (circle only) sets the radius
  • density sets the mass
  • restitution sets the elasticity
  • friction sets the friction
  • type can be either “static” or “dynamic”
  • skin sets a display object from the library as a material, for example circle.skin = new Face();



  • scale(width, height) sets the width and height
  • position(x, y) sets the position


There are other properties and methods that are in the process of integration and that will be available in a future release. In the meantime, you guys can take the library as is, add, edit, remove stuff, dissect it as you wish. Just don’t claim it as yours ;)

You can grab the code from the Google repository here :
http://box2dwrapper.googlecode.com/svn/trunk/

Lab

Exploring a Papervision3D class – Sound3D

February 4th, 2009

So, this weekend I was working on a project; I use Flex Builder to code and as I was casting a class, I noticed “Sound3D” in the code completion and immediately got intrigued. I have heard of that class being buried deep in the Papervision packages before, but really didn’t pay much attention to it, till now.

Before we jump into anything, check out the example below. It uses the FirstPersonCamera3D class so you can move around and get a sense of what the Sound3D class does.

How cool is that?! I recently developed a game in which that would have worked out pretty well. No complaints though :) One thing about the Sound3D class in the Papervision3D packages is that it does not have the ability to load sounds, it only accepts a sound object as a parameter in the constructor, so, I decided to to add the functionality in there myself by creating an EnhancedSound3D class which extends the Sound3D, and here it is:

package
{
    import flash.events.Event;
    import flash.events.ProgressEvent;
    import flash.media.Sound;
    import flash.media.SoundLoaderContext;
    import flash.net.URLRequest;
   
    import org.papervision3d.objects.special.Sound3D;

    public class EnhancedSound3D extends Sound3D
    {
       
        private var soundObjectParams:Object = {};
       
        public function EnhancedSound3D(soundObj:Sound = null)
        {
            super(soundObj);
        }
       
        public function load(soundPath:String, startTime:Number = 0, loops:int = 0, soundTransform:SoundTransform = null):void{
            var soundRequest:URLRequest = new URLRequest(soundPath);
            sound = new Sound();
            sound.load(soundRequest, new SoundLoaderContext());
            sound.addEventListener(Event.COMPLETE, handleSoundLoaded);
            sound.addEventListener(ProgressEvent.PROGRESS, handleLoadProgress);
            soundObjectParams = {startTime:startTime, loops:loops, soundTransform:soundTransform}
        }
       
        private function handleSoundLoaded(e:Event):void
        {
            sound.removeEventListener(ProgressEvent.PROGRESS, handleLoadProgress);
            sound.removeEventListener(Event.COMPLETE, handleSoundLoaded);
            play(soundObjectParams.startTime, soundObjectParams.loops, soundObjectParams.soundTransform);
            dispatchEvent(e.clone());
        }
       
        private function handleLoadProgress(e:ProgressEvent):void
        {
            dispatchEvent(e.clone());
        }
       
    }
}

The Sound3D class extends DisplayObject3D, so it inherits all its properties such as x, y and z so you can basically place the sound anywhere you like in 3D space. A few added properties are maxSoundDistance and soundDistance. “maxSoundDistance” determines the maximum distance the sound can travel and “soundDistance” is used to control the volume. It puts out values ranging from -1 to 1 where 0 to -1 represent the sound being behind the camera.

You can basically attach your sound to anything, a car, a plane an enemy walking behind in your first person shooter game [wink, wink].

Below is the code I used for the example you just saw. Remember that you’ll need to grab the FirstPersonCamera3D class if you don’t have it yet.

package
{
    import flash.events.Event;
    import flash.events.ProgressEvent;
    import flash.text.TextField;
   
    import org.papervision3d.materials.ColorMaterial;
    import org.papervision3d.materials.WireframeMaterial;
    import org.papervision3d.materials.special.CompositeMaterial;
    import org.papervision3d.materials.utils.MaterialsList;
    import org.papervision3d.objects.primitives.Cube;
    import org.papervision3d.objects.primitives.Plane;
    import org.papervision3d.view.BasicView;

        import FirstPersonCamera3D;
        import EnhancedSound3D;

    public class CameraTest extends BasicView
    {
       
        private var firstPersonCamera:FirstPersonCamera3D;
        private var cubes:Array = [];
        private var sounds:Array = [];
       
        private var SIZE:Number;
        private var DISTANCE:Number;
       
        public function CameraTest()
        {
            super(640, 480, true, false);
            SIZE = 30000;
            DISTANCE = (SIZE * 0.5) - 250;
            setupCamera();
            createObjects3D();
            startRendering()
        }
       
        private function setupCamera():void
        {
            firstPersonCamera = new FirstPersonCamera3D();
            firstPersonCamera.initialize(viewport, 0.8, 0.2);
            firstPersonCamera.mode = "manual";
            firstPersonCamera.y = 750;
            firstPersonCamera.moveBounds = {left:-DISTANCE + 750, right:DISTANCE - 750, front:DISTANCE - 750, back:-DISTANCE + 750};
            firstPersonCamera.mapKeys(FirstPersonCamera3D.WASD_AND_ARROWS);
        }
       
        private function createObjects3D():void
        {
            var positions:Array = [{x:DISTANCE, y:750, z:DISTANCE},
                                   {x:-DISTANCE, y:750, z:DISTANCE},
                                   {x:DISTANCE, y:750, z:-DISTANCE},
                                   {x:-DISTANCE, y:750, z:-DISTANCE}];
           
            var cubeMat:WireframeMaterial = new WireframeMaterial(0xFF0000);
            var colorMat:ColorMaterial = new ColorMaterial(0x00FF00);
            var compMat:CompositeMaterial = new CompositeMaterial();
            compMat.addMaterial(cubeMat);
            compMat.addMaterial(colorMat);
           
            var materialsList:MaterialsList = new MaterialsList({ all:compMat });
           
            for(var a:Number = 0; a<4; a++){
                var cube:Cube = new Cube(materialsList);
                cube.x = positions[a].x;
                cube.y = positions[a].y;
                cube.z = positions[a].z;
                cube.lookAt(firstPersonCamera);
                cubes[a] = cube;
                               
                var sound3D:EnhancedSound3D = new EnhancedSound3D();
                sound3D.load(a+".mp3");
                sound3D.x = positions[a].x;
                sound3D.y = positions[a].y;
                sound3D.z = positions[a].z;
                sound3D.maxSoundDistance = SIZE * 0.75;
                sound3D.addEventListener(ProgressEvent.PROGRESS, handleSoundLoadProgress);
                sounds[a] = {sound:sound3D, tf:TextField(this["soundLoad"+a])};        
               
                scene.addChild(cube);
                scene.addChild(sound3D);
            }
           
            var floor:Plane = new Plane(new WireframeMaterial(0xFFFFFF), SIZE, SIZE, SIZE*0.001, SIZE*0.001);
            floor.rotationX = 90;
            scene.addChild(floor);
        }
       
        private function focusCubesOnCamera():void
        {
            for(var a:Number = 0; a<cubes.length; a++){
                cubes[a].lookAt(firstPersonCamera)
            }
        }
       
        private function handleSoundLoadProgress(e:ProgressEvent):void
        {
            for(var a:Number = 0; a<sounds.length; a++){
                if(e.target == sounds[a].sound){
                    sounds[a].tf.text = "Sound"+a+" -- "+Number(Math.round((e.bytesLoaded/e.bytesTotal)*100))+"% loaded."
                }
            }
        }
       
        override protected function onRenderTick(event:Event=null):void
        {
            firstPersonCamera.look(0.5);
            focusCubesOnCamera();
           
            renderer.renderScene(scene, firstPersonCamera, viewport);
        }
       
    }
}

Lab, as3, papervision3d