Sign in to follow this  

Character Movement Sync

This topic is 2047 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all,

Please bare with me with this topic as this is quite new to me. I've basically created a 2D sidescroller game and working on the networking side of things. I was wondering what would be the best way to implement movement of other characters. Since when a character moves, I can send data across my socket and my socket then sends it to all clients to be updated but I'm unsure on what I should be sending. For example I could do:

1) Send Key presses. So if a client press their left key (making their character move left) I send that command to all other connected clients and that character then moves left until they send the command that they have stopped pressing the left key.

The issue I see with this is that each client may have a different FPS and the placement of each character might be different after a while and end up making the characters be in positions they are actually a 100 or 200 pixels off.

the other option would be to somehow calculate the distance the client moves when they press their left key and then send that data to all clients.

2) User moves left 10 pixels send that data to the other clients and move the character 10 pixels.

There are a few issues I see with option 2- how often and frequently should you send data across? For example, my character could be pressing left for 10 straight seconds and obviously I shouldn't wait until that user has finished the operation to update all other clients so I will need the socket to update the clients as the character is moving. I use an ENTER_FRAME event listener and so I could send socket data ever frame entry but that just seems excessive and I was wondering if there is a better way to work this out.

Any help would be greatly appreciated on this subject.

Share this post


Link to post
Share on other sites
Isnt it better to send the position or velocity instead of keypress? We call it state syncs, we send X state syncs every second. Every game the X is different, for example a FPS game the X is higher, because of the speed of the game. I think the only rule is it should always be lower than the frames per second. Edited by laapsaap

Share this post


Link to post
Share on other sites
From the client, you want to always send only key presses (or similar inputs) to avoid cheating.
From the server to the client, you typically want to send a position/velocity every once in a while (every 5 seconds, say,) but use key presses in between to save on bandwidth.

Share this post


Link to post
Share on other sites
I've managed to come up with a solution and character movement is now synced pretty much in real time. The only problem, as you say, is bandwidth. At the moment it is sending movement data for x and y positions every time it changes, which is probably not a very practical solution. I made it so it doesn't send any data if it doesn't change, but even so, it doesn't seem to wise to be sending and receiving lots of data. In terms of cheating, I will do calculations server-side, so it say a players x position is 200, and then suddenly it jumps to 1000 without any skill being used, then its obviously not going to be allowed.

Interesting you say to send key presses- but to me its not going to sync 100%, which is pretty vital in my game. It probably wouldn't make too many errors, but every now and then you probably would get a character a couple of pixels off where he/she should be, and then sending out a resync would just look weird if the character suddenly jumps back a couple of spaces.

I've also been working on monster movement sync- so that the monsters position are the same for all clients. This meant having to store the monsters server-side and every time they moved, update all connected clients. I decided that instead of putting the game logic on the server, such as map coordinates and other variables that would be a pain to code, to have a host client, which would be the host for the monsters movement and send that data to all other clients. If the host disconnects from the server, another host is chosen from the client list by the server and he will then be responsible for monster movement. Its not the perfect solution and really needs some more optimisation, especially if there is like, 50 monsters on one map. I may end up sending an array of all monster movement at a time instead of sending each individual monster's movement.

Anyways, if you guys have any other solutions that can be used I'd be greatful. I'm not too concerned about hacking the game client at the moment, just more getting things to work and optimised.

Share this post


Link to post
Share on other sites
What we do in all of our games which follow the client-server architecture is that the client sends movement actions (==keypresses & releases) to the server. The server updates the game according to the received presses, and streams absolute object positions back to each client.

To keep the bandwidth requirements to a minimum, the data the server sends for each client is carefully compressed bit by bit, using both lossless and lossy compression techniques.

The effect of compressing versus not compressing are of this magnitude: http://dl.dropbox.com/u/40949268/Tundra/Tundra_RigidBody_PhysicsScene.png This application consisted of some 50-100 boxes falling from the sky and colliding onto a ground terrain. Overall, we now require about 0.55KB/sec of data to continously stream the position of one player character from a server to one client while the character is moving. ( http://dl.dropbox.com/u/40949268/Tundra/NewRigidBodyPackets_11b.png )

The code to perform this kind of compressing replication is available in github at https://github.com/realXtend/naali/blob/tundra2/src/Core/TundraProtocolModule/SyncManager.cpp#L703

To perform the low-level compression, it utilizes a serializer object in the kNet networking library: [url="https://github.com/juj/kNet/blob/stable/include/kNet/DataSerializer.h#L119"]DataSerializer.h[/url]

Feel free to borrow any code and techniques there if it's of any help.

Share this post


Link to post
Share on other sites

This topic is 2047 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this