Client/Server Character Movement

Coders forum. Discuss techniques, swap code snippets, ask for help etc. relating to all forms of software development.
Post Reply
Trufun101
Baa!
Baa!
Posts: 40
Joined: Mon Sep 19, 2005 6:44 am 0
Contact:

Client/Server Character Movement

Post by Trufun101 »

Some friends and I are working on a little MMORPG. We've got the network layer working from a Player XYZ perspective.

I'm now trying to figure out the best way to render this onscreen. (btw, its a 3D game, using the Ogre engine.)

I can't simply move the player to the new XYZ, I'm going to have to smoothly transition them to the new position.

I'm thinking that I'll just queue up the points and pop off the new XYZ as needed. If the queue is empty, stop walking.

In theory this should work, but I'm kinda worried about performance. In order to get accurate movement, then a TON of XYZ broadcasts are going to occur.

Should *this* many update commands be coming from the server?
-Chris B.
User avatar
Mr Dom
Baaaaa!
Baaaaa!
Posts: 1375
Joined: Fri Dec 03, 2004 3:03 pm 0
Location: Cardiff

Post by Mr Dom »

Put simply - No.
You need to send the minimum moves across the network so...

Basically you want to send their position in 2 seconds or so time and let the client work out the smooth walk based on their current position and where the server tells it it will be at time 't'.

To read moves off the client, you do a similar thing. The player clicks, client works out the path and starts moving. The client also works out how long it takes toget there.

All you need to send to the server is the click position in the 3D world. You can then pass this position & time directly to the other clients and let them work out the intermediate positions too. If all clients & the server have the same code for the pathfinding, then given the same start position, end position, and time of arrival, thy will all generate the same path, so you only send these 3 small bitsof info for each move.

If a client is out of sync, then by sending the end position & time the client can re-sync that player.

Hope that makes sense :)
- Dom
Play Games
<Haircuts are for wimps!>
Trufun101
Baa!
Baa!
Posts: 40
Joined: Mon Sep 19, 2005 6:44 am 0
Contact:

Post by Trufun101 »

Yeah, I think I'm with you for the most part.

The interface of the game is going to be 1st/3rd person WSAD, rather than point and click, but the movement is basically the same.

At a given interval, like 2 seconds, if moving, the client will send its updated XYZ to the server. This movement will then be broadcasted to all clients in the immediate area. Each client will have a player object for each player in its immediate area. When a new movement broadcast is received, the new XYZ is queued up for the given player object, and each player object moves smoothly towards its current XYZ "goal".

Does that sound reasonable? In theory this should work, I just wanted to get some thoughts before I go down this path. :P

thanks again
-Chris B.
User avatar
Mr Dom
Baaaaa!
Baaaaa!
Posts: 1375
Joined: Fri Dec 03, 2004 3:03 pm 0
Location: Cardiff

Post by Mr Dom »

Yep - that sounds good.
Remember tho that the client should be sending where it expects to be in a second or so's time so there is not to much lag between the client moving and what the other users see.
Dont worry about getting thetiming perfect - in the most part it doesnt matter that players are actually a few seconds behind. Unless 2players sit next to each other they wont actually notice!

You can see thison WoW. Ifyou 'follow' a player, on your screen you move a metre or so behind them, on their screen you are about 5 meters behind, simply because of tis effect. With 2 million+ players noone has complained so it works ;)
- Dom
Play Games
<Haircuts are for wimps!>
User avatar
piku
Sheepie Whore
Sheepie Whore
Posts: 20960
Joined: Wed Apr 24, 2002 10:44 am 0
Website: Http://Www.ncot.uk
Location: Wakefield
Contact:

Post by piku »

Mr Dom wrote: Dont worry about getting thetiming perfect - in the most part it doesnt matter that players are actually a few seconds behind. Unless 2players sit next to each other they wont actually notice!
Same with "live" broadcast telly and radio. It's a few minutes behind the real world (and in the case of Big Brother, I think it's something like 15 minutes behind so they can censor stuff).
return (0);
Becky Rose
Baaa!
Baaa!
Posts: 114
Joined: Thu Jul 28, 2005 12:44 pm 0

Post by Becky Rose »

My method works very well except when I tried to use it in a high speed race game where the cars where driving inches under each others wing at enermous velocity in terms of the gameplay speed, so aside from that this method does the trick:

I send x/z/b (b=yaw) and any movement data which might not sync perfectly in timing code such as current speed in acceleration based games, and I also send the control key being pressed.

