Reputation: 1127
I use the following code to simulate the animation of a pendulum, however, it ignores air resistance and never stops.
while (true) {
angleAccel = -9.81 / length * Math.sin(angle);
angleVelocity += angleAccel * dt;
angle += angleVelocity * dt;
repaint();
try { Thread.sleep(15); } catch (InterruptedException ex) {}
}
Now I want to take into account air resistance so that the pendulum would slow down and finally stops in the vertical direction. I've tried to adapt the code to my need however none of them worked.
Upvotes: 1
Views: 661
Reputation: 4110
The most sensible way to do this, in my opinion, is to build air resistance into the differential equation that governs the system. Air resistance could be approximated by a force proportional to the velocity but in the opposite direction, for example. Then either the equation could be solved in closed form (e.g., damped harmonic oscillator e^{-kt} sin (wt)
) or could be numerically integrated (a more versatile solution in the long run).
Upvotes: 1