Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm making a game which you can see here, if you are on Windows or Linux:

If you click and hold your mouse on a bird, you can see I'm just swinging the bird back and forth in pendulum motion. I would like to, instead, implement a more realistic motion, where the movement of your mouse affects the swinging of the bird like a pendulum with a moving pivot.

I found a document on this topic but the equations rely on knowing the pivot's acceleration (X'' and Y''), which I do not; I am only repeatedly translating the bird graphic to the current mouse position.

I have the bird's angle (-180 to 180 degrees), angular velocity and acceleration. I will need to alter these three variables each time the mouse is moved, so I will also have the last (x,y) and the new mouse (x,y).

Is this enough to make a good simulation of a pendulum with moving pivot?

share|cite|improve this question
Possibly better suited to stackoverflow. Physics/math questions are okay there, as long as they're programming-related, which this certainly is. – MatrixFrog Aug 22 '10 at 22:08
@MatrixFrog Thanks for the suggestion, I just created an identical question in SO. – Ricket Aug 22 '10 at 22:23
up vote 1 down vote accepted

The actual solution may not be what you want, since it will come with perks like the birds swinging almost a full $-180^\circ$ to $180^\circ$ everytime when pulled hard and then immediately held still. Also you will have to solve a differential equation in real time.

May I offer an alternative instead which will make it look real while being easy to implement.

Since you have a simple oscillation, I assume you simply vary the angle as $\alpha(t)=r\cos(2\pi f t)+3\pi/2$ for some range $r$ and frequency $f$ in your code.

To add a touch of reality, you can vary the 'down' angle based on the horizontal velocity of the mouse. If the velocity is $v$, then calculate $\beta(v)=3\pi/2-\tan^{-1}(Av)$. This will give some pseudo-direction for the adjusted direction of pull.

Also decrease the range of the oscillation as the velocity increases: $r(v)=1/(1+(Bv)^2)$.

Finally use the following function to get the angle: $\alpha(t)=r(v)\cos(2\pi f t)+\beta(v)$.

You will have to choose fractions $A$ and $B$ till you hit upon the right motion that pleases you. $A=B=0$ will give your original motion back. Larger $A$ will make the change in angle more sensitive to speed, and larger $B$ will dampen the oscillations more at a given velocity.

share|cite|improve this answer

The mouse velocity is [(current mouse position) - (previous mouse position)]/timestep (presumably you're using a constant timestep of 10ms or something?) and then the acceleration is similarly [(current mouse velocity) - (previous mouse velocity)]/timestep. (At least, approximately.)

Once you have the acceleration, I guess you can use the formulas you already have.

share|cite|improve this answer

In case you really need to simulate accurate physics, you can use the solution given below.

alt diagram

In the picture above, vector $A = g-a$ where $g$ is acceleration due to gravity, and $a$ is the acceleration of mouse which you need to compute in real time. You will have to translate $g=9.8m/s^2$ into $pixel/s^2$ and store as a constant. For $A$, you need to calculate $A_x$ and $A_y$ based on the last two frames.

We need to find $\alpha(t)$.

We have $\beta=tan^{-1}\frac{A_x}{A_y}$ and $A_1=|A|\cos(\alpha-\beta)$. Angular acceleration will be $-A_1/h$ ($h$ will vary from bird to bird).

You will need to solve the differential equation $\frac{d^2\alpha}{dt^2}=A_1/h$ in real time to make the bird swing realistically.

The obvious way to solve the differential equation numerically is given below -

Store angular velocity $\omega=\frac{d}{dt}\alpha$. At each frame of the animation, assuming $\Delta t$ time has passed since last frame, do

  1. $\omega\leftarrow\omega+\frac{A_1}{h}\Delta t$
  2. $\alpha\leftarrow\alpha+\omega\Delta t$
share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.