User input slows down simulation

Post Reply
Shtille
Posts: 5
Joined: Sat Jan 21, 2017 5:08 pm

User input slows down simulation

Post by Shtille » Thu Jul 06, 2017 1:23 pm

Hey there. I've made a simple application that uses bullet library: just a ball rolling over the ground. WASD keys apply an impulse to it. But when I hold any button (that are not even used) during ball rolling the ball slows down and stops very fast. It happens only with holding any key during simulation. I guess my problem is input processing.

My main loop:

Code: Select all

sht::system::Clock clock;
float time_gameclock = clock.GetTime();
float time_physics_prev, time_physics_curr;
time_physics_prev = time_physics_curr = time_gameclock;
const float kTickTime = 0.02f;

while (!PlatformNeedQuit())
{
	// Render a frame
	app->BeginFrame();
	app->Render();
	app->EndFrame();

	// Update physics
	time_physics_curr = clock.GetTime();
        dynamics_world_->stepSimulation(time_physics_curr - time_physics_prev, 10, 0.002f);
	time_physics_prev = time_physics_curr;

	// Game Clock part of the loop
	/*  This ticks once every TickMs milliseconds on average */
	float dt = clock.GetTime() - time_gameclock;

	while (dt >= kTickTime)
	{
		dt -= kTickTime;
		time_gameclock += kTickTime;

		PlatformPollEvents();

		app->SetFrameTime(kTickTime);
		app->Update();
	}
}
PlatformPollEvents:

Code: Select all

void PlatformPollEvents()
{
	MSG msg;
	while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
	{
		// Handle or dispatch messages
		if (msg.message == WM_QUIT)
		{
			g_window.need_quit = true;
		}
		else
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
}
WindowProc:

Code: Select all

LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
.....
	switch (uMsg)
	{
.....
        case WM_KEYDOWN:
	case WM_SYSKEYDOWN:
	{
		const sht::PublicKey translated_key = TranslateKey(wParam, lParam);
		if (translated_key == sht::PublicKey::kUnknown)
			break;
		const int modifiers = TranslateModifiers();

		app->keys().key_down(translated_key) = true;
		app->keys().modifiers() = modifiers;

		app->OnKeyDown(translated_key, modifiers);
		break;
	}
.....
	}

	return DefWindowProc(hWnd, uMsg, wParam, lParam);	// Pass Unhandled Messages
}
I have tested this code on Windows and Mac OS X. Both platforms have the same problem.
I guess you may had the same problem earlier. Any suggestions?

Shtille
Posts: 5
Joined: Sat Jan 21, 2017 5:08 pm

Re: User input slows down simulation

Post by Shtille » Thu Jul 06, 2017 1:49 pm

Sorry for inconvenience. I have found the reason of this issue. I was setting the velocity value on every key input. :lol: :lol: :lol:

Code: Select all

void GameScene::OnKeyDown(sht::PublicKey key, int mods)
{
	const float kPushPower = 2500.0f;
	sht::math::Vector3 velocity(0.0f);
	if (key == sht::PublicKey::kA)
		velocity.z += kPushPower;
	if (key == sht::PublicKey::kD)
		velocity.z -= kPushPower;
	if (key == sht::PublicKey::kS)
		velocity.x += kPushPower;
	if (key == sht::PublicKey::kW)
		velocity.x -= kPushPower;

	ball->Activate();
	ball->SetLinearVelocity(velocity);
}
So fucking stupid. :D

Post Reply