2 Replies Latest reply on May 11, 2010 8:22 PM by zeroskillz

    whats the physics behind Flash ActionScript

    rhgtv

      I'm a student at Academy of Art University and for my final project for  my Physics for Artist class I am illustrating the physics behind a ball  bounching using Fl. I found a useful tutorialkirupa.com but my teacher says, "There's a small problem with the equations:  the speed term should be negative, if downward is positive." and,wants  to know "Also, can you tell me how the program knows when to switch  between the two equations?" I looked at the AS, which I really am not  that familiar with, and cant really get how it is using the laws of  physics to make the motion realistic. Does anyone know of a source that  explains how the developers at flash have integrated the laws of physics  in the code syntax? Or even some other tutorials of sample files that  create this motion in flash and explain/ illustrate the laws of physics  behind it? Any help would be much appreciated-this stuff is hard to  find!,/p>

        • 1. Re: whats the physics behind Flash ActionScript
          kglad Adobe Community Professional & MVP

          there is no physics in any of the flash classes except the tween easing classes.  you can check the easing equations by opening the flash easing class files or check robert penners easing equations.  the latter is recommended so you don't damage a flash class file and you can find explanations.

          • 2. Re: whats the physics behind Flash ActionScript
            zeroskillz Level 3

            There's a number of libraries available for implementing physics in Flash--Box2D is one... I'd recommend the Advanced ActionScript 3 Animation book from Friends of ED for a number of great examples with detailed explanations. Below is a example based on some code sample from that book. Play with the bounce, gravity and friction variables.

             

            As an example of some simple physics math in flash, put this in your actions palette for frame 1. Compile and enjoy. Clicking on the movie will 'reset' the balls position and velocity.

             

             

             

            import flash.display.Sprite;
            import flash.display.StageAlign;
            import flash.display.StageScaleMode;
            import flash.events.Event;
            
            
            // draw a pretty ball
            var rad:Number = 30;
            var gradientBoxMatrix:Matrix = new Matrix();
            gradientBoxMatrix.createGradientBox(rad*3,rad*3,0,-(rad*2),-(rad*2));
            var ball:Sprite = new Sprite();
            ball.graphics.beginGradientFill(GradientType.RADIAL, [0xffffff, 0xff0000, 0x4b0101, 0x000000], [1,1,1,1], [0,50,225,255], gradientBoxMatrix);
            ball.graphics.drawCircle(0,0,rad);
            ball.graphics.endFill();
            ball.x = stage.stageWidth / 2;
            ball.y = stage.stageHeight / 2;
            addChild(ball);
            
            
            var vx:Number; // velocity X
            var vy:Number; // velocity Y
            var bounce:Number = -0.7;
            var gravity:Number = .5;
            var friction:Number = .9;
                      
            init();
            
            function init():void
            {
                 stage.scaleMode = StageScaleMode.NO_SCALE;
                 stage.align=StageAlign.TOP_LEFT;
                 vx = Math.random() * 10 - 5;
                 vy = -10;
                 stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
                 stage.addEventListener(MouseEvent.CLICK, resetBall);
            }
            
            function enterFrameHandler(event:Event):void
            {
                 if(Math.abs(vx)<.01 && Math.abs(vy)<.3){stage.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);}
            
                 vy += gravity;
                 ball.x += vx;
                 ball.y += vy;
                 var left:Number = 0;
                 var right:Number = stage.stageWidth;
                 var top:Number = 0;
                 var bottom:Number = stage.stageHeight;
                 
                 if(ball.x + ball.width*0.5 > right)
                 {
                      ball.x = right - ball.width*0.5;
                      vx *= bounce;
                 }
                 else if(ball.x - ball.width*0.5 < left)
                 {
                      ball.x = left + ball.width*0.5;
                      vx *= bounce;
                 }
                 if(ball.y + ball.width*0.5 > bottom)
                 {
                      ball.y = bottom - ball.width*0.5;
                      vy *= bounce;
                      vx *= friction;
                 }
                 else if(ball.y - ball.width*0.5 < top)
                 {
                      ball.y = top + ball.width*0.5;
                      vy *= bounce;
                 }
            }
            
            
            function resetBall(event:MouseEvent):void
            {
                 if(!stage.hasEventListener(Event.ENTER_FRAME))
                 {
                      stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
                 }
                 ball.x = mouseX;
                 ball.y = mouseY;
                 vx = Math.random() * 10 - 5;
                 vy = -10;
                 bounce = -0.7;
                 gravity = .5;
            }
            

             

             

             

            HTH,

            -Ted