关于龙书上的问题
bool Display(float timeDelta)
{
if( Device )
{
//
// Update the scene: update camera position.
//
static float angle = (3.0f * D3DX_PI) / 2.0f;
static float height = 2.0f;
if( ::GetAsyncKeyState(VK_LEFT) & 0x8000f )
angle -= 0.5f * timeDelta;
if( ::GetAsyncKeyState(VK_RIGHT) & 0x8000f )
angle += 0.5f * timeDelta;
if( ::GetAsyncKeyState(VK_UP) & 0x8000f )
height += 5.0f * timeDelta;
if( ::GetAsyncKeyState(VK_DOWN) & 0x8000f )
height -= 5.0f * timeDelta;
D3DXVECTOR3 position( cosf(angle) * 3.0f, height, sinf(angle) * 3.0f );
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
D3DXMATRIX V;
D3DXMatrixLookAtLH(&V, &position, &target, &up);
Device->SetTransform(D3DTS_VIEW, &V);
//
// Draw the scene:
//
Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
Device->BeginScene();
Device->SetMaterial(&d3d::WHITE_MTRL);
Device->SetTexture(0, Tex);
Box->draw(0, 0, 0);
Device->EndScene();
Device->Present(0, 0, 0, 0);
}
return true;
}
我尝试将 angle -= 0.5f * timeDelta; 改成 angle -= 0.5f * 0.03; 按住左键不动就物体转的飞快,加上timeDelta转的就正常了,这是为什么?
[解决办法]
timeDelta是用来控制变量变化快慢的的,这样变量变化的快慢就可以不依赖帧率,而只与时间相关。这个例子很简单,所以程序的帧率非常高,前后两帧之间timeDelta很小,可能比0.03小一个数量级,于是就出现了你所说的情况