• 13
• 18
• 19
• 27
• 10

# Jerky movement

This topic is 3721 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi guys, I am trying to move a mesh around the screen using arrow keys. I achieved that, but it moves with a little jerky motion. It is only one mesh and I don't know why it translates like that. It must be the way that I've coded it perhaps. Furthermore, I will use the mouse later but I am not reading from mouse for now. If I press an arrow key and move the mouse, the mesh doesn't translate at all everything freezes. Would you please take a look at my code a little and tell me if I am doing something wrong here? Many thanks in advance!!!! Here is the game loop:
void Engine::Run()
{
ShowWindow(window,SW_NORMAL);
input = new Input(this);
float currenttime,lasttime,elapsed;
lasttime = 0.0;
// Enter the message loop.
MSG msg;
ZeroMemory( &msg, sizeof( MSG ) );
while( msg.message != WM_QUIT )
{
currenttime = (float)timeGetTime();
elapsed = (currenttime - lasttime)/1000;
lasttime = currenttime;
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
input->UpdateInput(); //update the input
SetupView(elapsed); // Translate the mesh
if (input->GetKeyPress(DIK_ESCAPE))
PostQuitMessage(0);
Render();
}
}
}


Here is the UpdateInput() and GetKeypress():
void Input::UpdateInput()
{
while(true)
{
lpDIKeyboardDevice->Poll();
HRESULT hr = lpDIKeyboardDevice->GetDeviceState(sizeof(keyboardBuffer),(LPVOID)&keyboardBuffer);
if (hr == DIERR_INPUTLOST || hr == DIERR_NOTACQUIRED)
{
if(FAILED(lpDIKeyboardDevice->Acquire()))
return;
}
else
{
break;
}
}
while(true)
{
lpDIMouseDevice->Poll();
HRESULT hr =  lpDIMouseDevice->GetDeviceState(sizeof(DIMOUSESTATE),&mouseState);
if (hr == DIERR_INPUTLOST || hr == DIERR_NOTACQUIRED)
{
if(FAILED(lpDIMouseDevice->Acquire()))
{
return;
}
}
else
break;
}
}

bool Input::GetKeyPress(unsigned char key)
{
if (keyboardBuffer[key]&0x80)
{
return true;
}
else
return false;
}


And finally to translate the mesh:

void Engine::SetupView(float elapsed)
{
const float speed = 560.0f;

//D3DXMatrixIdentity(&translate);

if(input->GetKeyPress(DIK_LEFT))
{
meshObj.location.x += -speed*elapsed;

}
if(input->GetKeyPress(DIK_RIGHT))
{
meshObj.location.x += speed*elapsed;
}
if(input->GetKeyPress(DIK_UP))
{
meshObj.location.y += speed*elapsed;
}
if(input->GetKeyPress(DIK_DOWN))
{
meshObj.location.y += -speed*elapsed;
}
D3DXMATRIX translate;
D3DXMatrixTranslation(&translate,meshObj.location.x,meshObj.location.y,meshObj.location.z);
device->SetTransform(D3DTS_WORLD,&translate);
}



##### Share on other sites
look where your render call is. it isn't called every time through the loop.

##### Share on other sites
Quote:
 Original post by stonemetallook where your render call is. it isn't called every time through the loop.

No that part is fine, he should be setting up his loop that way so that the message queue is emptied before getting the input and rendering. His problem is that he's updating his timer with every iteration of the loop, and not every frame. He should instead update his timer and calculate a time delta every time UpdateInput and Render are called, which is once per frame.

Also to the original poster: I very highly recommend that you don't use DirectInput for anything other than legacy gamepads/joysticks. DirectInput offers zero advantages over standard windows messages, as it's more difficult to use and also has extra overhead (since it creates an extra thread that just reads messages). In fact Microsoft recommends against using it. For keyboard, you can either process WM_KEYDOWN/WM_KEYUP messages or use GetAsyncKeystate. For the mouse, you can either use GetCursorPos or process WM_MOUSEMOVE messages. This is good for "normal" GUI cursor input, if you want very accurate mouse data that supports high-resolution hardware you should the RawInput functions of the Win32 API.