Send weapon fire in a phisic world

mmm so i have to use tcp to send bullet packet data? Im prefer use only UDP it isn’t possible?
Anyway, look this gif, this gif illustre my trouble:
In local my player is moving as soon as im press FOREWARD, but in server side this event is noticed whit 200 ms delay so the player move after this time.
When i relased the FOREWARD button in local my player stopping to move, but now my ping is 250 so in server this event is noticed whit 250 ms delay and so the player stopping to move after this time.
The porblem is that now server situation and client situation dont coincide.

http://img20.imageshack.us/img20/2899/troublepl8.gif

But if i dont move my player in local, i see my player move after ping delay, and i think this is very bad and make unfeeling game sensation.
So how can i solve this prob?

So if im decide to send only start fire position and direction i have to use TCP so ima sure that message arrive at server, but if im decide to send bullet position, direction and speed each update i have to use UDP, it s true?

Why i have to do this?

blow, you’re assuming you 250 ping wich is terrible ping… on servers in my local country I have ~50ms. I see your illustrated problem, but as JAW mentioned, corrections on simulated movement are very small. In your animation guy moves 2m more then it should, this can’t happen if you send messages frequently.
Furthermore, don’t confuse ping with network ups, not the same. If you have high ping, your messages will all arrive to server but reaction time they are applied will be large. Correction you need to do after appying simulation dosen’t depends on ping (theoreticly), but on network ups (messages you send to server in a second). If you send 10 ups (100ms pause), your simulation will only be wrong for maximum 100ms (case where message is sent and exactly after client changes direction or something).

About TCP vs UDP, when I researched for my game what to use I’ve noticed that reliability is very important and that you’ll eventually need it. People that started off with UDP were frequently upgraded it to be reliable in which case they should use TCP straight away. Also I didn’t came across any example where UDP is faster then TCP, seems it is just mentioned theoretically that it could be faster. I would recommend TCP.

About your shots problem… I don’t have a clue how to deal with that, but you may ask CommanderKeith, he made SGS, a small 2D game with a lot of shooting and it’s working fine.

You need interpolation or simulation of entities. And you need synchronized timers.

The server runs the timer, clients copy the timer. Server sends a timer update to all clients from time to time to keep them close to sync. Best would be to take into account the latency. Have the server send a package with his current timer time to the client and the client sends it back. The server can read the time value and compare to his current time and calculate a clients latency. Take that latency into account when syncing timers. When the server sends a sync which takes 100 ms to travel to the client, you must add 100 ms to the timer value to really be in sync with server time.

Now you can operate on each client based on server time. Each client runs a simulation of what happens on the server. And clients fill gaps of time.

A server would send a message “unit U is in position x,y with direction d and speed v at time t”. The client can then

  • save this position as reference position
  • every frame calculate a current position based on direction and speed and time elapsed since recieving the reference position

Easy example, an entity is at position 10, 10 at a server time of 1000ms, moving right (east) at 10 per second, making it 1 per 100 ms.

The client recieves the information at a time of 1200ms, he saves the reference 10,10 at 1000ms, heading right with 10 per second.
Elapsed time is 200ms, so the position offset is 2. The client draws the entity at 12,10.
He does so until he recieves a new reference information.

Ideally, the new reference information is perfectly in sync. But it is possible, that the information differes. Perhaps the unit did stop or change direction. So the current display position and the real server side position differ. Dont matter, discard the current position and use the new reference.

Consequence is, that the unit “jumps” because the client simulates it too far into a wrong direction, because he didnt know that the unit stopped or changed direction. When he gets the new information, he just corrects things by jumping / warping the units to their correct position.

In fact this doesnt matter. The differences in position are usually minimal, a few pixels. And there is the chance that the player doesnt even see affected units, because his screen position is somewhere else.

The same way damage can apply “in the past”. Client 1 sends an attack against a unit of Client 2. It takes 200 ms to the server, he calculates damage and sends it to Client 2. It travels another 200 ms. As effect, the damage arrives 400 ms later than it actually happens.

