57

More than 5 years have passed since last update.

posted at

updated at

Organization

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

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

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

### 力の公式

$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)

### オイラー積分

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;

}



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
What you can do with signing up
57