# Variation on Pendulum Function

## Main Question or Discussion Point

I basically need a function that will animate a chain necklace. Think of the necklace as having three square links on each side with a central medallion. I need this to swing like an actual necklace.

My biggest problem with this is the fact that I don't have a standard for gravity. Gravity has to be a function of x and y acceleration data received from an accelerometer.

I basically need it to swing using the two top links as anchors. I will also need it to be able to fall straight down if the object is held upside down.

I've been struggling with this for a while. Any help would be immensely appreciated.

Thanks,
Brandon

Related Classical Physics News on Phys.org
I basically need a function that will animate a chain necklace.
What does "function that will animate" mean? Is this a computer programming question? If so, I can give you a piece of code or something.

Think of the necklace as having three square links on each side with a central medallion. I need this to swing like an actual necklace.
Not sure exactly what you're describing. Can you draw a diagram? Including the point from which it's swinging?

My biggest problem with this is the fact that I don't have a standard for gravity. Gravity has to be a function of x and y acceleration data received from an accelerometer.
Are you saying that the ambient gravitational acceleration in the problem changes as a function of time?

The pendulum problem (which does not sound like what you are describing) does not have a general closed-form solution, IIRC, except in the limit where the angle subtended by the swing is very small. The problem you're describing sounds more like you want a general 2D Newtonian physics simulator. I mean if you're trying to model multiple objects interacting in an arbitrary arrangement with arbitrary forces applied. There are some good physics simulator programs out there, but they're a lot of code; they aren't trivial. You could probably find an open-source simulator somewhere and just grab its source code.

If you're really interested in this, you can probably put together a really simplified physics simulator yourself, by just numerically integrating the Newtonian equations of motion in some sloppy way and then adding in ad-hoc fixes to correct for any weird rounding error behavior you get. This won't be perfect, and still sounds like a fair amount of work.

I'm submitting a very simple diagram. The top 3 rows are "links" the single box on the bottom is the "medallion". I basically need this to swing as if it's being worn. so the top two "links" would be the anchors.

Now imagine the box is rotated on the x and y axis, kind of like if it laid down. I would need the pieces to swing accordingly in that direction, but still holding to the anchors.

I don't have the option of using a large simulator, as this is a very small project. It doesn't have to be perfect either. I just need some kind of formula to get these things to move correctly.

I'm including my attempt and calculating the x and y changes. The pieces are moved based on a fraction of this value, to slow the animation a little. This works to some extent. The pieces swing and fall in a swinging motion of sorts. I just can't get everything to anchor correctly, and the "medallion" floats to the left border of the screen and stays there.

Code:
//extract the acceleration components
float xx = -[acceleration x];
float yy = [acceleration y];

// Has the direction changed?
float accelDirX = SIGN(xvelocity) * -1.0f;
float newDirX = SIGN(xx);
float accelDirY = SIGN(yvelocity) * -1.0f;
float newDirY = SIGN(yy);

// Accelerate.
if (accelDirX == newDirX)
xaccel = (abs(xaccel) + 0.85f) * SIGN(xaccel);
if(accelDirY == newDirY)
yaccel = (abs(yaccel) + 0.85f) * SIGN(yaccel);

// calculate Theta's
L1ThetaAccel = (yaccel/h1) * sin(L1Theta);
L1ThetaVel += L1ThetaAccel;
L1Theta += L1ThetaVel;

L2ThetaAccel = (yaccel/h2) * sin(L2Theta);
L2ThetaVel += L2ThetaAccel;
L2Theta += L2ThetaVel;

L3ThetaAccel = (yaccel/h3) * sin(L3Theta);
L3ThetaVel += L3ThetaAccel;
L3Theta += L3ThetaVel;

BThetaAccel = (yaccel/hB) * sin(BTheta);
BThetaVel += BThetaAccel;
BTheta += BThetaVel;

xvelocity = -xaccel * xx;
yvelocity = -yaccel * yy;
Any Help would be appreciated. Thanks again.

#### Attachments

• 9.4 KB Views: 265
Another note. I'm also struggling with getting the "links" to gravitate toward the middle when they're moving back down. I need the "medallion to be the center point of gravitation. If that makes any sense.

Sorry, I can't completely follow your code. I'm not sure I know what all those variables represent. I think I can guess some of them, but others I'm kind of scratching my head on. I also don't think you should need to be taking the sign of anything as part of the code (this is really a vector operation). I can write you some quick and dirty code, but I'd really like to know exactly what you're working on and stuff if I'm going to contribute my leet skillz to the project. ;)

OK, so do you want these to be modeled as boxes anchored to each other at a pivot point? I mean, can the objects rotate like rigid bodies, or are you satisfied just modeling each of them as particles with no particular orientation? If that's not how they're connected, then how? Invisible strings modeled as ideal springs, maybe? Either that or rigid rods are about the simplest connectors I can think of.

