Simulating pendulum movement

Started by
13 comments, last by Buckeye 14 years, 3 months ago
The triangle on the left shows the decomposition of the mg vector into a tangent vector and a vector in the direction of the rope. The triangle on the right shows the decomposition of the vector in the direction of the rope.

Your calculation of the angle theta and ball.posVec are both incorrect. theta = atan2( ball.x, ball.y ). Don't add " -restAngle." theta is the angle of the rope with the y-axis as shown in the picture.

I think you're incorrectly adding the acceleration to both the velocity vector and the position vector.

I'm pretty sure it should be:
pos += vel*time + 1/2 a*time*time

then
vel += a*time

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Advertisement
Quote:Original post by Buckeye

Your calculation of the angle theta and ball.posVec are both incorrect. theta = atan2( ball.x, ball.y ). Don't add " -restAngle." theta is the angle of the rope with the y-axis as shown in the picture.


I was doing this just like the spring-mass, but instead of the change in
position, its change in angle. So I thought that the rest angle is 270 degrees,
so if there is a movement then there is a change in angle, which is
currentAngle - restAngle? I am not sure why I need to remove the restAngle, but
i'll give it a try.

Quote:
I think you're incorrectly adding the acceleration to both the velocity vector and the position vector.
I'm pretty sure it should be:
pos += vel*time + 1/2 a*time*time

then
vel += a*time


Isn't that what I have? Maybe I should use a different approach? And by the way, thanks for your responses. You seem to be the only helpful one around here, usually other people are more friendly and tend to help out as well. Thank you.
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github
You can include the restAngle if you want, but it's just a constant offset, so why complicate the calculation? As long as the angle you use for the calc is the angle between the rope position and the Y-axis, it's all the same.

You have:
vel_1 = vel_0 + a*time
pos += vel_1*time + 1/2a*time*time

which is
pos += (vel_0 +a*time)*time + 1/2a*time*time

so you've accounted for the acceleration's effect on the position twice in one time interval. I'm suggesting you reverse the order of those two equations. I probably wasn't clear on that.

That is:
you have-
vel += a*time
pos += vel*time + 1/2 a*time*time

I think-
pos += vel*time + 1/2 a*time*time
vel += a*time

is correct.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Thanks for the explanation. I tried it but it won't budge. You know I am a little
disappointed. I think we did the calculations correctly, we implemented it
correctly, but the physics simulation says otherwise. If you wan't and have time, I can zip it to you so you can play around with it. Although I will admit, that its not pretty.
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github
I didn't properly take into account the length of the rope.

The derivation of the equation is based on using cylindrical coordinates as cartesian coordinates are very difficult to work with in this case.

This is the update function. "fSecs" is your deltaTime or time-per-frame. theta0 is the angle of the rope with the y-axis at time zero.

You can, of course, make gravity and/or mass and/or theta0 whatever you like. Start out with float time = 0.

float theta;
float theta0 = 1.5708f; // pi/2 - horizontal
float grav = 32.0f;
float mass = 1.0f;
float ropeLen = 10.0f;
float cosFac = sqrt(grav/mass)*time;
theta = theta0 * cos(cosFac);
pendLoc.x = ropeLen*sin(theta);
pendLoc.y = -ropeLen*cos(theta);
time += fSecs;

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

This topic is closed to new replies.

Advertisement