|
-
Mar 16th, 2007, 03:08 PM
#1
Thread Starter
Addicted Member
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
-
Mar 16th, 2007, 03:44 PM
#2
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:
private int acceleration = 1;
int velocity;
int x;
private void Form1_Load(object sender, EventArgs e)
{
cmdStartStop.Text = "Start";
}
private void timer1_Tick(object sender, EventArgs e)
{
box.Left += x;
velocity += acceleration;
x += velocity;
lblPos.Text = "Position: " + x;
lblVel.Text = "Velocity: " + velocity;
lblAcc.Text = "Acceleration: " + acceleration;
}
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;
}
}
private void cmdStartStop_Click(object sender, EventArgs e)
{
switch (cmdStartStop.Text)
{
case "Start":
{
cmdStartStop.Text = "Stop";
timer2.Enabled = false;
timer1.Enabled = true;
break;
}
case "Stop":
{
cmdStartStop.Text = "Start";
timer1.Enabled = false;
timer2.Enabled = true;
break;
}
}
}
-
Mar 16th, 2007, 03:51 PM
#3
Thread Starter
Addicted Member
Re: Acceleration/Deacceleration
that worked thanks just weird that the loop made it not work tough
-
Mar 16th, 2007, 03:54 PM
#4
Thread Starter
Addicted Member
Re: Acceleration/Deacceleration
altough its not a very smooth deacceleration . kinda stops realy fast - cant seem to find a good value to deaccelerate with
-
Mar 16th, 2007, 04:09 PM
#5
Thread Starter
Addicted Member
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
-
Mar 17th, 2007, 02:02 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|