Skip to main content
deleted 164 characters in body
Source Link
genaray
  • 517
  • 5
  • 18

Network interpolation when game lagswont work for local player

Class Launcher{

// create static player, his position will be send to other clients, if connect, no need for but without it it wont work either

static Player player;

.
.
.


public void update(){
    
    gameScreen.player.update();

    // other stuff like updating packets
    .
    . 
    .

}

.
.
.

Network interpolation when game lags

Class Launcher{

// create static player, his position will be send to other clients, if connect, no need for but without it it wont work either

static Player player;

.
.
.


public void update(){
    
    gameScreen.player.update();

    // other stuff like updating packets
    .
    . 
    .

}

.
.
.

Network interpolation wont work for local player

Class Launcher{

.
.
.


public void update(){
    
    gameScreen.player.update();

    // other stuff like updating packets
    .
    . 
    .

}

.
.
.
added 2613 characters in body
Source Link
genaray
  • 517
  • 5
  • 18

>>>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{

// create static player, his position will be send to other clients, if connect, no need for but without it it wont work either

static Player player;

.
.
.


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());

.
. 
.

}

>>>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{

// create static player, his position will be send to other clients, if connect, no need for but without it it wont work either

static Player player;

.
.
.


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());

.
. 
.

}
edited title
Link
Almo
  • 6.7k
  • 6
  • 36
  • 67

Network interpolation when game laggslags

deleted 1 character in body
Source Link
genaray
  • 517
  • 5
  • 18
Loading
Source Link
genaray
  • 517
  • 5
  • 18
Loading