[JAVA] Formulas often used when making animations

Here are some formulas that are often used when making animations. Or rather, it's a memo. Other than Game, it is inevitable when thinking about interactive expressions.

If you want to implement it in Swift, you can animate it using the previously posted formula Implement EnterFrameBeacon in Swift.

Force formula

Force = mass x acceleration $ F = ma $

Acceleration formula

Acceleration = mass / force $ \displaystyle a = \frac{F}{m} $

Acceleration is also the rate of change of the passage of time.

$ \displaystyle a = \frac{dv}{dt} $

In other words, this also holds.

$ \displaystyle \frac{dv}{dt} = a = \frac{F}{m} $

Similarly, velocity is the rate of change of position over time.

$ \displaystyle v = \frac{dx}{dv} $

Numerical Integration

Current time is $ t $ $ Dt $ (delta time) for the time change of the writing step feeling

Euler integral

var t: Float = 0.0
let dt: Float = 1.0
var velocity: Float = 0.0
var position: Float = 0.0
let force: Float = 10.0
let mass: Float = 1.0
        
position = position + velocity * dt
velocity = velocity + (force / mass ) * dt
t = t + dt

Basic formula $ s = ut + 0.5at^2 $

RK4 (Runge-Kutta 4)

struct State {
    float x;
    float v;
};


struct Derivative {
    float dx; // dx/dt = velocity
    float dv; // dv/dt = acceleration
};

/**
 
 */
float acceleration(const State &state, float t){
    const float k = 10;
    const float b = 1;
    return -k * state.x - b * state.v;
}

/**
 
 */
Derivative evaluate(const State &initial, float t, float dt, const Derivative &d){
    State state;
    state.x = initial.x + d.dx * dt;
    state.v = initial.v + d.dv * dt;
    
    Derivative output;
    output.dx = state.x;
    output.dv = acceleration(state, t+dt);
    return output;
}

/**
 
 */
void integrate(State &state, float t, float dt){
    Derivative a, b, c, d;
    
    a = evaluate(state, t, dt, Derivative());
    b = evaluate(state, t, dt*0.5f, a);
    c = evaluate(state, t, dt*0.5f, b);
    d = evaluate(state, t, dt, c);
    
    float dxdt = 1.0f / 6.0f * (a.dx + 2.0f*(b.dx + c.dx) + d.dx);
    float dvdt = 1.0f / 6.0f * (a.dv + 2.0f*(b.dv + c.dv) + d.dv);
    
    state.x = state.x + dxdt * dt;
    state.v = state.v + dvdt * dt;
    
}

Recommended Posts

Formulas often used when making animations
Gem often used in Rails
Matcher often used in RSpec