Results 1 to 6 of 6

Thread: Acceleration/Deacceleration

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    162

    Acceleration/Deacceleration

    Im making a small "physics" app that accelerates a picture box, and then deaccelerate it until it stops. I have the acceleration working, but the deacceleration is giving me problems:

    Code:
    // Accelerate
    private void timer1_Tick(object sender, EventArgs e)
            {
                cmdStartStop.Text = "Stop Simulation";
                box.Left += x;
                velocity += acceleration;
                x += velocity;
                lblPos.Text = "Position: " + x;
                lblVel.Text = "Velocity: " + velocity;
                lblAcc.Text = "Acceleration: " + acceleration;
    
            }
    Code:
    private void timer2_Tick(object sender, EventArgs e)
    // Deaccelerate
            {
                while (velocity > 0)
                {
                    box.Left += x;
                    velocity -= acceleration;
                    x += velocity;
                    lblPos.Text = "Position: " + x;
                    lblVel.Text = "Velocity: " + velocity;
                    lblAcc.Text = "Acceleration: " + acceleration;
    
                }
            }
    I start and stop the simulation with a button.
    timer2 is disabled at load
    button activates timer1
    button then deactivate timer1 and activate timer2

  2. #2
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Acceleration/Deacceleration

    Don't loop inside a function that itself is looping.

    Instead of while loop, you should check if velocity is greater than 0 or not.

    Code:
    private void timer2_Tick(object sender, EventArgs e)
    
    if (velocity > 0)
    {
       box.Left += x;
       velocity -= acceleration;
       x += velocity;
       lblPos.Text = "Position: " + x;
       lblVel.Text = "Velocity: " + velocity;
       lblAcc.Text = "Acceleration: " + acceleration;
    }
    This should work for you, I have both timers disabled initially.
    C# Code:
    1. private int acceleration = 1;
    2.         int velocity;
    3.         int x;
    4.         private void Form1_Load(object sender, EventArgs e)
    5.         {
    6.             cmdStartStop.Text = "Start";
    7.         }
    8.  
    9.         private void timer1_Tick(object sender, EventArgs e)
    10.         {
    11.             box.Left += x;
    12.             velocity += acceleration;
    13.             x += velocity;
    14.             lblPos.Text = "Position: " + x;
    15.             lblVel.Text = "Velocity: " + velocity;
    16.             lblAcc.Text = "Acceleration: " + acceleration;
    17.         }
    18.  
    19.         private void timer2_Tick(object sender, EventArgs e)
    20.         {
    21.             if(velocity > 0)
    22.             {
    23.                 box.Left += x;
    24.                 velocity -= acceleration;
    25.                 x += velocity;
    26.                 lblPos.Text = "Position: " + x;
    27.                 lblVel.Text = "Velocity: " + velocity;
    28.                 lblAcc.Text = "Acceleration: " + acceleration;
    29.  
    30.             }
    31.         }
    32.  
    33.         private void cmdStartStop_Click(object sender, EventArgs e)
    34.         {
    35.             switch (cmdStartStop.Text)
    36.             {
    37.                 case "Start":
    38.                     {
    39.                         cmdStartStop.Text = "Stop";
    40.                         timer2.Enabled = false;
    41.                         timer1.Enabled = true;
    42.                         break;
    43.                     }
    44.                 case "Stop":
    45.                     {
    46.                         cmdStartStop.Text = "Start";
    47.                         timer1.Enabled = false;
    48.                         timer2.Enabled = true;
    49.                         break;
    50.                     }
    51.             }
    52.         }
    Show Appreciation. Rate Posts.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    162

    Re: Acceleration/Deacceleration

    that worked thanks just weird that the loop made it not work tough

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    162

    Re: Acceleration/Deacceleration

    altough its not a very smooth deacceleration . kinda stops realy fast - cant seem to find a good value to deaccelerate with

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    162

    Re: Acceleration/Deacceleration

    Code:
        if(velocity > 0)
                {
                    
                    box.Left += x;
                    velocity += -acceleration;
                    x += velocity;
                    lblPos.Text = "Position: " + x;
                    lblVel.Text = "Velocity: " + velocity + " pixels/frame";
                    lblAcc.Text = "Acceleration: " + acceleration + " pixels/frame^2";
    
                }
    is only executed once, even if velocity > 0

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Acceleration/Deacceleration

    Of course it is. That's what an if(){} block does: evaluates the condition and executes the block if the result of the condition is true.

    for(;;;){}, while(){}, and do{}while() are loops. if(){} is not.

    What Harsh is saying is that you have the if(){} block inside a timer event handler. The timer event is fired regularly until the timer is disabled, hence there's no need to loop within it. The alternative method would be to loop without a timer, but then you'd lose control over its speed (unless you use some more advanced trick).

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