50

How to simulating two client-controlled vehicles colliding (sensibly) in a typical client/server setup for a network game? I did read this eminent blog post on how to do distributed network physics in general (without traditional client prediction), but this question is specifically on how to handle collisions of owned objects.

Example

Say client A is 20 ms ahead of server, client B 300 ms ahead of server (counting both latency and maximum jitter). This means that when the two vehicles collide, both clients will see the other as 320 ms behind - in the opposite direction of the velocity of the other vehicle. Head-to-head on a Swedish highway means a difference of 16 meters/17.5 yards!

What not to try

It is virtually impossible to extrapolate the positions, since I also have very complex vehicles with joints and bodies all over, which in turn have linear and angular positions, velocities and accelerations, not to mention states from user input.

Vaillancourt
  • 1,380
  • 1
  • 11
  • 42
Jonas Byström
  • 25,316
  • 23
  • 100
  • 147

8 Answers8

12

I don't know of a perfect solution, and I have a feeling that one does not exist. Even if you could accurately predict the future position of the vehicle, you would be unable to predict the way the user will operate the controls. So the problem comes down to minimizing the negative effects of client/server lag. With that in mind, I would approach this from the position of the principle of least astonishment (paraphrased from Wikipedia):

In user interface design, the principle of least astonishment (or surprise) states that, when two elements of an interface conflict, or are ambiguous, the behaviour should be that which will least surprise the human user at the time the conflict arises.

In your example, each user sees two vehicles. Their own, and that of another player. The user expects their own vehicle to behave exactly the way they control it, so we are unable to play with that aspect of the simulation. However, the user can not know exactly how the other user is controlling their vehicle, and I would use this ambiguity to hide the lag from the user.

Here is the basic idea:

  1. The server has to make the decision about an impending collision. The collision detection algorithm doesn't have to be 100% perfect, it just has to be close enough to avoid obvious inconsistencies.
  2. Once the server has determined that two vehicles will collide, it sends each of the two users a message indicating that a collision is imminent.
  3. On client A, the position of vehicle B is adjusted (realistically) to guarantee that the collision occurs.
  4. On client B, the position of vehicle A is adjusted (realistically) to guarantee that the collision occurs.
  5. During the aftermath of the collision, the position of each vehicle can be adjusted, as necessary, so that the end result is in keeping with the rest of the game. This part is exactly what MedicineMan proposed in his answer.

In this way, each user is still in complete control of their own vehicle. When the collision occurs, it will not be unexpected. Each user will see the other vehicle move towards them, and they will still have the feeling of a real-time simulation. The nice thing is that this method reacts well in low-lag conditions. If both clients have low-latency connections to the server, the amount of adjustment will be small. The end result will, of course, get worse as the lag increases, but that is unavoidable. If someone is playing a fast-paced action game over a connection with several seconds worth of lag, they simply aren't going to get the full exeperience.

Community
  • 1
  • 1
