So what am I doing is sending the position of the player every time he moves
public void update() {
int h = player.health;
int mh = player.maxHealth;
float xx = player.x;
float yy = player.y;
player.tick();
if ((xx != player.x || yy != player.y) && ticks) {
client.getServerConnection().sendUdp(new PacketPos(PlayerID, player.x, player.y));
}
if (h != player.health || mh != player.maxHealth) {
client.getServerConnection().sendUdp(new PacketHealth(player.health, player.maxHealth));
}
ticks++;
}
and my server sends the data to other clients
public void received(Connection con, Object obj) {
if (obj instanceof PacketPos) {
for (Player p : ps) {
Connection c = p.CON;
if (c.getId() != con.getId()) {
c.sendUdp(obj);
}
}
}
}
and then my clients receives that packet and moves the player with that id
public void received(Connection con, Object obj) {
if (obj instanceof PacketPos) {
PacketPos pos = (PacketPos) obj;
if(!hasID(pos)){
con.sendTcp(new PacketReqReg(pos.id)); // if it cant recognize the id, it requests registration packet
}
}
}
public boolean hasID(PacketPos pos){
for (PlayerOnline po : gclient.po) {
if (po.id == pos.id) {
po.posMove(pos.x, pos.y); // moves the player
return true;
}
}
return false;
}
It is kinda working fine when there are 2 clients, when there are 3 it starts to drop the fps.
How do I optimize that and make the movement smooth?
I have tried to make it send the pos packet every few milisec but it looks chunky…
BTW: I use NitroNet