Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Variation on Pendulum Function

  1. Apr 29, 2009 #1
    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.

  2. jcsd
  3. May 2, 2009 #2
    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.

    Not sure exactly what you're describing. Can you draw a diagram? Including the point from which it's swinging?

    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.
  4. May 2, 2009 #3
    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 (Text):
    //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.

    Attached Files:

  5. May 2, 2009 #4
    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.
  6. May 2, 2009 #5
    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.
  7. May 2, 2009 #6
    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.

  8. May 2, 2009 #7
    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).

    OK, I guess I can try and model that as some kind of weird modified spring, I'll see what I can do.

    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.

    OK, so "the box" in this context just means the background, basically, right?

    Yeah, you and me both. But everyone gets better at communicating certain kinds of ideas as they accumulate experience in a given field.
  9. May 2, 2009 #8
    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.

  10. May 2, 2009 #9
    Ha! Awesome. OK, let me try to throw something together. This is C code, right?
  11. May 2, 2009 #10
    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.
  12. May 2, 2009 #11
    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.
  13. May 2, 2009 #12
    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?).
  14. May 2, 2009 #13
    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.

  15. May 2, 2009 #14
    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: May 4, 2017
  16. May 2, 2009 #15
    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.
  17. May 3, 2009 #16
    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.
  18. May 3, 2009 #17
    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.
  19. May 3, 2009 #18
    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.

  20. May 3, 2009 #19
    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.
  21. May 3, 2009 #20
    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.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Variation on Pendulum Function
  1. Calculus of variation (Replies: 2)