The great trick is to keep things in sync there. Imagine the unit of Client 2 dies by the damage. But the information travels 400 ms, and in the meantime, the unit does an attack and damages another unit. In fact, this would be impossible, because the unit is already dead, but Client2 doesnt know it yet. So when the server recieves the message that the unit wants do deal damage, the server needs to know, that the unit already died and thus ignore any damage done later.

The message of the article I read, which I can only confirm, is, that when you need a controlled communication, use TCP, do not use UDP and implement an own layer of transport control.

Network playing is no easy topic. You should look for some articles about this. If you have only local networks and few entites, you can allow some errors and asyncs. If you plan to use many entites like in an RTS and if you have high latency connections, you need strong synchronization algorithms and a strong server to perform the extra computation or a dedicated server.

Tell us about your game. If you use shots as objects which travel, send position and speed over UDP. If you use shots as instant effect actions, and insist on using UDP, give each shot a unique ID and let a client confirm if he got the information. While you have no confirmation, continue to send this shot to all clients that didnt confirm yet.

-JAW

Ok i have understand i think, that one you say is to draw a player opponent, its true?
My game is a 2D fast action game, like “Soldat”, each player can move very fast and can fire a lot of bullet for second.Each bullet is a real object and it must be renderizable on screen of each client!
For my player, how i can send my message at server and what my message has to contain?
I have to send only key pressed?
How i can move my player as soon as im pressing a key, if i have to send this first at server, and olny when server answar at me i can move my player?
So, i have this question:
1)Im a client, when i want to move my player, what i have to send at server? Only key press? and why?
2)If to move my player i have to wait server answer, what can i see my player moving as soon as im pressing a key?
3)In my game there is often an update, i think each game tick, becouse i have to send player’s arm position(it move whit mouse), so how often i have to send client data at server?
3)Server must send data at all clients as soon as receive a data from one client?

A small example:
Im a client,my player is in position x,y and i decide to move it right(east) whit 80pixel per second, so i press FOREWARD key for 1 second. In my local machine i move my player as soon as im press key, and i send this at server, so server can move my player server-side copy.
But when in my local machine my player is at position x+80,y (1 second is delayed) in server my player copy is at position x+80-ping_Time,y and when server send me a new position, is possible thta this position is back my local position, so my player jump back.

No offence, but … Don’t. Just don’t. Please. If you want to talk about TCP vs UDP, go to the thread that I stickied a couple of years ago, and READ it. Any posts by people who blatantly haven’t read it (and, note, I’ve read it several times, so I’ll KNOW if you haven’t) will get deleted. We’re not going to have any half-assed, ill-informed wars over it around here - one is enough :).

Everything you need to for doing the same, identical thing on server. It’s your game, you know best what makes objects moving.

well that depens on what you want. You can do that, but in fast games you really need quick reactions so what you do is move client straight away and try to duplicate on server what your client does best as you can. So you don’t correct your client with info you receive from server… no need to.

Yes, every game has an update each tick and every object is updatet with new values… Don’t quite understand your question… you must send updates often enough to make it look good on the other side. You could send 2 updates in a second, but on other side it would look like player is warping and much of simulated data would be wrong (depends on game though). Best is to send often as you can, keeping in mind bandwith.

Again it depens on game, my server sends data to clients in fixed interval, 10 times a sec. If you want more precision you could send some data right away if it is important.
Example: moving in quake like game needs precision or you’ll fall or something, so when simulating isn’t very satisfying becouse when client stops server could stop somewhere else and then warp player on server back later where he actually stoped, clif edge for example. To improve this I’ve read that programmers send, besides normal messages in interval, a message to server when player stops.

The TCP or UDP thread needs a conclusion or summary. Can anyone sum up this debate?

Its a question of some details and maybe even of what you like more.

[quote]1)Im a client, when i want to move my player, what i have to send at server? Only key press? and why?
2)If to move my player i have to wait server answer, what can i see my player moving as soon as im pressing a key?
3)In my game there is often an update, i think each game tick, becouse i have to send player’s arm position(it move whit mouse), so how often i have to send client data at server?
4)Server must send data at all clients as soon as receive a data from one client?
[/quote]
1 & 2)
You can either send action requests and wait for a server confirmation or you just act and send that to the server. To keep gameplay smooth, you generally need to respond immediately, so move the player instantly. In both cases, the server can reply with allowed or denied and if action is denied the server needs to correct the result.

