Sign in to follow this  
Butabee

Help with client side prediction

Recommended Posts

I'm trying to get client side prediction working but it's not going very well. I've pretty much copied the code from http://gafferongames.com/game-physics/networked-physics/ with a few changes. Here's the prediction code.
void Player::ClientCorrection(float time)
{
	std::list<OLDMOVES>::iterator it = oldMoves.begin();

	float itTime;
	if(!oldMoves.empty())
	{
		itTime = (*it).time;
		if(it != oldMoves.end())
		{
			while(time > itTime && it != oldMoves.end())
			{
				itTime = (*it).time;
				it++;

				if(it != oldMoves.end())
				{
					if(time == (*it).time)
					{
						if(((*it).pos - serverPos).length() > 1)
						{

							body->setPositionOrientation(serverPos, node->getOrientation());

							it++;

							std::list<OLDMOVES>::iterator index = it;

							while(index != oldMoves.end())
							{

								world->update((*index).frameTime);

								Ogre::Vector3 bodPos;
								Ogre::Quaternion bodOrient;
								body->getPositionOrientation(bodPos, bodOrient);
								(*index).pos = bodPos;

								index++;
							}
						}
					}
				}
			}
		}
	}
}

I'm not sure what I'm doing wrong but it is very snappy, maybe I'm not updating with the right time step is the only thing I can think of.

Share this post


Link to post
Share on other sites
You should cache more than just the position. You have to consider what your body update does, what physical properties are involved.

I would consider at least delta time (check), position (check), velocity, and orientation for each moves and correction.

If your velocity is not reset correctly when you correct your body, your compute position will likely diverge as well, triggering more corrections from the serve.

The orientation of the player would also be a good candidate. Depending on the inputs and orientation of your player, the player velocity, and therefore position will change. But that's completely dependent on how you update your player.

In short, if a correction triggers constant corrections from the server, it means you are missing a variable to correct before you do the updates.

Quote:

world->update((*index).frameTime);


That should not be necessary.

Share this post


Link to post
Share on other sites
I would also rewrite that function so it looks more like this. Note that I haven't checked that this function does exactly the same as your own, but you get the idea, don't do nested if's if it can be easily avoided.


void Player::ClientCorrection(float time)
{
std::list<OLDMOVES>::iterator it = oldMoves.begin();

float itTime;
if(oldMoves.empty())
return;

itTime = (*it).time;
if(it == oldMoves.end())
return;

while(time > itTime && it != oldMoves.end())
{
itTime = (*it).time;
it++;

if(it == oldMoves.end())
continue;

if(time != (*it).time)
continue;

if(((*it).pos - serverPos).length() > 1)
body->setPositionOrientation(serverPos, node->getOrientation());

it++;
std::list<OLDMOVES>::iterator index = it;
while(index != oldMoves.end())
{
world->update((*index).frameTime);
Ogre::Vector3 bodPos;
Ogre::Quaternion bodOrient;
body->getPositionOrientation(bodPos, bodOrient);
(*index).pos = bodPos;
index++;
}
}
}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this