Damping Effect of Spring-Mass System (or is this ElasticEase?)

后端 未结 2 933
-上瘾入骨i
-上瘾入骨i 2021-02-13 22:15

I\'m trying to emulate an animation effect in code (almost any language would do as it appears to be math rather than language). Essentially, it is the emulation of a mass-sprin

2条回答
  •  自闭症患者
    2021-02-13 22:43

    Skip the physics and just go straight to the equation.

    parameters: “Here's what I will know in advance - the pixel distance [D] and number of seconds [T0] it takes to get from point A to point B, the number of seconds for oscillation [T1].” Also, I'll add as free parameters: the maximum size of oscillation, Amax, the damping time constant, Tc, and a frame rate, Rf, that is, at what times does one want a new position value. I assume you don't want to calculate this forever, so I'll just do 10 seconds, Ttotal, but there are a variety of reasonable stop conditions...

    code: Here's the code (in Python). The main thing is the equation, found in def Y(t):

    from numpy import pi, arange, sin, exp
    
    Ystart, D = 900., 900.-150.  # all time units in seconds, distance in pixels, Rf in frames/second
    T0, T1, Tc, Amax, Rf, Ttotal = 5., 2., 2., 90., 30., 10. 
    
    A0 = Amax*(D/T0)*(4./(900-150))  # basically a momentum... scales the size of the oscillation with the speed 
    
    def Y(t):
        if t

    The idea is linear motion up to the point, followed by a decaying oscillation. The oscillation is provided by the sin and the decay by multiplying it by the exp. Of course, change the parameters to get any distance, oscillation size, etc, that you want.

    enter image description here

    notes:

    1. Most people in the comments are suggesting physics approaches. I didn't use these because if one specifies a certain motion, it is a bit over-doing-it to start with the physics, go to the differential equations, and then calculate the motion, and tweak the parameters to get the final thing. Might as well just go right to the final thing. Unless, that is, one has an intuition for the physics that they want to work from.
    2. Often in problems like this one wants to keep a continuous speed (first derivative), but you say “immediately slows down”, so I didn't do that here.
    3. Note that the period and amplitude of the oscillation won't be exactly as specified when the damping is applied, but that's probably more detailed than you care about.
    4. If you need to express this as a single equation, you can do so using a “Heaviside function”, to turn the contributions on and off.

    At the risk of making this too long, I realized I could make a gif in GIMP, so this is what it looks like:

    enter image description here

    I can post the full code to make the plots if there's interest, but basically I'm just calling Y with different D and T0 values for each timestep. If I were to do this again, I could increase the damping (i.e., decrease Tc), but it's a bit of a hassle so I'm leaving it as is.

提交回复
热议问题