4 Replies Latest reply on Feb 23, 2010 1:51 PM by Francisc


    Francisc Level 3



      How can I implement INERTIA when rotating something on mouse roll over for example?

      To take into account the mouse "speed" would be nice



        • 1. Re: Inertia
          archemedia Level 4

          If you want to implement physics in your app, then time, speed etc. are important.

          You could start by adding a timer to your app. The time handler (which can be set to a few milliseconds) could add x or y coördinates to an array. The diffrence of the values in this array enable you to calculate speed and/or direction of the mouse! If you use this information into basic physical formulas, you can simulate gravity, inertia and whatever you want. It's a bit of mathematics, but it's great fun!



          Does this help?



          1 person found this helpful
          • 2. Re: Inertia
            Francisc Level 3

            Hm, yeah, that much I figured with the exception of how speed is calcualated, I was thinking checking speed between 2 points within 100 milliseconds was enough.


            My problem is don't know how I can take that and implement gravity or inertia for example.

            • 3. Re: Inertia
              archemedia Level 4

              For gravity, you don't need speed, you just need acceleration which is 9,81 in falling movements.

              Since s = v * t (distance = speed * time) and a = v / t (acceleration = speed / time) you can put s = a * t * t

              In falling movements, acceleration = gravity so s = g * t * t.


              About measuring speed:

              Now, suppose you move your mouse rapidly from left to right and you measure the x position every 10 milliseconds, you could create an array like [50, 250, 600, 400, 120, -50]. If you add the absolute differences between each position, you'd get 200 + 350 + 200 + 280 + 170 = 1200 over 50 milliseconds. That's a speed of 24 pixels/second. You can use this value in a physical formula.


              Like I said, inertia is a factor in a more complex formula, but the principle stays the same. Your mouse movement is a dynamic factor.


              O'Reilly has a very good book on this: Physics for game developers:



              I can't write the code for you because it would take me too much time, as much as I would like to though


              Hope this helps



              • 4. Re: Inertia
                Francisc Level 3

                Very clever, thanks!


                Because of Flex, I am always looking for built-in classes instead of doing what I would do in PHP (for example) which is write my own, even if PHP has nothing to do with physics.


                To get an accurate reading for speed I should do the distance of the slope between (x1,y1) and (x2,y2) I think though.


                Thank you!