Jump to content
  • Advertisement
Sign in to follow this  
BiGF00T

how should i send player data?

This topic is 5389 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

for now i send it as P%.1f %.1f %.1f // position and R%.1f %.1f %.1f // rotation (view vector) then i sscanf it on the other side but it seems to me that there has to be a better way to do it.. ok, i could send only the speed and then calculate the position with speed and view vector... but there would be a difference after a while because i have only 1 position after the decimal point. or maybe only every 10th time send the position and otherwise only the speed :/ if there is a good tutorial for this then let me know or give me a hint. thx BiGF00T

Share this post


Link to post
Share on other sites
Advertisement
One optimization would be to scale the float by ten to remove the decimal point and try to send both vectors together without any extra spacing, switching to hexadecimal might also save you a couple of bytes.

void SendFloat(char *buf, float f_value) {
char sign;
int i_value = int(f_value * 10.0f);

sign = (i_value < 0) ? '-' : '+';
sprintf(buf, "%c%x", sign, abs(i_value));
}

float RecvFloat(char *buf) {
char sign;
int i_value;

sscanf(buf, "%c%x", &sign, &i_value);

if(sign == '-') {
i_value = -i_value;
}

return i_value * 0.1f;
}


However you to save some more space you probably want to send binary data instead.
Here's a snippet that uses variable length integers by sending 7-bits of data and a terminator bit per byte. This should avoid any portability problems with different floating point formats and byte orders.
The amount of storage needed should be equivalent to sending about two decimal digits per character, without any spacing.

// untested, but I hope that you get the general idea in case i screwed something up ;)
void SendFloat(float f_value) {
int i_value = int(f_value * 10.0f);

i_value <<= 1;
if(i_value < 0) {
i_value = -i_value + 1;
}

while(i_value >= 0x80) {
SendByte(i_value & 0x7f);
i_value >>= 7;
}

SendByte(0x80 | i_value);
}

float RecvFloat() {
int byte;
int i_value = 0;

while((byte = RecvByte()) < 0x80) {
i_value += byte;
i_value <<= 7;
}

i_value += byte & 0x7f;

if(i_value & 1) {
i_value = -i_value - 1;
}
i_value >>= 1;

return i_value * 0.1f;
}




edit: Oops.. I forgot about negative numbers.

[Edited by - doynax on October 14, 2004 11:54:06 AM]

Share this post


Link to post
Share on other sites
Quote:
ok, i could send only the speed and then calculate the position with speed and view vector... but there would be a difference after a while because i have only 1 position after the decimal point. or maybe only every 10th time send the position and otherwise only the speed :/


If you stick with this idea of sending positions and speeds and all this info from client to server, you are doomed to having bad performance and bad security. For example, if the client can send the speed to the server, then there is nothing stopping someone from writing a speed hack. A better method would be to just send messages to the server that indicate what action the player would like to perform. MOVE_FORWARD, FIRE_WEAPON, TURN_LEFT, CHANGE_WEAPON, etc. The server will update the game state based on the messages it recieves. Then the server can broadcast hard numbers on position and orientation to synchronize all the clients say every 10 ms (or whatever, you pick a value). Between updates the clients use interpolation and prediction to keep things smooth. You will be sending much less data and it will be more secure.

Share this post


Link to post
Share on other sites
Quote:
Original post by CodeMunkie
If you stick with this idea of sending positions and speeds and all this info from client to server, you are doomed to having bad performance and bad security. For example, if the client can send the speed to the server, then there is nothing stopping someone from writing a speed hack. A better method would be to just send messages to the server that indicate what action the player would like to perform. MOVE_FORWARD, FIRE_WEAPON, TURN_LEFT, CHANGE_WEAPON, etc. The server will update the game state based on the messages it recieves. Then the server can broadcast hard numbers on position and orientation to synchronize all the clients say every 10 ms (or whatever, you pick a value). Between updates the clients use interpolation and prediction to keep things smooth. You will be sending much less data and it will be more secure.
CodeMunkie is right. The clients shouldn't be trusted to send anything except raw input.
Unfortunately player input only constitues a fraction of the total network traffic. So you'll still need an efficient way of broadcasting the game's state to each client.

Share this post


Link to post
Share on other sites
thank you so far :)
i will try to implement some of your ideas as soon as i have some free time not consumed by learning for university...
maybe i should really go for binary transfer... and maybe i should only send the messages :/ i'm not sure how it could work if i only send the messages...
you mean that i send key_pressed and key_released messages... but wouldnt that go out of sync in no time? maybe if i would do this plus send the position/rotation values from time to time... i dont really care about security because its only a 2 player game and i doubt anyone will play it in the near future :) but maybe i should take it serious as if this would be a real project and not something i only use to learn about the basic principles of opengl, networking and sound. i'll see what i can do and post the results :)
thx for the insights you provided

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!