Advertisement

Real-Time with node/socket.io {HTML5-JavaScript}

Started by November 08, 2014 05:00 PM
5 comments, last by d000hg 9 years, 11 months ago

So I have been trying to create this little real-time game similar to Tetris but with multiplayer (Head 2 Head play) and I am having extreme difficulty. I am able to send/receive the "moves" to the correct player, but eventually the game board's never match. ex: My board on my screen will be totally different then My Board on your screen.

I figured it would work a little faster to just send the "moves", like "L" for left, "R" for right, "D" for down while playing, but it doesn't seem to work correctly in the end.

Anyone got any idea's, suggestions on how I can keep both players synched constantly?

Coding Samples :: Tips :: Discussions :: Game Directory => Game Makers Forums Online RPG Creator
You need to time-stamp each move for when it's supposed to take effect, and make it take effect at that exact time in the simulation (which is not necessarily the same as wallclock time.)
This means that my board on your machine will be slightly "behind" my board on my own screen, OR each command I give on my machine will have some lag on my machine.
enum Bool { True, False, FileNotFound };
Advertisement

You need to time-stamp each move for when it's supposed to take effect, and make it take effect at that exact time in the simulation (which is not necessarily the same as wallclock time.)
This means that my board on your machine will be slightly "behind" my board on my own screen, OR each command I give on my machine will have some lag on my machine.

I am assuming you mean a time-stamp on the server side?

I already have some sort of time-stamp on the client side: This help's with when a player can "execute" a move such moving the block right or left with keyPress.


var the_date = new Date();
var stamp = the_date.getTime();
var new_stamp = the_date.getTime() + 200;

function game_loop() 
{
	the_date = new Date();
	stamp = the_date.getTime();
	if(new_stamp <= stamp)
	{
		if(player_move[0] != "")
		{
			Player1.checkMoveBlock(player_move[0]);
			player_move[0] = "";
		}

		the_date = new Date();
		new_stamp = the_date.getTime() + 200;

		//RUN GAME for players
		Player1.execu();
		Player2.execu();
	
	}
	
	if(player_move[1] != "")
	{
		Player2.checkMoveBlock(player_move[1]);
		player_move[1] = "";
	}
	
	Player1.render();
	Player2.render();
	
	// request new frame
	requestAnimFrame(function()
	{
	  game_loop();
	});
} 
Coding Samples :: Tips :: Discussions :: Game Directory => Game Makers Forums Online RPG Creator

I am assuming you mean a time-stamp on the server side?


Not quite. I meant that, for your simulation, it has to be very disciplined about time.
Your simulation is a function of state and player input and time step, yielding a new state.
What you want is for the state in two separate parallel simulations to be the same over time.
This means that steps need to be taken in the same order, for the same duration, with the same inputs.

The easiest way to do this, is to fix your time step (there are exactly X time steps per second,) and to tag each command with which simulation step number it took effect. The first step is 0, the next is 1, if the player then presses "right," then the "right" command is tagged with step number 2.
This means that a remote simulation of the state cannot step forward from step A to step (A+1) until the input for step A has actually arrived (the "input" may be "the player did nothing.")
enum Bool { True, False, FileNotFound };

I think I may be following, so say its 10x a second:

Each x is the step on client?

User 1: step 1 = nothing

User 1: step 2 = nothing, but presses left

User1&2: step 3 = command: user1 Pressed Left, did nothing

User1: step 4 = nothing

Something like that?

Coding Samples :: Tips :: Discussions :: Game Directory => Game Makers Forums Online RPG Creator
What you want to do is lockstep networking. It works perfectly well when just replaying the input, i've used it in sport titles on console back when modem play was your target demographic.

Basically each side send commands for a frame and wait for the other side to say they're ready to process the next one. Most bugs with this system occurs because a non deterministic event happened and it could cause a major divergence only 300 frames down the road.

http://gafferongames.com/networking-for-game-programmers/what-every-programmer-needs-to-know-about-game-networking/
Advertisement

You can also let each client run freely but also run a hidden lock-step model. Each time you get a message from the other player, you update the hidden model - at the correct time-stamp/frame. You also apply it "right now" on the visual model. If the two result in mismatched states, you have to roll-back the visual one and reapply the moves in the right order at the right time. It means things can run more smoothly.

This topic is closed to new replies.

Advertisement