Archive

Posts Tagged ‘physics’

Crates with physics: Papervision3D + WOWEngine

December 5th, 2008
Comments Off

Needed to quickly prototype crates falling from the sky with some physics applied for this cool project on working on.

I used a Factory Class to create the crates for me, very efficient stuff! What the class does is create both the papervision and wowengine objects and returns them in an object under the values “crate” and “fisixCrate”.

package
{
    import fr.seraf.wow.core.WOWEngine;
    import fr.seraf.wow.primitive.WSphere;
   
    import org.papervision3d.materials.MovieAssetMaterial;
    import org.papervision3d.materials.utils.MaterialsList;
    import org.papervision3d.objects.primitives.Cube;

    public class CrateFactory
    {
       
        public static function buildCrate($material:String, $width:Number = 500, $height:Number = 500):Object
        {
            // display object 3d
            var crateMat:MovieAssetMaterial = new MovieAssetMaterial($material);
            var materialsList:MaterialsList = new MaterialsList({ all:crateMat });
            var crate:Cube = new Cube(materialsList);
           
            // fisix object
            var wowCube:WSphere = new WSphere(crate.x, -3500, crate.z, 250, false);
            wowCube.mass = 10000;
            wowCube.elasticity = 0;
            wowCube.friction = 0;
           
            return {crate:crate, fisixCrate:wowCube};
        }
       
    }
}

In my document class, I have a function which uses the CrateFactory to build the crates for me at random places. It adds the papervision object to the scene and the wow particle to the wow engine:

private function buildCrate():void
{
    var objCrate:Object = CrateFactory.buildCrate("CrateMaterial");
    scene.addChild(objCrate.crate);
    wow.addParticle(objCrate.fisixCrate);
    arrCrates.push(objCrate);
    objCrate.fisixCrate.px = MathUtil.random(1500, -1500);
    objCrate.fisixCrate.pz = MathUtil.random(500, -300);
}

All the returned objects are placed in an “arrCrates” array. This array is used in the ENTER_FRAME listener to render out both the 3d and physics objects accordingly. My main class extends BasicView so I override the “onRenderTick” event handler:

override protected function onRenderTick(event:Event=null):void
{
    wow.step();
   
    for(var a:Number = 0; a<arrCrates.length; a++){
        var crate3D:Cube = arrCrates[a].crate;
        var fisixCrate:WSphere = arrCrates[a].fisixCrate;
        crate3D.x = fisixCrate.px;
        crate3D.y = -fisixCrate.py;
        crate3D.z = fisixCrate.pz;
    }
   
    var rotY: Number = (mouseY-(stage.stageHeight/2))/(stage.height/2)*(300);
    var rotX: Number = (mouseX-(stage.stageWidth/2))/(stage.width/2)*(-300);
    camera.x= camera.x+(rotX-camera.x)/5;
    camera.y= camera.y+(rotY-camera.y)/5;              

    renderer.renderScene(scene, _camera, viewport);
}

Experiment, Lab , , ,