• Advertisement
Sign in to follow this  

Help with client side prediction

This topic is 2910 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Thanks for the help, I found out I was doing the physics a little differently on the server though, and that's what was causing most of my problems.

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
Sign in to follow this  

  • Advertisement