Because I know what key(s) is being pressed (I send a binary system so it's just one byte, 1=up, 2=down, 4=left etc) I continue to process remote players as if they are local, infact my control routines say if leftkey() then controlKey=controlKey+1 and then I have a processKeystate() function that does the actual movement - so remote players are literally being processed like a local player.

This means that movements such as turning are still processed during the turn and so the remote machines calculation is actually near identical to the sending machine.

Now this gives us a unique opportunity, most net games send data at a constant rate. This is wrong. Totally wrong. Infact it's so far wrong that if I wasn't very nearly alone in thinking this (on the indi circuit at least) i'd be calling anyone who doesn't do it an idiot/newb etc... As it stands i'm the odd one out, but i'm already that what with the lack of balls and everything so I figure what the hell...

If I send x/z/b/speed to you and you get that information, then wait 100ms, press no new controls, and then I send you new information - i'm not telling you anything you cant have figured out yourself. What matters in making net connectivity smooth isn't the frequency of the data you sent (in waterworld's ultra low bandwidth mode the standard packet interval is a whopping 45000ms).

What matter is changes in the control state, that's the information that needs to be sent, so why do most indi's wait until the next packet interval to send this information?

You are already sending the last key [combination] being pressed in addition to your x/z/yaw etc information, so calculate the binary key code every program loop even when a packet isn't due and then send your data packets when the key code information changes.

As a backup measure use a timed packet too, infact my actual method of making an instant send is to set my variable for timeUntilNextPacket to 0 when the keystate changes. This ensures that if an important packet doesn't reach the destination a new & correcting packet will arrive shortly anyway, meening that the worst that can happen is a few hundred milliseconds of "old" information being processed.

The result of this is that under localhost the network play code is totally perfect, creating an exact mimic on each machine. Under LAN it is near perfect. Over the internet you may still have some small issues however and this is because of the differential in packet delivery times.

To compensate for this also send a timeSinceLastPacketWasSent and compare that to the time it actually arrived, you now know the exact differential in packet delivery and can use that to guess-correct the position. (if your trying out Waterworld's net code I didnt do this in that game).

Finaly, if you are getting big differences between calculated position and received position, apply the difference in x/z/b gradually over a few hundred millisecs.

The only thing not addressed by the above is shadowing (compensating for the base ping rate delay) which you mentioned you've no need to cure on your game. Otherwise you'll find it as robust as a Kubelwagon, it certainly works for me and i'm just a lousy BASIC programmer and if a girl can make it work in BASIC, then it can work in anything.
Trufun101
Baa!
Baa!
Posts: 40
Joined: Mon Sep 19, 2005 6:44 am 0
Contact:

Post by Trufun101 »

Thanks everyone for your help.

I'm probably going to get cranking on this in the next few days, so I'll let you know how things go.

Thanks again,
-Chris B.
Siberwulf
Posts: 5
Joined: Fri Jan 20, 2006 3:16 am 0

Lag?

Post by Siberwulf »

Becky,

How do you accomidate from flucuating lag and TCP resending of Data? I'd assume that if there was a timedelay of 'n seconds' from when they started running and stopped running, it wouldn't matter that much. My issues is that when they issues the stop command and change keystate, what happens when the lag is n+14 seconds? I know you send a timed correction packet, but wouldn't that create a lot of bouncing, as network conditions change?
Becky Rose
Baaa!
Baaa!
Posts: 114
Joined: Thu Jul 28, 2005 12:44 pm 0

Post by Becky Rose »

If you're not worried about the time latency per se (ie: The shadowing of the base ping) then all you need to worry about is the variance in packet delivery times.

So if a packet takes 110ms to arrive, and the next packet takes 160ms to arrive, the calculations are 50ms innacurate.

In this instance the sending machine knows the delay since the last packet was sent, lets say for arguments sake the packets where sent at 100ms intervals, so they arrives at the destination T+110ms and T+260ms.

Included in the packet is the interval since the last packet was sent, so the second packet holds the data "Time since last packet = 100ms". The receiving machine knows the difference in delivery was 150ms, but it was supposed to be 100ms, so it's 50ms late.

The 50ms can then be applied to all movements/positions/rotations etc with a delta of 50ms to correct the position.

If a packet arrives earlier than is possible, the information is taken as is and the base delay is reset.
User avatar
Daniel
Baaaa!
Baaaa!
Posts: 563
Joined: Tue Sep 10, 2002 12:09 pm 0
Location: Sweden.
Contact:

Post by Daniel »

Interesting topic. Some observations:
For this kind of realtime data re-sending is not very useful, so I would use UDP instead of TCP unless your network messages are very large (>1k).
Also, as long as your network messages are much less than 1k there is no point in optimizing the message size, because the ethernet packet size is fixed at around 1400 bytes. So sending a 100 byte message is no slower than sending a one byte message.
Post Reply