- Joined
- Jun 26, 2009
- Messages
- 1,045
- Reaction score
- 0
- Points
- 36
Hi everyone,
I made a small program to simulate a pendulum last night and it seems to work OK apart from one thing: the pendulum gains momentum as it swings. First off, here's the code I'm using:
These calculations are run every time a timer ticks (which is every 10 milliseconds or 0.01 seconds) and the pos[0] and pos[1] variables are the x and y coordinates of the pendulum bob relative to the origin (the point where the string is held). 'g' is 9.81 and 'l' is 1.5.
For some reason, the pendulum swings noticeably higher on each swing and I can't figure out why. If it helps, all of the variables are defined as type 'double'.
Thankyou for your help!
I made a small program to simulate a pendulum last night and it seems to work OK apart from one thing: the pendulum gains momentum as it swings. First off, here's the code I'm using:
Code:
accAng = -g*sin(posAng)/l;
velAng += accAng*0.01;
posAng += ((velAng*0.01)-(0.5*accAng*0.0001));
pos[0] = l*sin(posAng);
pos[1] = l*cos(posAng);
These calculations are run every time a timer ticks (which is every 10 milliseconds or 0.01 seconds) and the pos[0] and pos[1] variables are the x and y coordinates of the pendulum bob relative to the origin (the point where the string is held). 'g' is 9.81 and 'l' is 1.5.
For some reason, the pendulum swings noticeably higher on each swing and I can't figure out why. If it helps, all of the variables are defined as type 'double'.
Thankyou for your help!