59
57

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Quad incAdvent Calendar 2015

Day 11

アニメーションを作るときによく使う公式

Last updated at Posted at 2015-12-11

アニメーションを作るときによくつかう式をまとめました。というかメモですね。
Game以外でもインタラクティブ表現を考えるときには避けては通れないところです。

Swiftで実装するなら、下記の公式を以前投稿したSwiftでEnterFrameBeaconを実装を利用してアニメーションさせたりできます。

力の公式

力 = 質量 x 加速度
$ F = ma $

加速度の公式

加速度 = 質量 / 力
$ \displaystyle a = \frac{F}{m} $

加速度は時間の経過速度の変化率でもあります。

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

つまりこちらも成り立ちます。

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

同様に速度は、時間上の位置の変化率であります

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

数値積分法 (Numerical Integration)

現在時刻を$t$
書くステップ感の時間変化を$dt$(delta time)

オイラー積分

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

基本公式
$ s = ut + 0.5at^2 $

RK4 (ルンゲクッタ 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;
    
}

59
57
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
59
57

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?