e.James
  • 116,942
  • 41
  • 177
  • 214
  • 1
    Exactly! Your idea of hiding the post-collision calculations in the aftermath is very good. Step 5 should be dedicated to you :) The crux of my suggestion is to take small liberties with the apparent position of the other vehicle *before* the collision takes place. I hope I made that distinction clear in my answer. – e.James May 07 '09 at 20:07
  • One problem with step 1 is that the server will have to extrapolate 600 ms ahead of current server time to get position of client B. To say the least, 0.6 seconds is looong time in a fast-paced racing game. – Jonas Byström May 08 '09 at 08:55
  • Plus this starts feeling weird when you take into consideration a 3rd vehicle ramming your 2 colliding (but at different locations on the players' screens) vehicles. – Blindy Aug 08 '10 at 03:57
  • 2
    This might work head-on collisions, but is much too naive since most collisions are just minor bounces off one another. Consider for instance the truck having a jeep on top of its trailer. Or two race cars driving along side each other. Server simulation diverge too quickly. – Jonas Byström Sep 21 '10 at 08:39
  • @JonasByström, why 0.6 seconds, not 0.3? – Prof. Falken Nov 28 '12 at 22:11
  • @AmigableClarkKant: depends on solution of course, but roundtrip for 300 ms client is 600 ms, so either use 2x.3 s or 1x.6 s extrapolation. The solution I went with keeps all clients lag+jitter ahead of server, so no extrapolation is necessary, as that is not feasible in games with advanced physics simulations. – Jonas Byström Nov 29 '12 at 08:35
6

Perhaps the best thing that you can do is not so show the actual collision real time, but give the illusion that things are happening in real time.

Since the client is behind the server (lag), and the server needs to show the result of the collision, perhaps what you can do, client side, is to show a flash or explosion or some other graphic to distract the user and buy enough time on the server side to calculate the result of the collision.. When you are finished with the prediction, you ship it back to the client side for presentation.

MedicineMan
  • 15,008
  • 32
  • 101
  • 146
  • Isn't that solution too naive? What if the collision never happens on the server? – Jonas Byström May 07 '09 at 16:41
  • 1
    Voting up for the cool idea. Perhaps this could work by pretending that predicted collisions that never happened were actually collisions that were not severe enough to cause any type of feedback to the colliding objects. – Ross May 07 '09 at 17:29
2

Regarding "What not to try". You are assuming that you need to predict perfectly, but you are never going to find a perfect solution in a game with complex physics. An approximation is probably the best you can do (for example, most commercial physics engines can cast a shape into the physics scene and return the first point of collision).

For example, I implemented some critical parts of the network physics for Mercenaries 2 under the guidance of Glenn (the blog poster you mentioned). It was impossible to push all of the necessary physics state across the wire for even a single rigid body. Havok physics gradually generates contact points each frame, so the current "contact manifold" is a necessary part of the physics state to keep the simulation deterministic. It's also way too much data. Instead, we sent over the desired transform and velocities and used forces and torques to gently push bodies into place. Errors are inevitable, so you need a good error correction scheme.

Evan Rogers
  • 768
  • 4
  • 11
  • 1
    A more specific proposal (which has been suggested, but worth repeating): Keep an alternate physics scene of network-relevant bodies with simplified shapes and vehicle physics model. Use it to simulate from the remote player's last known frame to the current local frame and report the approximate point of any high velocity collision. This would allow a cheap, "pretty good" prediction which can be combined with error correction techniques. – Evan Rogers Feb 14 '10 at 05:31
  • What I ended up doing was simulating the physical bodies as close as possible to the remote ends', but "pushing" (or rather "sliding") the graphical representation. Similar soluation, but I think I got better determinism in this way. – Jonas Byström Sep 21 '10 at 08:44
2

Sorry to answer with "What not to try", but I've never heard of a solution that doesn't involve predicting the outcome on client side. Consider a simplified example:

Client A is stationary, and watching client B's vehicle approach a cliff. Client B's vehicle is capable of reducing speed to 0 instantly, and does so at the last possible moment before going over the cliff.

If Client A is attempting to show Client B's state in real time, Client A has no choice but to predict that Client B fell off the cliff. You see this a lot in MMORPGs designed such that a player's character is capable of stopping immediately when running full-speed. Otherwise, Client A could just show Client B's state as the state updates come in, but this isn't viable, as Client A needs to be able to interact with Client B in real time in your scenario (I assume).

Could you try simplifying the collision models so that extrapolation is possible for real time prediction? Maybe make your "joints and bodies all over" have processor-less-intensive physical models, like a few cubes or spheres. I'm not too familiar with how to improve the efficiency of collision detection, but I assume it's done by detecting collisions of models that are less complex than the visual models.

Ross
  • 3,709
  • 8
  • 35
  • 33
  • Good idea, but unfortunately very hard to do. All my models are already at a minimum, an excavator will for instance have only three joins for the arm, boom and hoe. – Jonas Byström Sep 21 '10 at 08:49
1

What I eventually ended up doing was simply skipping prediction alltogether and simply doing this:

  1. Client has very much say about its own position,
  2. Server (almost) only says anything about the owning client's position when a "high energy" collision has happened with another dynamic object (i.e. not static environment).
  3. Client takes meshoffset=meshpos-physpos when receiving a positional update from the server and then sets meshpos=physpos+meshoffset each frame and gradually decreases meshoffset.

It looks quite good most of the time (in low latency situation), I don't even have to slerp my quaternions to get smooth transitions.

Skipping prediction probably gives high-latency clients an awful experiance, but I don't have time to dwell on this if I'm ever going to ship this indie game. Once in a while it's nice to create a half-ass solution that works good enough but best. ;)

Edit: I eventually ended up adding the "ownership" feature that Glen Fiedler (the blogger mentioned in the question) implemented for Mercenaries 2: each client gets ownership of (dynamic) objects that they collide with for a while. This was necessary since the penetration otherwise becomes deep in high latency and high speed situations. That soluation works just as great as you'd think when you see the GDC video presentation, can definitely recommend it!

Jonas Byström
  • 25,316
  • 23
  • 100
  • 147
1

Few thoughts.

  1. Peer to peer is better at dealing with latency & high speeds.

So if this is your own engine then switch to peer to peer. You then extrapolate the other peer's vehicle based on their button input to move forward to where it is now. You the set collision such that you collide against the other vehicle as if it's world. I.e. you take the hit.

This means as you collide against the other, you bounce off, on the peer's network they bounce off you, so it looks roughly correct. The lower the latency the better it works.

  1. If you want to go client / server then this will be inferior to p2p

Things to attempt o) Extrapolate clients forward as in p2p to perform collision detection. o) Send collision results back to clients and extrapolate forward

Note, this is NEVER going to be as good as p2p. Fundamentally high speed & latency = error so removing latency is the best strategy. P2P does that.

PompeyPaul
  • 554
  • 5
  • 11
0

In addition to predicting on the client side where the other user might be and sending the collision information and how you handled it to the server, what most mmo's do to deal with lag is they have the server run "in the past" as it were. Basically they buffer the recent inputs but only react to what happened .1sec in the past. This lets you "peek into the future" when you need to (ie when a collision is about to happen in your time frame, you can look at the buffered input to see what will happen and decide if the collision is real).

Of course, this adds an extra layer of complexity to your program as you have to consider what data to send to your clients and how they should react to it. For example you could send the entire "future" buffer to the clients and let them see which possible collisions will actually happen and which won't.

Blindy
  • 65,249
  • 10
  • 91
  • 131
  • This makes the server run even more behind the client. Or do you mean that both clients and server run "in the past", so that the normal pipe can be overridden by the more current but inaccurate "approximation pipe"? – Jonas Byström Jul 14 '09 at 17:30
  • Everything runs "in the past" but with information about some of the immediate future events. It really doesn't have to be too far into the past, certainly not far enough to really notice, just enough to give you an uniform buffer to help predict collisions (and other things). – Blindy Jul 15 '09 at 11:19
-1

Ross has a good point. You could simplify the model you use to detect collisions by abstracting it to some simpler volume (i.e. the rough boxy outline of the vehicle). Then you can do the predictions based on the simple volume and the detailed calculations on the exact volumes while you have the user distracted by the "explosion". It may not be perfect but would allow you to speed up your collision detection.

dagorym
  • 5,695
  • 3
  • 25
  • 23