Saturday, October 18, 2014

Networking Interpolation


So I've almost finished my little networking framework and the biggest problem I've had is entity interpolation. I've tried so many different methods and I can't seem to apply it to my own framework properly. I'll post code snippets below but first, let me explain a little bit. Basically the server sends world snapshots (a map containing ids to a list of properties corresponding to render/sound assets) 20 times a second. The client sends an input snapshot (an array of keys/mouse buttons being pressed) also 20 times a second. Both of them simulate (the client renders and the server updates) the world at 66 frames per second. I store the previous world state as the current world state and set the current world state to the new world state every time there's a new packet (20 times a second). When I try to lerp between these two states it's either way too fast, way too slow, goes to random positions, or simply doesn't work. I've made a variable called RENDER_LATENCY which is 100 (I got it from the Source networking wiki) and my delta is an integer usually at 15-16ish (66 FPS). So my official question is, how do I fix my code so that the interpolation works properly? I'll post some snippets below to help you understand more.


Current Lerp Code:



Java Code:



float x = (float) entry.getValue().getProperties().get("x");
float y = (float) entry.getValue().getProperties().get("y");

float newX = MathUtils.lerp((float) prevWorldState.get(entry.getKey()).getProperties().get("x"), x, delta * RENDER_LATENCY);
float newY = MathUtils.lerp((float) prevWorldState.get(entry.getKey()).getProperties().get("y"), y, delta * RENDER_LATENCY);

My lerp method:

Java Code:



static public float lerp (float fromValue, float toValue, float progress) {
return fromValue + (toValue - fromValue) * progress;
}

World state handler code:

Java Code:



Map<Integer, Asset> worldState = (Map<Integer, Asset>) p.getData()[4];

Game.getInstance().setCameraX(cameraX);
Game.getInstance().setCameraY(cameraY);
Game.getInstance().setCameraScaleX(cameraScaleX);
Game.getInstance().setCameraScaleY(cameraScaleY);

Game.getInstance().setPrevWorldState(Game.getInstance().getWorldState());
Game.getInstance().setWorldState(worldState);


No comments:

Post a Comment