Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm doing a network implementation of a fast-paced game. I have a puck on bot the server and client side of the simulation. I want to update the client puck position only if they are on the same direction and the server's puck is ahead of the client's puck. If the message comes with a delay and the server's puck is behind client's puck I will keep that way.

So I tried this, which is not working properly and sometimes causes some glitches.

b2Vec2 puckLocal = b2Vec2(puck->GetLinearVelocity().x, puck->GetLinearVelocity().y);
b2Vec2 puckServer = b2Vec2(vx, vy * -1);

puckLocal.Normalize();
puckServer.Normalize();

float_t dotProduct = puckLocal.x * puckServer.x + puckLocal.y * puckServer.y;

if (dotProduct < .9) {
    return;
}

Is it correct? I have read that if I normalize two vectors, and do the dot product I get their direction. Which -1 means opposite direction, 0 perpendicular and 1 same direction. Is that correct?

What is the best solution when the angles differs a bit? Like the result is .85 which means that they are 30 degrees from each other? Should I interpolate it? Any suggestions on that?

share|improve this question
    
I think the problem happens when they differ a lot in length. Could that be a problem aswell? –  Gilson Oct 18 '11 at 13:36
1  
Really, don't go for the 'update if ahead' stuff - it can lead to incorrect results - interpolate the results over a very small time period; just like Counter Strike and most non-RTS games do. –  Jonathan Dickinson Oct 18 '11 at 18:50
    
Am I failing to see other than its context for you, how this relates to networking? Looks like just a vector math question? –  Daniel Oct 18 '11 at 22:32

1 Answer 1

up vote 5 down vote accepted

What can affect the movement of the puck? Is this pong?

In any case, I would recommend a different synchronization technique. I would send three pieces of information from the server to the client:

  • Position
  • Velocity Magnitude and Direction
  • Time on a shared clock

The shared clock can be done many different ways, and it could depend on the language you are using. A common ways to achieve a shared clock is for the server to use its own system clock, and the client(s) use their own system clock with an offset to the server's clock. To calculate the offset, the server would send its time to the client(s), and the client(s) would take the difference in clocks minus half of the ping time to adjust for network latency.

Note: Network time synchronization is actually quite a deep subject, I have provided a very simple solution that should work over relatively short periods of time like in a networked game. If you're interested, here's some further reading: http://en.wikipedia.org/wiki/Clock_synchronization

By the time the client receives the puck packet, the time at which the event took place should be in the past, and you can interpolate where the puck should be as of right now. If the event is not in the past, you will want to re-adjust the client's clock.

Here is a related post: http://stackoverflow.com/questions/4713582/simple-online-pong-game-network-synchronization

share|improve this answer
    
Thank you for the enlightenment. –  Gilson Oct 19 '11 at 14:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.