Pendulum from a moving cart with some specs (needed for a game, not homework)

SevenSeas

New member
Joined
Aug 14, 2014
Messages
15
Hello!

I'm making a javascript game about swinging in threads. I've modeled the swinging as a simple pendulum motion, and it works fine for swinging from a static pivot point.

Since HTML5 in browsers does not guarantee that animation updates come with even intervals, I did not find a way to just use variables for angular acceleration, angular velocity and angle, and then adding the calculated acceleration to the velocity each animation frame and then adding the angular velocity to the angle.

Instead I used an equation for the angle as a direct function of time. That equation was:


theta(t) = theta0 * cos((2PI * t) / T)

where theta(t) is the angle (with vertical down as 0),
theta0 is the maximal angle (also called amplitude),
t is the time,
and T is the time for a complete pendulum cycle, calculated as
T = 2PI * sqrt(L / g),
where L is the length of the pendulum,
and g is the acceleration of gravity.

I found this equation here: http://en.wikipedia.org/wiki/Simple_Pendulum .


But now I'm on to tackling a bigger problem: I want the player to be able to swing from moving objects. I understand that the thing I want is close to a common physics lab for higher education. I found this page: http://www.myphysicslab.com/pendulum_cart.html . At the end it gives these equations, to be put into the Runge-Kutta numerical method:


v' = [mR * omega² * sin(theta) + mg * sin(theta) * cos(theta) - kx - dv + (b/R) * omega * cos(theta)] / (M + m * sin²(theta))


omega' = [-mR * omega² * sin(theta) * cos(theta) - (m+M)g * sin(theta) + kx * cos(theta) + dv * cos(theta) - (1 + M/m) * (b/R) * omega] / (R * (M + m * sin²(theta)))


where v is the velocity of the cart,
m is the mass of the pendulum,
R is the length of the pendulum,
omega is the angular velocity of the pendulum,
theta is the angle of the pendulum (with vertical down as 0),
g is the acceleration of gravity,
k is "spring stiffness" (I don't think I need this at all)
d is a friction constant for the cart,
b is a friction constant for the pendulum,
and M is the mass of the cart.


But when I look at the the wikipedia page about Runge-Kutta (http://en.wikipedia.org/wiki/Runge_Kutta) it is not clear to me which of the Runge-Kutta algorithms to use.

Also, I believe the only type of moving cart I need has it's x position as a function of time

x(t) = someConstant * sin(t) .

i.e. a type of back-of-forth-going as if it was going around in a circle with constant speed, but only using the x axis of it, if you understand what I mean... So I don't want the "spring" motion of the cart as on that page. There is also this pdf which describes the problem for a general moving cart instead of a cart on a spring, but I can't understand it completely: http://www.phys.lsu.edu/faculty/gonzalez/Teaching/Phys7221/PendulumWithMovingSupport.pdf

Would it be easier to solve this problem knowing my restriction to a certain motion equation for the cart?

Also, there is this: the moment when the system starts, t might have other values than 0 (i.e., the cart may be "in the middle" of its back-and-forth-motion), and in addition, also the angle theta may be other than 0 at that moment, i.e. there is an initial angle to the pendulum.

There also may or may not be some initial velocity for the bob point in x and y directions, to be translated into an initial angular velocity. But this is optional.

I would like to solve this in an as simplified way as possible -- it doesn't have to be completely physically correct, but only give a motion that is smooth and looks OK on the screen, for a light-hearted game.

Thank you in advance!
 
Last edited:
OK, according to this page: http://blogs.mathworks.com/seth/2008/10/09/challenge-metronome-and-cart-equations-of-motion/

you can arrive at this equation for a simplification of the situation:

theta'' = -(g / L) * sin(theta) + (x'' / L) * cos(theta) (i)


As I have defined x(t) = k * sin(t) above, we have
x'(t) = k * cos(t) (Correct? My calculus is rusty...)
and
x''(t) = -k * sin(t). (ii)

Putting (ii) into (i) gives:

theta'' = -(g / L) * sin(theta) - ((k * sin(t) * cos(theta)) / L)

If I understand correctly this is what you'd call a differential equation. I've never solved differential equations. So as I understand it, I have two options:

1) To solve this differential equation, to get theta as a function of t (I suppose there would be a range of answers for different "initial values" of theta or something, is this correct?)

2) To use the definition above of the angular acceleration theta'' directly in my simulation, adding it to an angular velocity which I then add to the angle each frame. The only thing is that I then would need a way for correcting for the fact that delta t might be different between each update, and I don't think a simple multiplication with delta t would be the correct way to do it? Also, I've read a warning somewhere that simulation in this way might not conserve energy in the system, so there is a risk of the pendulum going faster and faster due to rounding errors?

I also forgot to mention that I would need to simulate "damping" or friction in some rough way, to make the pendulum go slower and slower if no new force acts on it.
 
Last edited:
OK, I actually implemented something that kind of works for me, already.

I went the simulation route, as the differential equation apparently was a hard one.

So consider this closed, although if you have something interesting to say about this, I guess I will check out this thread now and then for your answers.
 
Top