It depends a little on the nature of the game. For a fast action game with a lot of bullets, I would tell the server for each object position and velocity along each axis. It would be unusually that the server denies something, and correction is likely not to be noticed by the player. A player wont see when 1 of 20 bullets jumps 4 pixels or disappears after a collision.

  1. You dont need to send everything every tick. You could:
  • split work. Put all objects into a list and send 10 packets per tick. When the list is through, go back to the beginning.
  • send only every x frames. 3 to 5 updates per seconds can be enough. A human player will not see it at all.
  • send some things more often than others
    Identify things that change often. Players change directions. Bullets are fast and so are likely to collide with something, so communicate collisions often. And you need to be sure that bullets are visibile for everyone, so send them often. If a packet drops, the next arrives soon. Send this 10 times per second or so. Other static things are less important. Crates destroyed, ammo or armor picked up, etc. Updating this 2 or 3 times a second is enough. The effect is, that an armor disappears at worst a half second later than it has actually been picked up. While you use UDP only, you can never be sure, if a information reaches the server or client. So if you send one “armor picked up” packet and it gets lost, the armor is still present for other players. Either send the armor status (present or not) every 500 ms or you need some confirmation response and send the information until you get a confirm.

Depends on how your server is written. Probably it also has a game loop. So it might be like this

  • get all incoming messages
  • verfiy messages for correctness. Send denials to clients, ignore those.
  • process messages, mark all objects that changes
  • send changed informations to all clients

Do you encounter any specific problems? I would send everything, divided among several frames. Just a list of all objects which i process over and over. Every object gets updated every few frames. Its scalable because the ammount of updates is static, it just takes more frames for one object to update again. Maybe everything works. If it does not work, try to correct the problem. Is there too much network traffic, is there too much latency, or are clients out of sync? It is easier to handle a specific problem.

-JAW

Hi

I just read another article dealing with RTS networking. It says that Dungeon Keeper used 4 “turns” per second and another fame used 6 “turns” per second. A turn is a complete exchange of information, that means, all clients send their new input to the server and the server sends all new informations to every client. The clients use interpolation to keep the visuals smooth until the next update. And interpolation in most cases isnt more than “guessing” where would the object be now, if nothing changed (if direction and speed stayed the same). Interpolation will not make decisions, so you dont decide that an object collides with another, you just make objects overlapping and wait for the server to send the collision. The server decides. Interpolation just moves the things around based on the information the client has.

For a RTS they use TCP in the article, because with TCP they can be sure, that messages do arrive and that they arrive in order. The network protocol relies on the fact that all clients are sure to get the same messages in the same order, so that all clients stay in sync. Using TCP you can develop a “fire and forget” mentality (well, send and forget), because you can be sure that your commincation arrives at the destination and in exactly the same order. This saves work for recovering lost messages and resoring message order and thus reduces the disadvantages of TCP.

But in your case, neither order of messages nor message loss should matter. Anyway, you can make your own decision. If you cant afford message loss and / or need to keep the order of the messages, try TCP, if not, use UDP. And, maybe more important for you, some professional (RTS) games use 4 - 6 network updates per second, for an action game maybe use 6 - 8 updates then. I’d try 6 and do only more if 6 isnt enough.

-JAW

fire and forget is UDP system actually… you send a message and you forget about it, not dealing when or will they come to server.
RTS are generaly slow games… for fast games 10 updates should be used I think.
JAW please read TCP vs UDP thread here, it has really lots of usefull information.

JAW - you’ve been warned, you’ve been pointed to a full detailed discussion of all the issues, and you’ve ignored it. “I can’t be bothered to read that thread” isn’t a good enough excuse. Last chance. Read it and start a new thread if you want to question TCP and UDP, but do not carry on spouting ill-informed flame bait.

blahx3, I don’t see anything wrong with the majority of his post. He was primarily explaining how another game goes about the task. I agree that it would be beneficial for anyone carrying on a discussion in the Networking board to read the flame war before posting on UDP or TCP though. In his defense though, it is really easy to divert to that topic. ::slight_smile: