im currently developing a little duengon crawler. It should have lan multiplayer and it works great but i think im sending to much packets. Should i send packets over network, when a player moves in delta time? But thats not my question.... Sometimes it lags when more than 2 players are online. So i thought interpolation would be a great idea. But im not sure if i did that right, because it isnt much more fluently when the player moves :/ Heres what i did :
positionFromInput.set(posX, posY);
differenzX = positionFromInput.x-position.x;
differenzY = positionFromInput.y-position.y;
position.x += differenzX/10;
position.y += differenzY/10;
I thought this would be right ... but it wont work what i did i missed ?
>>>UPDATE<<<
I think it only wont work for the local player because , if theres another player, his position is sended to the host, right after that the other player is drawn correctly with interpolation...
Heres how i move my local player :
public void update(){
state = 0;
if(Gdx.input.isKeyPressed(Keys.A) && moveDirections[0]){
position.x -= Gdx.graphics.getDeltaTime() * 200f;
state = 1;
}
if(Gdx.input.isKeyPressed(Keys.D) && moveDirections[1]){
position.x += Gdx.graphics.getDeltaTime() * 200f;
state = 2;
//System.out.println(currentState);
}
if(Gdx.input.isKeyPressed(Keys.W) && moveDirections[2]){
position.y += Gdx.graphics.getDeltaTime() * 200f;
state = 3;
//System.out.println(currentState);
}
if(Gdx.input.isKeyPressed(Keys.S) && moveDirections[3]){
position.y -= Gdx.graphics.getDeltaTime() * 200f;
state = 4;
}
}
This is being called in an Launcher, which is called in the render method.
Class Launcher{
.
.
.
public void update(){
gameScreen.player.update();
// other stuff like updating packets
.
.
.
}
.
.
.
}
And this is called in the render thread :
@Override
public void render(float delta) {
.
.
.
launcher.update();
.
.
.
}
Thats not all, i also need to draw my player :
public void draw( final float posX, final float posY, OrthographicCamera camera){
interpolate(posX, posY);
camera.position.set(position.x/Box2dVars.UNIT,position.y/Box2dVars.UNIT,0);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(currentFrame, position.x/Box2dVars.UNIT, position.y/Box2dVars.UNIT, playerWidth/Box2dVars.UNIT, playerHeight/Box2dVars.UNIT);
batch.end();
}
The interpolate() is just the interpolation....
private void interpolate(float posX, float posY){
interpolatePosition.set(posX, posY);
differenzX = interpolatePosition.x-position.x;
differenzY = interpolatePosition.y-position.y;
position.x += differenzX/10;
position.y += differenzY/10;
}
To draw the player it is also gonna called in the render thread :
@Override
public void render(float delta) {
.
.
.
launcher.update();
.
.
.
player.draw(launcher.getPlayerX(),launcher.getPlayerY());
.
.
.
}