# Check if the vector is behind another or maybe opposite directions?

I'm doing a network game and on the client side, i interpolate the client position with the server sent extrapolated position. The client has its own physics simulation wich is corrected by the server in steps. The problem is when it laggs and i 'kick' the ball, the server gets a delayed message and sends me the position backwards of the client position wich makes the ball goes back and forth. I want to ignore those and maybe compensate that on the server, not sure though. The problem is the clock difference on those case are 0.07ms or 0.10 ms wich isn't that high to ignore the message i guess.

When i get the server position, i extrapolate with the clock interval * serverBallVelocity

Can i check if my new ball server position is behind my actual ball vector position? I tried to use the dot product after normalized the two vectors to check if they are opposite but it ain't working properly.

Any suggestions on checking that?

So you have a problem of the kind: Is a point BS in front or behind a plane defined by a point BC and a normal vector N?

• BS is the ball position as seen by the server
• BC is the ball position as seen by the client
• N is the vector which points exactly to the "front" of the ball position (usually, this will be your camera's viewing direction or a character's front direction).

Consequently, the vector , pointing from the client's to the server's ball position, will point roughly in the same direction as N if the ball position of the server is in front of the plane, and in the other direction if it's behind it. As such, the dot product of those two - - will be positive if BS is in front of BC, negative if it's behind, and around zero if it's to the side.

The calculation is then simple, in pseudo-C, and assuming three Euclidean dimensions:

bool is_in_front(vector3 ball_server,
vector3 ball_client,
vector3 front_direction)
{
double product = (ball_server.x - ball_client.x) * front_direction.x
+ (ball_server.y - ball_client.y) * front_direction.y
+ (ball_server.z - ball_client.z) * front_direction.z;
return (product > 0.0);
}

• Well, in my case i dont have the N vector. I have the ball_server and ball_client and i want to know if the client's ball is ahead of server's ball. Oct 25, 2011 at 15:14
• @Gilson: If you have no directional vector - not from the camera, not from the character, not from the playfield, not from the predicted or real movement of the ball, nothing - you have no notion what "ahead" or "behind" even mean. Your question is not answerable without this piece of information. Oct 25, 2011 at 15:18
• Would solve my problem if i create a new vector with the ball_client.position * ball_cliente.normalized_velocity * 2 or something? Oct 25, 2011 at 15:35
• @Gilson: Why not just using the ball's velocity vector for N? Oct 25, 2011 at 15:38

This is a good example of a situation you're asking for an answer to the wrong question.

You don't want to detect if the incoming vector is behind your ball. It will always be behind your ball, since there is always lag between your server and client.

The correct solution is to measure the round trip time between client and server and to adjust accordingly. If you know that you have a latency of 40ms, the ball is moving at velocity (1,-1), and you want to tell the client that the ball will be at (4,7) right now, you want to send the the position as (4.04, 6.96).

That way, when the client receives the update 40ms later, it will set the position to (1,-1) * 40ms away from where the ball was when the server sent the message, which is exactly where it should be.

You'll also want to do smoothing and interpolation, rather than teleporting the ball to the requested position.

• I believe the problem that he's having is that he kicks the ball on the client, changing its velocity, and in the round-trip time he gets a position update from the server which extrapolated using the old velocity vector. So the problem is recognizing that the server's information is out of date. Aug 23, 2012 at 14:27

As a real life example, I wanted to see if my player was allowed to attack a zombie with a weapon.

The zombie had to be in front of the player...

var playerDirection:Vector2 = VectorUtility.createFromAngle(