Results 1 to 7 of 7
  1. #1
    JavaWizKid is offline Member
    Join Date
    Jul 2011
    Posts
    19
    Rep Power
    0

    Question Java Slick + Marte Engine Networked Game - Windows Specific Lag

    Hey,
    First post, hope it is a good one!
    I'm making a networked tank game using the Marte engine based on Slick which is based on LWJGL.
    My issue is that for some bizarre reason, when the connected players are moving, my movement is smooth for them, however when they are stationary, my movement on their end is extremely jumpy. Evidently is is not server lag or else why would it be smooth when they are moving!

    Some code which may be useful in deciphering my issue is below.

    Watcher.java - this listens for server messages and when it receives one it calls a function in the client.
    Java Code:
    import java.io.*;
    
    public class Watcher extends Thread
    {
    	private GameScene client;
    	private BufferedReader in;
    
    	public Watcher(GameScene c, BufferedReader i)
    	{
    		client = c; in = i;
    	}
    
    	public void run()
    	{
    		String line;
    		try
    		{
    			while ((line = in.readLine()) != null)
    			{
    				client.showMsg(line);
    			}
    		}
    		catch(Exception e)
    		{
    			System.exit(0);
    		}
    	}
    }
    Function called by Listener.java
    Java Code:
    	public void showMsg(final String msg) throws SlickException
    	{
    		if(msg.contains(username) && msg.contains("WasKilledBy"))
    		{
    			String tokens[] = msg.split("\\|");
    			if(tokens[2].equals(username))
    			{
    				//kills increase
    				System.out.println("You killed "+tokens[0]);
    			}
    			else if(tokens[0].equals(username))
    			{
    				//death increase
    				System.out.println("You were killed by "+tokens[2]);
    			}
    		}
    		
    		// If server message is not this clients message then read, else don't
    		if(!msg.contains(username))
    		{
    			if(msg.contains("New"))
    			{
    				addPlayer(msg);
    			}
    			if(msg.contains("Update"))
    			{
    				updateGame(msg);
    			}
    			if(msg.contains("Bullet"))
    			{
    				String tokens[] = msg.split("\\|");
    				fireBullet(Float.valueOf(tokens[2]),Float.valueOf(tokens[3]),tokens[4], tokens[1]);
    			}
    			if(msg.contains("Who"))
    			{
    				sendMessage(username,player.x,player.y,"up");
    			}
    			if(msg.contains("Exit"))
    			{
    				removePlayer(msg);
    			}
    		}
    	}
    In this case,
    Java Code:
    updateGame(msg);
    is called.


    updateGame function
    Java Code:
    	// Called whenever something happens
    	public void updateGame(final String msg) throws SlickException
    	{
    		String tokens[] = msg.split("\\|");
    		// If update message is coming from a network player who isn't a child yet, add them
    		if(find(tokens[1]) == null)
    		{
    			System.out.println("New players were added to old data...");
    			NetworkPlayer networkPlayer = new NetworkPlayer(Float.valueOf(tokens[2]),Float.valueOf(tokens[3]));
    			networkPlayer.name=tokens[1];
    			add(networkPlayer);
    		}
    		// If message comes from existing network player, move them as requested
    		if(find(tokens[1]) != null)
    		{
    			// Set connected players coordinates
    			find(tokens[1]).x=Float.valueOf(tokens[2]); 
    			find(tokens[1]).y=Float.valueOf(tokens[3]);
    			// Set connected players sprite direction
    			if(tokens[4].equals("up"))
    			{
    				find(tokens[1]).setGraphic(Player.tankUp);
    			}
    			if(tokens[4].equals("down"))
    			{
    				find(tokens[1]).setGraphic(Player.tankDown);
    			}
    			if(tokens[4].equals("right"))
    			{
    				find(tokens[1]).setGraphic(Player.tankRight);
    			}
    			if(tokens[4].equals("left"))
    			{
    				find(tokens[1]).setGraphic(Player.tankLeft);
    			}
    		}
    	}
    Hopefully this is all the code needed, if not just ask for more.

    I don't understand why it works on macs perfectly. Is it something to do with Java settings?

    Also, in the main client class file I've got these settings:
    Java Code:
    	public static void main(String[] argv)
    	{
    		try
    		{
    			AppGameContainer container = new AppGameContainer(new CastleWars("Tank Wars Yeahhh!"));
    			container.setDisplayMode(900, 700, false);
    			//container.setTargetFrameRate(60);
    			container.setAlwaysRender(true);
    			container.setVSync(true);
    			container.setSmoothDeltas(true);
    			//Renderer.setRenderer(Renderer.VERTEX_ARRAY_RENDERER);
    			container.start();
    		}
    		catch (SlickException e)
    		{
    			e.printStackTrace();
    		}
    	}
    However, playing with them doesn't seem to affect the Windows client performance at all.

    Thanks!

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    Just a guess, but you could have Nagle's algorithm turned on in your OS network stack.
    Get in the habit of using standard Java naming conventions!

  3. #3
    JavaWizKid is offline Member
    Join Date
    Jul 2011
    Posts
    19
    Rep Power
    0

    Default

    Maybe, how do I turn it off?
    Where do I put TCP_NODELAY?
    Client side or server side?
    This code is client side.
    Java Code:
    public void makeContact()
    	{
    		try
    		{
    			sock = new Socket(HOST, PORT);
    			BufferedReader in  = new BufferedReader(new InputStreamReader(sock.getInputStream()));
    			out = new PrintWriter(sock.getOutputStream(),true);
    			new Watcher(this, in).start();
    			out.println("New|"+username);
    			out.println("Who|"+username);
        	}
        	catch(Exception e)
        	{
        		System.out.println(e);
        	}
      	}
    Last edited by JavaWizKid; 07-22-2011 at 09:59 PM.

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    It used to be a common thing for gamers to do on WinXP to decrease their pings. But I've been using Linux for years and don't know much about Windows anymore. I'm sure Google knows...

    Edit: if the clients are seeing your movement as jerky, I'd guess it's your end doing the Nagling.
    Get in the habit of using standard Java naming conventions!

  5. #5
    JavaWizKid is offline Member
    Join Date
    Jul 2011
    Posts
    19
    Rep Power
    0

    Default

    But all my messages are going to the server instantly from my mac. Why would their character movement affect how smooth my character movement is?

    When they are stationary, I'm laggy.
    When they are moving, I'm smooth.

    Makes absolutely no sense at all! I've tested it with friends who have macs and there is not a single hitch wrong!

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    Yeah, that is weird. If it's not Nagle, I'm wondering if their clients are blocking waiting for the input controlling their own movement, and not updating your position during that time. But it's weird that it makes a difference what OS you're using.
    Get in the habit of using standard Java naming conventions!

  7. #7
    JavaWizKid is offline Member
    Join Date
    Jul 2011
    Posts
    19
    Rep Power
    0

    Default

    Java Code:
    setTcpNoDelay(true)
    worked, I've put it on client and server side.

Similar Threads

  1. Networked Card Game.
    By T-Prime in forum Networking
    Replies: 9
    Last Post: 07-26-2011, 12:22 AM
  2. Easy Clip2D Game Engine now posted
    By rdjava in forum Java Gaming
    Replies: 2
    Last Post: 06-03-2011, 05:18 PM
  3. Replies: 1
    Last Post: 06-02-2011, 09:29 AM
  4. Game Engine in Java, is it practical idea?
    By chan_nguyen in forum New To Java
    Replies: 2
    Last Post: 09-14-2010, 03:20 PM
  5. Crate Game Engine 20080323
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 03-25-2008, 04:50 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •