「スムーズな」アニメーションを作成するにはどうすればよいですか?私はアンドロイドでゲームを開発しています。スムーズなアニメーションを作成するデフォルトの方法が最善ではないことを私は知っています:
perFrame() {
time = highrestime();
frameTime = time - lastTime;
lastTime = time;
// now you could use frameTime as delta. this will lead to not real
// smooth animations
}
このテーマに関するいくつかの文書を読みました。特に物理シミュレーションを使用する場合。有名な記事は次のようです:http://gafferongames.com/game-physics/fix-your-timestep/ 次のような基本クラスを作成しました。
public class TimeSystem {
private long mCurrentTime;
private long mAccumulator;
private Simulation mSimulation;
private Renderer mRenderer;
private static float mSimulationDelta = 1000f / 60f;
public TimeSystem(Simulation simulation, Renderer renderer) {
mCurrentTime = System.currentTimeMillis();
mAccumulator = 0;
mSimulation = simulation;
mRenderer = renderer;
}
public void notifyNextFrame() {
long newTime;
long frameTime;
newTime = System.currentTimeMillis();
frameTime = newTime - mCurrentTime;
mCurrentTime = newTime;
if(frameTime > 250)
frameTime = 250;
mAccumulator += frameTime;
// while full steps of simulation steps can be simulated
while(mAccumulator >= mSimulationDelta) {
mSimulation.simulate(mSimulationDelta);
mAccumulator -= mSimulationDelta;
}
// render
mRenderer.render(frameTime / 1000.0f); // frameTime in seconds
}
public interface Simulation
{
void simulate(float delta);
}
public interface Renderer
{
void render(float delta);
}
}
私は実際にはゲーム開発の専門家ではありませんが、この記事の基本を理解していることを願っています。.simulateは、固定のタイムステップを持つために必要な頻度で呼び出されます。問題は、複雑なシステムのアニメーションがまだスムーズではないことです。私のプログラムのFPSは約60FPSです。
テスト用のアプリケーションは、Android用のJavaで記述されています。
public float x = 40;
public float y = 40;
public float xmove = 0.1f;
public float ymove = 0.05f;
public void simulate(float delta) {
x += xmove * delta;
y += ymove * delta;
if(xmove > 0)
{
if(x > 480)
xmove = -xmove;
}
else if(xmove < 0)
{
if(x < 0)
xmove = -xmove;
}
if(ymove > 0)
{
if(y > 480)
ymove = -ymove;
}
else
{
if(y < 0)
ymove = -ymove;
}
RandomSleep(8);
}
Random rnd = new Random();
public void doDraw(Canvas canvas, float delta) {
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL);
paint.setTextSize(30);
canvas.drawColor(Color.BLUE);
canvas.drawCircle(x, y, 10, paint);
canvas.drawText("" + FPSConverter.getFPSFromSeconds(delta), 40, 40, paint);
RandomSleep(5);
}
public void RandomSleep(int maxMS)
{
try {
Thread.sleep((int)(rnd.nextDouble()*maxMS));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
合計でランダムスリープは最大になります。13ミリ秒。これは極端なようですが、与えられたシステムでは違いを感じないはずです。(ゲームシステムをシミュレートして、実際のゲームエンジンのアニメーションがスムーズでない理由を調べようとしています。そのため、最悪のシナリオを使用しています)
編集:直接的な答えがない場合は、誰かが「バグ」がどこにあるかを見つける方法を指摘できるかもしれません。このことを正しく分析するために私は何ができるでしょうか?