Note: the library I am using is kryonet
I am sending over 4 packets for positioning over the network:
DX(delta X), DY(delta Y) and the X and Y coordinates
My approach is to create a temporary “network” variable to check if the value of one of those variables has changed.
If it changes, it is sent over the network.
The following code is in my update method:
Update method in the player class:
if(this.dx != this.netDX)
{
PacketUpdateDX packet = new PacketUpdateDX();
packet.dx = this.dx;
network.client.sendUDP(packet);
this.netDX = dx;
}
if(this.dy != this.netDY)
{
PacketUpdateDY packet = new PacketUpdateDY();
packet.dy = this.dy;
network.client.sendUDP(packet);
this.netDY = dy;
}
if(this.x != this.netX)
{
PacketUpdateX packet = new PacketUpdateX();
packet.x = this.x;
network.client.sendUDP(packet);
this.netX = x;
}
if(this.y != this.netY)
{
PacketUpdateY packet = new PacketUpdateY();
packet.y = this.y;
network.client.sendUDP(packet);
this.netY = y;
}
Server reception of the packets
if(o instanceof PacketUpdateX){
PacketUpdateX packet = (PacketUpdateX) o;
players.get(c.getID()).x = packet.x;
packet.id = c.getID();
server.sendToAllExceptUDP(c.getID(), packet);
}else if(o instanceof PacketUpdateY){
PacketUpdateY packet = (PacketUpdateY) o;
players.get(c.getID()).y = packet.y;
packet.id = c.getID();
server.sendToAllExceptUDP(c.getID(), packet);
}else if(o instanceof PacketUpdateDX){
PacketUpdateDX packet = (PacketUpdateDX) o;
players.get(c.getID()).dx = packet.dx;
packet.id = c.getID();
server.sendToAllExceptUDP(c.getID(), packet);
}else if(o instanceof PacketUpdateDY){
PacketUpdateDY packet = (PacketUpdateDY) o;
players.get(c.getID()).dy = packet.dy;
packet.id = c.getID();
server.sendToAllExceptUDP(c.getID(), packet);
}
Client side reception of the packets
if(o instanceof PacketUpdateX)
{
PacketUpdateX packet = (PacketUpdateX) o;
SpaceoidsClient.players.get(packet.id).x = packet.x;
}else if(o instanceof PacketUpdateY)
{
PacketUpdateY packet = (PacketUpdateY) o;
SpaceoidsClient.players.get(packet.id).y = packet.y;
}else if(o instanceof PacketUpdateDX)
{
PacketUpdateDX packet = (PacketUpdateDX) o;
SpaceoidsClient.players.get(packet.id).dx = packet.dx;
}else if(o instanceof PacketUpdateDY)
{
PacketUpdateDY packet = (PacketUpdateDY) o;
SpaceoidsClient.players.get(packet.id).dy = packet.dy;
}
EDIT: I solved this problem on my own