c++ - Non instantaneous/jerky movement using SDL2 & OpenGL -
i've been working on little 3d engine trying fix camera , make less jerky. i've been using sdl input , while works it's doing thing if press , hold button instantly move once pause , start moving making movement feel unresponsive.
i recorded gif of , while may hard see what's happening it'll give idea:
moving forward , right like:
w wwwwwwwwwwwwwwww aaaaaaaaaaaaaaaaaaaaa
the important code here feel free ask more if necessary:
//poll events sdl_event event; while (m_enginestate != enginestate::exit) { m_last = m_current; m_current = sdl_getperformancecounter(); deltatime = (double)((m_current - m_last) * 1000 / sdl_getperformancefrequency()); while (sdl_pollevent(&event)) { switch (event.type) { case sdl_quit: m_enginestate = enginestate::exit; break; case sdl_mousemotion: break; case sdl_keydown: m_keys[event.key.keysym.sym] = true; break; case sdl_keyup: m_keys[event.key.keysym.sym] = false; break; } processevents(); } void engine::processevents() { if (m_keys[sdlk_w]) { m_camera->moveforward(5.0f*(deltatime*0.001)); } if (m_keys[sdlk_s]) { m_camera->moveforward(-5.0f*(deltatime*0.001)); } if (m_keys[sdlk_d]) { m_camera->moveright(5.0f*(deltatime*0.001)); } if (m_keys[sdlk_a]) { m_camera->moveright(-5.0f*(deltatime*0.001)); } } void camera::moveforward(float amount) { m_pos += m_forward * amount; } void camera::moveright(float amount) { m_pos += glm::cross(m_forward, m_up) * amount; }
do not use sdl_pollevent
sdl_keydown
, sdl_keyup
events, subject os keyboard repeat rates. great typing, not camera/player controls. use sdl_getkeyboardstate(null) in stead query current state of keys.
for example:
uint8* keystate = sdl_getkeystate(null); if(keystate[sdl_scancode_w]) { m_camera->moveforward(5.0f*(deltatime*0.001)); } if(keystate[sdl_scancode_s]) { m_camera->moveforward(-5.0f*(deltatime*0.001)); } // etc
(answer edited take corrections comments account)
Comments
Post a Comment