Saving entity states for networked physics

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Gregc91
Posts: 5
Joined: Thu Feb 28, 2013 1:49 pm

Saving entity states for networked physics

Post by Gregc91 »

Hi all,

I am currently working on a networked FPS and have run into some problems while implementing client side prediction.

Essentially, my clients send input to the server, and at the same time execute the input themselves. The server receives the input, performs the same move code and then sends back the result. When clients receive the result they must reset their player position to the server position and replay all input commands from that point until the current time. This allows clients to move with no input lag by staying slightly ahead of the server.

The problem I am getting is when I reset a player to a previous state. If my server sends a position to my client, I teleport my character controller to that position and even If I call an update with no move commands, after a teleport the client seems to move anyway. I can see on my server that the entity is constantly at the same position, but when I send that position and teleport to it on my client, the update loop seems to move it slightly so I am getting a jittering effect.

Is there a way to teleport a character controller (or any entity for that matter) so that it is in the exact same state that it was in on the server. IE the server doesn't move using the same position data which I send to my client, so my client shouldn't move either.

I am not sure if it is the TeleportToPosition() function I am using or whether I need to send a lot more data about the state of the entity. I can use a history of saved states on my client to rewind to if it makes it easier.

Any tips would be appreciated,

Thanks
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Saving entity states for networked physics

Post by Norbo »

Assuming all external factors (collisions and so forth) are held equal, a character controller's movement state, the body's position, and the body's velocity are the key pieces of information that must be transmitted.

If it still seems to move after doing this, you're probably running into the HorizontalMotionConstraint's position correction routine. It is designed to stop a character controller from sliding when standing still relative to its support. It is most useful when standing on rotating objects. However, if the character is teleported only slightly (below the violation threshold), the position corrector will think that it is error to be corrected.

To stop the corrector from doing this, you can either also send the server version of the HorizontalMotionConstraint's positionLocalOffset along with the rest of the character data, or you can force a recomputation of the positionLocalOffset on the client. This has the effect of telling the position corrector that the current position is the goal position, stopping it from moving the character unnecessarily.

The recomputation would look like this:

Code: Select all

                    Vector3.Multiply(ref downDirection, character.Body.Height * .5f, out positionLocalOffset);
                    positionLocalOffset = (positionLocalOffset + character.Body.Position) - supportEntity.Position;
                    positionLocalOffset = Matrix3x3.TransformTranspose(positionLocalOffset, supportEntity.OrientationMatrix);
For complete consistency, transmitting the server's value may be preferred. In most cases, though, recomputing it will suffice.
Gregc91
Posts: 5
Joined: Thu Feb 28, 2013 1:49 pm

Re: Saving entity states for networked physics

Post by Gregc91 »

Thanks a lot for the help. The positionLocalOffset was indeed the problem and all is fine now!
Post Reply