Advertisement

Frame\Time based client\server movement

Started by January 08, 2014 07:33 PM
6 comments, last by Grey Temchenko 10 years, 10 months ago

Hi there :) Let me first explain my goal. I have experience in dev, so this topic is not for code suggestion, but if there are some examples about my question, i will be appreciated.

So... For now i have self made C# server part, and some test cases and some samples of client on C++. But im totally new in gamedev. I have read many tutorials, ask this question on other forum but still can't figure out how to. I hope someone of you can help me.

Client: 3d game world, coords double x, y, z.

Question is the next. What is the best way to simulate client movement? But there are few conditions. Im not sure if this can be done at all, but... I want to make dynamic speed for clients(e.g. client on vechicle, client on fast mount, client on slow mount). My server supports dynamic scripts, so in theory i can make new vechicle and set speed what i want, but i don't know how client know about that? This is not required part) If this is immposible, so ok. Next, i cant figure out what is the common way to make player move with prediction and in the same time ask to server.

My last thought was: for example i stay on (10,10,0), press forward(on X axis), and get(1, 0, 0). And this is my first trouble, why 1? How client knows how far move camera? For example in walk i move with +1, in vechicle +10, in mount +5. But this values are genuine only for server, right? I can't alvays move on client with this speed. One client may have 50FPS and move slover, than client with 100FPS, but in new location 100FPS can fall to 40 and so on... Is this mean that i should dublicate speeds array to client, and calculate it depending on current FPS and use Frame based movement? And also i have read few nearby posts and ther are people say, that i should send only packet like "Forward pressed". How then i should calculate prediction if i don't know how far i move?

Sorry for my awful english :)

Clearly, entities moving with different speeds is not a problem, because many, many games support this.

I presume that you've played a variety of games such as Battlefield, Call of Duty, Planetside, World of Warcraft, and other games that supports players and vehicles with a variety of speeds? If not, then you probably should be trying these out before you try to build your own :-)

The simplest thing to do is to use an existing game engine that include player movement and networking. Unreal UDK, Crytek Engine, Source, C4 Engine, Unity, and a number of other engines come to mind.

If, for some reason, you absolutely have to build your own, then the way players move on the server depends on what your goals are.

For example, if you use a physics engine (such as ODE, Bullet, PhysX, etc) then you likely want to use the same physics engine on the server as on the client.

If totally accurate movement is very important for your game, you may want to use a lockstep simulation system, where a player doesn't move on the client until the server has received the command and sent it back to the client.

Meanwhile, if only approximate locations are OK, you can get rid of command latency by immediately moving the client, and accepting some inaccuracy. Then, how you deal with this is based on game mechanics -- direct fire like sniper rifles is different from ballistic fire like a mortar, is different from playing cards like poker.

enum Bool { True, False, FileNotFound };
Advertisement

Hi there smile.png Let me first explain my goal. I have experience in dev, so this topic is not for code suggestion, but if there are some examples about my question, i will be appreciated.

So... For now i have self made C# server part, and some test cases and some samples of client on C++. But im totally new in gamedev. I have read many tutorials, ask this question on other forum but still can't figure out how to. I hope someone of you can help me.

Client: 3d game world, coords double x, y, z.

Question is the next. What is the best way to simulate client movement? But there are few conditions. Im not sure if this can be done at all, but... I want to make dynamic speed for clients(e.g. client on vechicle, client on fast mount, client on slow mount). My server supports dynamic scripts, so in theory i can make new vechicle and set speed what i want, but i don't know how client know about that? This is not required part) If this is immposible, so ok. Next, i cant figure out what is the common way to make player move with prediction and in the same time ask to server.

My last thought was: for example i stay on (10,10,0), press forward(on X axis), and get(1, 0, 0). And this is my first trouble, why 1? How client knows how far move camera? For example in walk i move with +1, in vechicle +10, in mount +5. But this values are genuine only for server, right? I can't alvays move on client with this speed. One client may have 50FPS and move slover, than client with 100FPS, but in new location 100FPS can fall to 40 and so on... Is this mean that i should dublicate speeds array to client, and calculate it depending on current FPS and use Frame based movement? And also i have read few nearby posts and ther are people say, that i should send only packet like "Forward pressed". How then i should calculate prediction if i don't know how far i move?

Sorry for my awful english smile.png

I assume you're familiar with using delta time with the concept of velocity - movement over a given time period. This is handled by physics engines for you, so you don't need to give it too much thought. The best way to handle speeds of vehicles may be to replicate the speed information over the network, however, in most cases your vehicles will likely be defined as a class, so use the class default variables to do this rather than waste bandwidth telling the client what it already knows.

I would suggest using a fixed time step though (yes HPLUS, whilst I am not using one myself for reasons that involve optimisations), as it makes things simpler to debug. (and lends itself towards a better user experience).

I do it for myself) And i don't use any phys engines because for now, my purpose is to get basic experience in gamedev, and now i don't even try to implement some physic. On my test client i did basic movement conditions.

