I'm writing an advanced interface, but I don't yet have a concept of derivatives or integrals, and I don't have an easy way to construct infinite many functions (which could effectively delay or tween their frame's contributing distance [difference between B and A] over the next few frames).

I can store values for a frame, and I can also consume them or previous values and map into A.

For example, each frame could calculate the distance between B and A, then add that distance to A, but they would be perfectly in sync.

I can keep track of the last N frames' distances and constantly shift old distances off, but this would create a delay, not an elastic effect. Somehow, the function that's popping off past-frames' distances needs to adjust for how long it's been for those 10 frames.

Is there any function I can rewrite each frame, which picks up the progress from it's predecessor, and contributes it's correct delta amount, adjusting for the new total distance between B and A?

Does this question make sense? How can I achieve behavior where A is constantly catching up to B in a non-linear, exponential way?

  • $\begingroup$ Sorry, I can't make any sense of your question. Shrinking the distance between two points across time is trivial so I can't figure out what you are after... Elastically is quite puzzling. So is an advanced interface. $\endgroup$ – Yves Daoust Apr 20 '17 at 7:14
  • $\begingroup$ @YvesDaoust Sorry, I mean as like a spring, if B and A are attached by a spring, and you drag B quickly/slowly, then A will follow B in a non-linear way $\endgroup$ – neaumusic Apr 20 '17 at 7:16
  • $\begingroup$ Is there damping ? $\endgroup$ – Yves Daoust Apr 20 '17 at 7:17
  • $\begingroup$ I guess A would have to be dampened by drag force or it's own inertia, but B is tugging A along, as if there is a light spring between the two $\endgroup$ – neaumusic Apr 20 '17 at 7:19
  • $\begingroup$ Have you seen a Neko sprite in action? $\endgroup$ – dxiv Apr 20 '17 at 7:30

You can truly simulate the physics of a dampened spring, which leads to a differential equation.

$$m\ddot x_A+d\dot x_A+k(x_A-x_B)=0$$

where $x$ is the position, $m$ the mass, $d$ a damping coefficient and $k$ the stiffness constant of the spring.

As you probably have enough with a qualitatively realistic solution, there is no need for a sophisticated ODE solver and the Euler method should be good enough.

Turn the equation in a system of two first order equations:

$$\begin{cases}\dot v_A=\dfrac{kx_B-kx_A-dv_A}m,\\\dot x_A=v_A.\end{cases}$$

Hence you will iterate

$$\begin{cases}v_{A,n+1}=v_{A,n}+\dfrac{kx_{B,n}-kx_{A,n}-dv_{A,n}}m\Delta t,\\x_{A,n+1}=x_{A,n}+ v_{A,n}\Delta t\end{cases}$$

for some time increment. This way you can compute a position from the previous, but you also need to remember the speed.

By varying the damping and stiffness coefficients, you can achieve more or less tight following of $B$, with an oscillatory behavior or not.

If you want a 2D simulation, $x$ will be a vector, and the shape of the equations remains.

  • $\begingroup$ I think this is the solution, keeping track of the current speed $\endgroup$ – neaumusic Apr 20 '17 at 18:30

How about this: each frame, set A's position to a weighted average of B's new position and A's previous position:

$$A = (1-\alpha)A + \alpha B.$$

Tune alpha to taste.


Elastic motion obeys a time differential equation representing a dynamic system of order two or higher in which elasticity constants like $m,k$ are fixed. The simplest harmonic motion $ m \ddot x + k x=0 $ enforces distances and you have no further control except on the imposed boundary conditions.

  • $\begingroup$ What more than in my solution ? $\endgroup$ – Yves Daoust Apr 20 '17 at 18:39
  • $\begingroup$ I have not read your solution, trust me. Vibration ensues and is unstoppable/uncontrollable like a stone rolling down a hill slope once pushed down. $\endgroup$ – Narasimham Apr 20 '17 at 18:45
  • 1
    $\begingroup$ Well you should have, considering it was posted over an hour before yours. Besides, adding damping to the oscillator like in Yves' solution is quite effective at removing the vibration. $\endgroup$ – Rahul Apr 20 '17 at 19:17
  • $\begingroup$ Thanks for the suggestion. Dampers are not pure elastic modifiers afaik, but may be not so always. $\endgroup$ – Narasimham Apr 20 '17 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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