Yeah, the code's kind of thrown together. Not really sure what I'm doing there. This is kind of a side project for me, so I haven't had a whole lot of time to work on it.

You can basically think of each link as having an invisible string connecting it to the next link. Like a necklace.

As far as how they move. Each box is an image. So i'm really just moving the image container. I would love for them to rotate like the would in real life.

Basically the idea here is for the box to wear the necklace. If the box rotates the necklace moves accordingly.

Sorry, I'm never very good at explaining myself. And quick and dirty code works for me. I have the app written for the most part, I'm just struggling with the physics.

Thanks

Yeah, the code's kind of thrown together. Not really sure what I'm doing there. This is kind of a side project for me, so I haven't had a whole lot of time to work on it.
Not a problem. Can you tell me what the project is? If I'm going to contribute I would like to see it, if possible. I'd also like to get a mention somewhere, even if this is one of those kinds of random projects probably no one will ever see (just in case).

You can basically think of each link as having an invisible string connecting it to the next link. Like a necklace.
OK, I guess I can try and model that as some kind of weird modified spring, I'll see what I can do.

As far as how they move. Each box is an image. So i'm really just moving the image container. I would love for them to rotate like the would in real life.
Of course you would. So I'll have to throw in some rigid body stuff: torques, angular momenta, angular velocities, moments of inertia, etc. in addition to the linear force/velocity/momentum/mass stuff.

Basically the idea here is for the box to wear the necklace. If the box rotates the necklace moves accordingly.
OK, so "the box" in this context just means the background, basically, right?

Sorry, I'm never very good at explaining myself.
Yeah, you and me both. But everyone gets better at communicating certain kinds of ideas as they accumulate experience in a given field.

It's a novelty iphone app. And I will absolutely give you some credit if I ever get it done and in the app store.

Thanks,
Brandon

It's a novelty iphone app. And I will absolutely give you some credit if I ever get it done and in the app store.
Ha! Awesome. OK, let me try to throw something together. This is C code, right?

Objective C. Kind of different. If I can get the formula right, I should be able to translate it to Objective C without much problem.

OK, before I get too into this, it occurs to me that I should spell out for you what I'm going to do, in case you'd rather take a crack at it yourself.

I'm just going to use the naive and imperfect technique where you write all Newton's laws in differential form, and then do this little trick: if, for example, we know that:

v = dx/dt

then we can just take the initial value of x, assume some VERY small timestep dt, and rearrange that equation to get:

dx = dt * v

or to put it another way:

x_new = x_initial + dt * v.

So you just update all your quantities (position vectors using velocity vectors, velocity vectors using acceleration vectors, and acceleration vectors using forces calculated based on the tethers and the gravitation and stuff) at every timestep using all of Newton's laws of motion. In order for this corny technique to not suck too much, the timestep will have to be outrageously small. I don't know if it's going to work well enough for you, but with some friction in there to dampen things, I would bet it won't be too awful.

I don't know if that was clear, but that's what I'm a-gonna do.

