game loop

theyesfish
Posts: 10
Joined: Sat Apr 11, 2009 7:12 pm

game loop

Post by theyesfish »

So far I've been cheating and using VSync to set my game loop. But I can't rely on that since people have different monitor speeds.

I've spent the entire day trying out game loops all over the internet, the "Bullet canonical game loop" included. They seem to work fine sure, the animation plays smoothly, the logic updates consistently. But bullet... just stutters everywhere, the simulation is still accurate, there's no tunnelling, but it just doesn't look smooth at all. This isn't a walking over terrain thing. It's a rigid-body moving through empty space thing. I can't understand what I'm doing wrong.

Here's the current code. Yeah, it's an interop assembly but it all runs on one WinMain() thread. (The message pump has been shoved inside input->update())

Code: Select all

void Engine1system::Run(SceneTick^ tick)
{
	const unsigned int TickMs = 32;
	unsigned long time_physics_prev = 0, time_physics_curr = 0;
	unsigned long time_gameclock = 0;

	physics->m_clock.reset();
	while(graphics->Render())
	{
		time_physics_curr = physics->m_clock.getTimeMilliseconds();

		float deltaTime = ((float)(time_physics_curr - time_physics_prev))/1000.0;

		time_physics_prev = time_physics_curr;

		physics->dynamicsWorld->stepSimulation(deltaTime, 10);

		System::Collections::Generic::LinkedList<AnimChannelInstance^>::Enumerator i = AnimList->GetEnumerator();
		while(i.MoveNext())
		{
			i.Current->AddTime(deltaTime);
		}

		long long dt = physics->m_clock.getTimeMilliseconds() - time_gameclock;

		while(dt >= TickMs)
		{
			dt -= TickMs;
			time_gameclock += TickMs;
			input->Update();
			//tick(); // Game logic. Makes no difference enabled or disabled.
		}
	}
}
On a side note, what is a good interval to have game logic at? That one suggested 32, others suggest 25, or 30, or 60, or 100...

I hope I'm not a pain in the ass with all these help topics - here's work in progress to make it seem less futile :D
shot.jpg
You do not have the required permissions to view the files attached to this post.
User avatar
kenshin
Posts: 36
Joined: Fri Oct 31, 2008 5:10 pm

Re: game loop

Post by kenshin »

show you mine.

Code: Select all



void MyGame::Run(void)
{
	MyToolKit* toolkit=MyToolKit::GetSingletonPtr();
	this->pushState(IntroState::GetSingletonPtr());
	float TimeLastFrame=0.;
	while(Running())
	{
		float TimeCurrentFrame = toolkit->GetTime();
		float TimeSinceLastFrame = TimeCurrentFrame - TimeLastFrame;
		TimeLastFrame = TimeCurrentFrame;
		CaptureInput();
		Update(TimeSinceLastFrame);
		Draw();
		UpdateAfterRender(TimeSinceLastFrame);
		MessagePump();

	}

}
the physics simulation:

Code: Select all

void MyPhysicsSystem::StepSimulation(float TimeElapsed)
{
	if(!mPause)
		mAccumulator+=TimeElapsed;
	while (mAccumulator>=mDt)
	{
		
		mWorld->stepSimulation(mDt,1);
		mAccumulator -= mDt;

	}

hope helpfull. :)
theyesfish
Posts: 10
Joined: Sat Apr 11, 2009 7:12 pm

Re: game loop

Post by theyesfish »

This is great, thankyou! My game has gone from being jittery to smooth.

So was the canonical gameloop wrong? It says

// Physics handling part of the loop
/* This, like the rendering, ticks every time around.
Bullet does the interpolation for us. */

But in practice putting the stepSimulation() with the frames instead of the game logic just doesn't seem to work without VSync.
User avatar
kenshin
Posts: 36
Joined: Fri Oct 31, 2008 5:10 pm

Re: game loop

Post by kenshin »

umm,you are right
User avatar
MrPuff
Posts: 14
Joined: Wed Oct 27, 2010 8:16 pm

Re: game loop

Post by MrPuff »

I have the same problem, but I don't understand where StepSimulation() is getting called in your game loop?