Hard pseudocode))


move_matrix(1.23, 0, 2.4); //for example client move forward in this direction

if(aabb.horizontal_collision)
     move_matrix(0, Y, 0); //reset X,Z move
if(aabb.vertical_collision)
      move_matrix(X, 0, Z); //reset Y move

doMove(move_matrix);

It's very simple and, i guess, one of the worst approaches, but it works) For now...

So now, my physic requirements is only simple moving.

About dynamic speed, if sometimes i get it, it would be great)

The best way to handle speeds of vehicles may be to replicate the speed information over the network, however, in most cases your vehicles will likely be defined as a class, so use the class default variables to do this rather than waste bandwidth telling the client what it already knows.

All speeds will keep on server in dynamic loaded scripts, so client don't know about new object and his speed until something happened) I don't know what.

If i just connected the game and make first move, how i know how far i should move client(sample above move_matrix)? Now my client sample do it in this way


			// If this is the first frame, pick a speed
			if (evt.timeSinceLastFrame == 0) {
				this -> _move_scale = 1;
				this -> _rot_scale = 0.1;

				// Otherwise scale movement units by time passed since last frame
			} else {
				this -> _move_scale = this -> _move_speed * evt.timeSinceLastFrame;
				this -> _rot_scale = this -> _rotate_speed * evt.timeSinceLastFrame;
			}

I take it from some Ogre example. And now i have Frame based movement, right? One man give me this article http://www.koonsolo.com/news/dewitters-gameloop/ and author says that this is bad decision, and i think the best way is last method "Constant Game Speed independent of Variable FPS". Is it mean that i in update_game ask server for speed, for new location, and then in display_game i show it?

There are two questions here:

1) What data is sent from the server to the client?

2) How does the client simulate/show that data?

The client never asks the server for any data. The server determines that the client needs information (for example, by sending position/velocity dumps every 0.1 seconds.) The client receives this data, and has the job of displaying the state of the world to the player.

The client can choose to either display "ahead" of what the server sends, which means it will "guess" and be somewhat out of sync, or the client can choose to display "behind" what the server sends, which means that it will be in sync, but delayed.

Regarding the Ogre-based code you show, that is a "variable time-step" solution, which is not generally a good idea. The canonical game loop looks more like this: http://www.mindcontrol.org/~hplus/graphics/game_loop.html

enum Bool { True, False, FileNotFound };

Ok, thanks i will use this article! But i still can't imagine how move character on client( Sorry. According to this article and the words above, i don't send data from client, so in basic way i send only packet like "Move forward request", server get this packet, check client orientation, try to move, check collision and etc, and answer back wit "OK\FAIL" packet. Is it correct? If so, how abount "strafe" - simultaneously moving to forward\back and left\right, it will be separeted packets obviously, first to forward\back, second to left\right? Or make single packet like "Strafe forward|left request"?

And question about speed again. When i first time connect to game, make move, client send packet "Move forward request", until i get answer, obviously, i can't move because i don't know speed. I guess server response like "Move OK, speed 2", i save local this speed and then make prediction to move client smoothly on 2 each time, when i change speed, server send new speed, i have to rewrite old speed and use new one to prediction? Right? Without any experience i can't picture this proccess in my head so i don't know which way to go((

Advertisement

answer back wit "OK\FAIL" packet


You are thinking in a request/response paradigm. This is fundamentally wrong for networked action games.
You need to think in a concurrent process and stream paradigm.

The client will, at a fixed rate, read inputs from user, as well as receive updates from server. Update the local simulation. Display the state of the local simulation to the user.

The server will, at a fixed rate, receive inputs from all users. Apply inputs to server simulation, as well as send out received inputs to all users observing the entities in question. Update the server simulation.

Typically, you schedule physical simulation to run at a fixed rate -- 30, 60 or 120 Hz is common. You schedule network send/receive to run at a slower fixed rate -- 10 or 20 Hz is common. This means that inputs received from the client are bundled together for many time steps into a single update packet. This effectively adds some latency between client and server, but increases efficiency, especially in cases where the client needs to receive updates/inputs for many remote clients at a time.

Also, requests of different kinds are bundled into a number of messages, all of which are sent as a single packet. For example, if the user is pressing "forward" and "sideways," then you either include two messages for the same timestep, or you define the movement message as being "the set of keys I'm holding down is (forward+sideways-left)."
For a FPS, you may be able to compress all movement and fire input to a single byte (as a bitmask,) and the mouse-aim input to a total of three more bytes (giving a 1/4096th rotation resolution.) When you have lots of players making lost of movements, this level of compression matters.
enum Bool { True, False, FileNotFound };

Yeah of course, packet sequences, encryption and etc this is future improvement, but now it's advanced functionality for me. This time i want to create client <=> server communication even with lags and correct process data. I read all yours advices and go to try make something. Thanx. If i'll stuck with other issue, i wll ask here.

This topic is closed to new replies.

Advertisement