Good morning!

I am using Papervision3D and wow physics engine to do a simple 
dragging-object-around "hello world". The drag part works only when you drag 
sloowly; when you drag the ball a little bit too fast, it will fall behind the 
cursor and the "handleMouseUp" function doesn't get called. What I assume is 
that there is some bug in my code, either in the way I deal with the mouse 
part, or how I handle the  sync of physics and rendering.

Also I am confused by the coordinates in WOW as compared to Papervision3d. So 
the mouse isn't really "3D";(

You can see the example at:
http://maohao.com/lab/wowDrag.swf
code:
http://maohao.com/lab/WOWTest.as

Here is the code:

package
{
/*      /***
        * This is the Document class for Flash.
        *  BUG: When the sphere is dragged, a lot of times (when dragging is a 
little bit fastser) it gets left behind ...
        *  TODO: How to move the sphere in 3D space?
        
         * 
         * /*/
        import flash.events.Event;
        
        import fr.seraf.wow.core.WOWEngine;
        import fr.seraf.wow.core.data.WVector;
        import fr.seraf.wow.primitive.WOWPlane;
        import fr.seraf.wow.primitive.WParticle;
        import fr.seraf.wow.primitive.WSphere;
        
        import org.papervision3d.core.geom.TriangleMesh3D;
        import org.papervision3d.core.utils.Mouse3D;
        import org.papervision3d.core.utils.virtualmouse.VirtualMouse;
        import org.papervision3d.events.InteractiveScene3DEvent;
        import org.papervision3d.materials.ColorMaterial;
        import org.papervision3d.materials.MovieAssetMaterial;
        import org.papervision3d.materials.WireframeMaterial;
        import org.papervision3d.objects.primitives.Plane;
        import org.papervision3d.objects.primitives.Sphere;
        import org.papervision3d.view.BasicView;
        
        public class MainView3 extends BasicView
        {
                protected var mcAssetMaterial:MovieAssetMaterial;
                //protected var paperPlane:PaperPlane;
                protected var pvdPlane:Plane;
                protected var pvdGround:Plane;
                protected var pvdSphere:Sphere;
                private var wowSphere:WSphere;
                protected var wowEngine:WOWEngine;
                protected var wfMaterial:WireframeMaterial;
                private var objArray:Array = new Array();
                private var vMouse:VirtualMouse;
                private var mouse3D:Mouse3D;
                private var _dragging:Boolean = false;
                public function MainView3()
                {
                        //w, h, scaleToStage, interactive
                        super(0, 0, true, true);
                        //this.camera.zoom = 1;
                        this.camera.y = 520;
                        this.camera.z = 330;
                        this.camera.x = 10;
                        //opaqueBackground = 0;
                        //setupStage();
                        setupWOW();
                        createGround();
                        createStuff();          
                        //Start rendering
                        startRendering();
                        
                        //interactivity
                        vMouse = 
this.viewport.interactiveSceneManager.virtualMouse;
                        mouse3D = this.viewport.interactiveSceneManager.mouse3D;
                        Mouse3D.enabled = true;
                }
                
                protected function setupWOW():void
                {
                        wowEngine = new WOWEngine();
                        //Default is STANDARD, which is the most accurate.
                        wowEngine.collisionResponseMode = wowEngine.SELECTIVE;
                        wowEngine.addMasslessForce(new WVector(0, 3, 0));
                        wowEngine.damping = .95;
                }
                
                private function createGround():void
                {
                        var ground:WOWPlane = new WOWPlane(0,0,0);
                        ground.elasticity = 0.6;
                        ground.friction = 0.3;
                        wowEngine.addParticle(ground);
                        
                        wfMaterial = new WireframeMaterial(0, 100, .1);
                        wfMaterial.doubleSided = true;
                        wfMaterial.interactive = true;
                        pvdGround = new Plane(wfMaterial, 1000, 1000, 5, 5);
                        pvdGround.rotationX = -90;
                        scene.addChild(pvdGround);
                }
                protected function createStuff():void
                {       
                        var wowPlane:WOWPlane = new WOWPlane(0, -50, 0, 0, 0, 
0);
                        wowPlane.elasticity= 0.5;
                        wowPlane.friction = 0.02;
                        wowPlane.mass = .1;
                        wowEngine.addParticle(wowPlane);
                        
                        mcAssetMaterial = new MovieAssetMaterial("Symbol3");
                        mcAssetMaterial.interactive = true;
                        pvdPlane = new Plane(mcAssetMaterial);
                        pvdPlane.x = -wowPlane.px;
                        pvdPlane.y = -wowPlane.py;
                        pvdPlane.z = -wowPlane.pz;
                        pvdPlane.z+= 1000;
                        scene.addChild(pvdPlane);                       
                        this.objArray.push({physics:wowPlane, render:pvdPlane});
                        
                        var radius:Number = 50;//int(Math.random()*20)+1;
                        wowSphere = new 
WSphere(int(-Math.random()*50)-25,int(-Math.random()*200),int(-Math.random()*50)-25,radius,
 false, .1);
                        wowSphere.elasticity = .5;
                        wowSphere.friction = .02;
                        wowSphere.mass = 1;
                        wowEngine.addParticle(wowSphere);
                        
                        //var material:ColorMaterial = new 
ColorMaterial(0xFF00FF, 1, true);
                        pvdSphere = new Sphere(mcAssetMaterial, radius, 5, 3);
                        pvdSphere.x = -wowSphere.px;
                        pvdSphere.y = -wowSphere.py;
                        pvdSphere.z = -wowSphere.pz;
                        scene.addChild(pvdSphere);
                        //interactivity
                        
pvdSphere.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS, 
handleMouseDown);
                        
                        this.objArray.push({physics:wowSphere, 
render:pvdSphere});
                        
                }
                private function startDragging(e:InteractiveScene3DEvent):void
                {
                        var pvdSphere = e.target;
                        
pvdSphere.addEventListener(InteractiveScene3DEvent.OBJECT_MOVE, 
handleMouseMove);
                
pvdSphere.addEventListener(InteractiveScene3DEvent.OBJECT_RELEASE, 
handleMouseUp);
                        
pvdSphere.addEventListener(InteractiveScene3DEvent.OBJECT_RELEASE_OUTSIDE, 
handleMouseUp);
                        
                }
                private function stopDragging():void
                {
                        
pvdSphere.removeEventListener(InteractiveScene3DEvent.OBJECT_MOVE, 
handleMouseMove);
                        
pvdSphere.removeEventListener(InteractiveScene3DEvent.OBJECT_RELEASE, 
handleMouseUp);
                        
pvdSphere.removeEventListener(InteractiveScene3DEvent.OBJECT_RELEASE_OUTSIDE, 
handleMouseUp);
                }
                public function handleMouseDown(e:InteractiveScene3DEvent):void
                {
                        _dragging = true;
                        startDragging(e);
                        trace("drag____");
                }
                public function handleMouseUp(e:InteractiveScene3DEvent)
                {
                        _dragging = false;
                        stopDragging();
                        trace("stop dragging!");
                }
                public function handleMouseMove(e:InteractiveScene3DEvent)
                {
                        var sphere = e.target;
                        var physics = wowSphere; 
                        physics.px = mouse3D.x;
                        physics.py = -mouse3D.y;        
                        physics.pz = mouse3D.z; 
                }
                override protected function onRenderTick(event:Event=null):void
                {
                        wowEngine.step();
                        
                        var count:int = this.objArray.length;
                        while(count--)
                        {
                                var physics:WParticle = 
this.objArray[count].physics as WParticle;
                                var render:TriangleMesh3D = 
this.objArray[count].render as TriangleMesh3D;
                                
                                render.x = physics.px;
                                render.y = -physics.py;

                        }
                        super.onRenderTick(event);
                }
                
        }
}


      

_______________________________________________
osflash mailing list
[email protected]
http://osflash.org/mailman/listinfo/osflash_osflash.org

Reply via email to