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!
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: