Physics Simulation Forum

 

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Thu Jul 06, 2017 1:23 pm 
Offline

Joined: Sat Jan 21, 2017 5:08 pm
Posts: 5
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:
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:
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:
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?


Top
 Profile  
 
PostPosted: Thu Jul 06, 2017 1:49 pm 
Offline

Joined: Sat Jan 21, 2017 5:08 pm
Posts: 5
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:
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: Bing [Bot] and 23 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group