How to constantly move position A "elastically" towards position B?

48 Views Asked by At

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?


3

There are 3 best solutions below

1
On BEST ANSWER

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.

0
On

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.

4
On

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.