Book Image

JMonkeyEngine 3.0 Cookbook

By : Rickard Eden
Book Image

JMonkeyEngine 3.0 Cookbook

By: Rickard Eden

Overview of this book

Table of Contents (17 chapters)
jMonkeyEngine 3.0 Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Interpolating between player positions


If we were to only run our game in a LAN environment, we would probably never expect low latency or any significant packet loss. While many are blessed even with good Internet connections nowadays, from time to time, problems still happen. One of the tricks to try to mitigate these problems is to use interpolation for entities on the client side.

This means that rather than just applying the position and rotation the client gets from the server, the client will move towards the target position and rotation in steps.

How to do it...

Perform the following steps to interpolate between the player positions:

  1. To simulate some network problems, set framerate on the server to 10.

  2. If you connect to the server now, the movement will be noticeably jerky.

  3. We replace the contents of the controlUpdate method of ClientPlayerControl with the following lines to apply the interpolation:

    float factor = tpf / 0.03f; spatial.setLocalTranslation(spatial.getLocalTranslation().interpolateLocal...