Search code examples
game-enginephysics-engine

How to calculate this simple animation effect (physics engine)?


I am implementing a very simple animation effect for a game. The scenario is like this:

  1. there is a elastic rubber line, length is 1 meter, when it is extended over 1 meter, it is elastic.

  2. the line connects two dots A and B like this, the distance is S, S > 1 meter

A <------------- B

  1. then fix dot A, and releases B, the line takes B to the direction of A

I want to know how to calculate time T, which B costs to move X meters towards A (X <= S).

Any ideas? Thanks!


Solution

  • I have been meaning to learn how to animate these kinds of images in sage (a python based platform for math) for a while, so i used this as an excuse. I hope this code snippet and image is helpful.

    A = 3
    w = 0.5 
    
    # x = f(t) = A cos(wt) inside elastic region
    # with x = displacement from 1 meter mark
    # in the below code, x is the displacement from origin (x = A cos(wt) + 1)
    
    # find speed when we cross the one meter mark
    # f'(t) = -Aw sin(wt), but this is also max speed
    # ie f'(t at one meter mark) = -Aw
    
    speed_max = -A * w
    
    # time to reach max speed + time to cross last meter
    eta  = float(pi/2 * 1/w + 1/abs(speed_max))
    
    # the function you were looking for
    def time_left(x):
        if x < 1:
            return x/abs(speed_max)
        else:
            return 1/w * arccos((x-1)/A)
    

    enter image description here

    It may not be clear in the image but within one meter of the origin there is no acceleration.