Guys - have you thought about just modeling the shape of the necklace as a catenary curve? This is the curve followed by an ideal rope hanging from two points, which your necklace is not, but it might be a good enough approximation. You'd need to calculate how to vary the parameters of the catenary as you subject the necklace to whatever perturbations you're applying (presumably in response to the phone's accelerometer?).

I'm not really sure what perturbations are. I'm much more knowledgeable on the programming side than the physics side. Mostly what I'm looking for here is direction. If anyone has some time on their hands and thinks it would be a fun project to figure out the formula exactly, that would be awesome. But I'm not expecting that from anyone.

But, like I said Xelec, I will absolutely give credit.

Thanks,
Brandon

Oh, cool, the catenary would work. You could even swing it like a pendulum. It wouldn't look perfect, I mean it wouldn't sort of bounce around the way a real necklace would. (At least, if there's a way to do that, I don't know about it.)

The catenary equation and other related stuff is http://en.wikipedia.org/wiki/Catenary" [Broken] if you want to try it. The idea is that you would just make your squares fixed points along that curve, and just vary the curve so it has the right endpoints and hangs in the right direction. To make it swing back and forth, you just change the direction in which it's hanging, and slowly vary that direction in some sinusoidal way to make it look like it's swinging.

As I said before though, there isn't some formula that will just naturally model an object moving around in any random way. I mean, if you only want it to swing back and forth like a pendulum, that's one thing (that would be pretty much the catenary solution), but if you really want it to respond to an accelerometer as if you were shaking a box containing a necklace (which is what I thought you were saying), then you need a whole mess of code.

Last edited by a moderator:
I'm not so worried about the box shaking. Although that would be cool to have that kind of response. I'm mostly worried about this acting like the box is "wearing" the necklace.

It sounds like the catenary is really what you want, then.

Unfortunately, I seem to suck at math, and cannot find the formula to get the x and y offsets of the catenary in order to fit two arbitrary points for a given a. I'm giving up for tonight.

Fair enough man. Like I said, this is a side project I've been working on. I'm in no rush. But I would love to get this working. Don't lose any sleep over it. But if you help me figure it out, credit will be given, as it is undoubtedly due.

Xezlec: How hard would it be to actually get the shaking thing to work? The more I think about it the more that sounds cool and like it should be my goal. I'm not scared of a whole mess of code. Just need direction.

Thanks,
Brandon

Xezlec: How hard would it be to actually get the shaking thing to work? The more I think about it the more that sounds cool and like it should be my goal. I'm not scared of a whole mess of code. Just need direction.

Thanks,
Brandon
If you had unlimited resources (i.e. time and processing power), then the thing to do would be to model your necklace as a bunch of squares constrained such the the distance between each one and its neighbor is a constant (that's the effect of the string). You then apply gravity and derive equations of motion. Those equations are almost guaranteed to be impossible to solve analytically, i.e. to produce a nice formula for the position of each square, but you could then apply a numerical integration algorithm (what you described earlier in a basic form) to solve the equations numerically.

This would work - in principle(!!). Here are what I see as the main stumbling blocks:

1. The equations of motion are possibly not trivial to derive. If I were forced to do this, I would attempt to use something called Hamiltonian mechanics with Lagrange multipliers to provide the contraints of the strings. People smarter than I am might be able to do this in a reasonable amount of time - it would take me a certain amount of struggling. I would not, however, attempt to do this by simply applying Newton's Laws - this kind of problem is the reason Hamilton invented his formulation.

2. Numerically integrating the (probably) complicated equations of motion is probably going to be processor-intensive, and I have no idea if the processing a power available on current handheld devices is sufficient to do it in real time, i.e. to be able to display the results as you're moving the device.

I suspect you're going to have to make certain compromises in the name of practicality, but otherwise, this sounds like a nice idea.

P.S. Sorry about the "perturbations" remark earlier - that's Physics-speak for "things that change stuff", i.e. external forces.

One more thing: to make this thing look realistic, you probably want to include the effects of friction. If I were to tilt my wife over to a 45 degree angle, her necklace would sway over to the side, but it wouldn't just keep swinging forever like a pendulum. Rather, it would come to a pretty quick stop due to the friction against her lovely bosom.

This could actually simplify things, since you might just want the necklace to re-orient itself whenever the phone is tilted in a different direction but just to move smoothly to that new orientation without any back-and-forth swinging.

Ideally, a little swinging would be nice - say one or two swings - before it stopped, but to do that realistically complicates the problem a bit.

Xezlec: How hard would it be to actually get the shaking thing to work? The more I think about it the more that sounds cool and like it should be my goal. I'm not scared of a whole mess of code. Just need direction.
This is what I'm doing now. One question: you have an X and Y acceleration of the box, I take it, but what about angular acceleration? Do we know that?

If you had unlimited resources (i.e. time and processing power), then the thing to do would be to model your necklace as a bunch of squares constrained such the the distance between each one and its neighbor is a constant (that's the effect of the string).
What I'm doing is much simpler. Like I said before, I'm just using a spring equation (that only kicks in after a certain distance).

1. The equations of motion are possibly not trivial to derive. If I were forced to do this, I would attempt to use something called Hamiltonian mechanics with Lagrange multipliers to provide the contraints of the strings. People smarter than I am might be able to do this in a reasonable amount of time - it would take me a certain amount of struggling. I would not, however, attempt to do this by simply applying Newton's Laws - this kind of problem is the reason Hamilton invented his formulation.
Yes, I'm very well aware of the existence of proper algorithms to do this. I have no intention of doing that. Sorry, but that's just too much work. Like I said, I'm going to use Newton's laws because it's easy and might work passably well. There are only a few bodies in the problem. Like I also said, it will need a lot of damping, and may need some ad-hoc corrections because I'm not using a very good algorithm. If the phone doesn't have the necessary CPU to make it work decently, then I give up. If you can help solve the problem I mentioned with the catenary, that would be a great backup option. If you have any reasonable alternative suggestions, we're all listening.

OK, I give up. I wrote some code, but there's a bug somewhere, and I can't find it, and I've already spent a lot of time on it. And I still don't know if that ever would have worked well enough.

Xelec: What was the formula you were trying to use?

If I could get a formula that would take care of this, I might be able to work it out myself. Like I said, my biggest thing is I don't know how to go about solving this. I just don't know what direction to go in. I'll do some research tonight on hamiltonian mechanics. As well as the newton laws.

Thanks,
Brandon

Xelec: What was the formula you were trying to use?
We've both mentioned, there's no "formula" that's going to solve your problems for you. You have to simulate it.

http://www.angelingaround.com/

Someone knows what's up. This is flash. Click the person and move them off their seat. They swing just about like what I'm looking for.