A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Acceleration, the right way?

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    9

    Acceleration, the right way?

    Hey everyone,

    I've been trying to create a new class of vehicle and simulate a somewhat realistic acceleration/velocity relationship and behavior but with no success.
    At least, I can't figure out how to do what I want to do.

    Which is this:
    I have a vehicle with a maxVelocity and an initialAcceleration properties, and what I want to find out is how to express the vehicle's acceleration with the two above mentioned variables so that the vehicle stops accelerating when it reaches its maxVelocity (meaning that acceleration reaches 0).

    I believe there are some serious maths going on behind this seemingly simple thing, at least too serious for me, and I tried to remember what I learned years ago about arithmetic progressions and stuff like that but to no result other than a headache.

    Does someone know how to do it, if it's actually possible. And if not, how you guys do to simulate an acceleration that looks like a car's gear.

    Thanks!

  2. #2
    Mod cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    Here is a simple script how you can control the acceleration, no big math.
    PHP Code:
    import flash.utils.Timer;
    import flash.events.TimerEvent;

    var 
    counter:int 1;
    var 
    accValue:Number;
    var 
    myTimer:Timer = new Timer(1);
    myTimer.start();
    myTimer.addEventListenerTimerEvent.TIMERcHandler);
    function 
    cHandler(event:TimerEvent):void
    {
        
    counter++;
        
    accValue counter 0.01;
        if(
    accValue >= 2)
        {
            
    accValue 2;
        }
        
    mc.+=0.1 accValue;

    - The right of the People to create Flash movies shall not be infringed. -
    | www.Flashscript.biz | Flashscript Biz Classes/Components |

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks for your answer cancerinform.
    Actually, this is not what I need and I figured out how to just do that myself last night!
    It was actually very very easy... I don't know why I kept thinking it was difficult so I kept looking into complicated formulas and stuff and got stuck. The answer is much simpler than that. Take a look:


    Actionscript Code:
    velocity += acceleration; //as usual, add the acceleration to the velocity

    /*
    * set the acceleration to be half
    * (or third or whatever you need)
    * of what is basically the remaining
    * acceleration needed to reach maxVelocity
    */

    acceleration += (maxVelocity - velocity) / 2;

    /*
    * zero out the acceleration when
    * low enough since technically it'll never reach it
    */


    if (acceleration < .1) acceleration = 0;

    And that's it...! Sometimes, you need a good night of sleep to reboot your brain and basically stop looking for difficulty where there is no need for it

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center