Sign in to follow this  
Scouting Ninja

C# How to reduce data sizes?

Recommended Posts

I have been wondering about this for a while now.

Lets say I had a matrix that held location, rotation and scale, however all I would need was location(X,Y,Z), 8 directions(Every 45 angle) and 1 constant scale(Always 1).

What would be the most optimal way to store the value? So that it would use the least amount of memory and be very small so that you could send millions of them over a data packet?

Share this post


Link to post
Share on other sites

You can't send millions of anything in one packet.

Nor does the way you store a value necessarily relate to how you transmit a value.

One of 8 directions can be stored and transmitted in 3 bits. Scale is always 1 and therefore requires no storage or transmitted data. Location requires 3 numbers but the size of those depends on the precision of the values.

Share this post


Link to post
Share on other sites

What kind of precision do you need for your position coordinates?  What are your limits on data?  I imagine for something like this, a “snapshot frame” could send the full precision of state (X, Y, Z, rotation), while intermediary frames use less precise or fewer bits for changes between them.  If possible, I’d look into gzip compression, but I suspect it won’t have too much of an effect.

EDIT:
Some rough guesses.  Assuming some reference frame that has the full precision of the `(X, Y, Z, rotation)`, and for each frame, we use halffloat precision for deltas for a total of 27 bits/entity, then several million entities will be about 54 Mbits/update.  Or perhaps, you could use a variable number of bits and only send the components that need updating:  `(ID, (component, value), ...)`…

Share this post


Link to post
Share on other sites
8 minutes ago, Kylotan said:

You can't send millions of anything in one packet.

Interesting. Why not?

8 minutes ago, fastcall22 said:

What kind of precision do you need for your position coordinates?

Lets say the world ranges from -15 000 000 000 to +15 000 000 000 on all axis. No floating points.

12 minutes ago, Kylotan said:

One of 8 directions can be stored and transmitted in 3 bits.

What would the code for this look like?

 

Lets say the idea is the have 1 billion 2D zombies update each frame and send the update to all locations in the most optimal way. 

Share this post


Link to post
Share on other sites

The minimum amount of data you can send is 1 bit. You can't put a million bits in one packet.

You could potentially send an update that concerns millions or billions of characters and it could compress down to 1 bit, if you're lucky. That's a different problem. It's also unrealistic.

Storing 8 directions in 3 bits is just a case of reserving 3 bits of whatever storage you're using for a given number. You can flip bits in C# with the | and & operators. I suspect this is largely a waste of time compared to more productive means of reducing the cost of what you send, however.

You are not going to be able to have 1 billion zombie updates per frame without either redefining the meaning of either (a) update, (b) frame, or (c) billion.

Share this post


Link to post
Share on other sites
5 minutes ago, Kylotan said:

You are not going to be able to have 1 billion zombie updates per frame without either redefining the meaning of either (a) update, (b) frame, or (c) billion.

Would there be a reasonable amount?

I mean particles can be in the millions while holding collision data. So to what limit could we push the amount of objects if we reduce the data they use?

" 65,542 bytes in a network pack" is what I got from a quick google search. What is the rough amount of 2D zombies I could update with this?

35 minutes ago, fastcall22 said:

will be about 54 Mbits/update.

54 Mbits seem like a lot for what is needed.

 

 

Sorry if I am asking a lot of questions. I just cant understand why there are games that only update 16 characters and still send MB worth of data, while there are games updating 100 of characters for a few KB.

I assume it's because the later only send data it must and at very small precision. The question then is how? How do they reduce there data sizes?

19 minutes ago, Kylotan said:

Storing 8 directions in 3 bits is just a case of reserving 3 bits of whatever storage you're using for a given number.

I assume it has something to do with this. I have always resorted to storing data in a smaller type if I needed less precision.

For example: int MyNumber = 100; sbyte SmallerData = MyNumber.

But that means sbyte is the smallest I know how to make a number. (-128 to 127).

Share this post


Link to post
Share on other sites
6 minutes ago, Kylotan said:

To get transmission sizes down, you need to think in terms of information, not in terms of data.You're not trying to copy memory locations across the wire, you're trying to send whatever information is necessary

I feel like a idiot for not realizing this. I would never have to update the scale as it is already there and never changed. Now it makes sense, I only need to update the differences.

I could limit the data send by simply limiting the possible differences.

 

So if I had a 3rd person game where characters can jump I wouldn't need to send: bool Jump = 0; Every update. Only when the character jumps would I ever have to alert the other PC.

19 minutes ago, Kylotan said:

If you're not comfortable with bitpacking, maybe read this: https://gafferongames.com/post/reading_and_writing_packets/

I don't think I fully understand this yet. But a quick read of the link shows that it is more or less what I was thinking about. Thanks for all of the help.

Share this post


Link to post
Share on other sites

Furthering what Kylotan said about only sending the information needed rather than all the data:

If you for some reason want a million zombies on a bunch of clients and a server, I'm guessing the zombies are AI. In which case, if you calculate the exact same numerical zombie AI simulation etc on each client, in theory you only need to send the things that might affect the zombie simulation (i.e. the real players and their inputs). This is a similarish idea to client side prediction, and afaik is useful for RTS games. Presumably you have to be very careful about your simulation to make sure it runs identical on all machines.

If you really do have lots (hoping not millions) of players, then usually each client doesn't need to know about them all, only the ones within visible range. An authoritative server might keep track of all the players, but it only needs to send a subset of these to each client. And of course in the other direction the input only comes from one player per client.

Share this post


Link to post
Share on other sites

Some crash course about bit packing (i'm using hex numbers which makes it easy to understand):

int32 a = 0x01, b = 0x02, c = 0x55;

All numbers must be >=0 and <= 0xFF (255) - for that range we need 8 bits because 2^8 = 256 possibilities.

 

int32 compressed = a | (b<<8) | (c<<16);

We get 0x00550201 by bit shifting and OR to pack 3 numbers into one. (upper most 8 bits still unused - you could pack a 4th number)

 

Now unpack:

int32 A = compressed & 0xFF; // mask out the other numbers

int32 B = (compressed>>8) & 0xFF; // bit shift and mask 

int32 C = (compressed>>16); // no mask necessary if there is no 4th number

 

What to do if our original numbers are larger, but we want to use only 8 bits?

int32 a = 0x00120004;

We could ignore less important bits:

int32 qA = a>>16; // 0x00000012

then after packing and unpacking you lost 16 bits, resulting in 0x00120000 but sometimes that's good enough. Edit: E.g. if you bin characters to smaller tiles of space, less bits become enough for the position relative to the tile, which is a form of delta compression.

 

2 hours ago, Scouting Ninja said:
2 hours ago, Kylotan said:

One of 8 directions can be stored and transmitted in 3 bits.

What would the code for this look like?

You would need to tell how you store your original data. In the worst case it's a switch with 8 cases.

 

Edited by JoeJ

Share this post


Link to post
Share on other sites

If you want a billion zombies, then synchronising them over a network would be impractical... But you don't have to. You can synchronise their initial conditions and any player interactions, and then run identical updates on each client. 

https://www.gamasutra.com/view/feature/131503/1500_archers_on_a_288_network_.php

Share this post


Link to post
Share on other sites
1 hour ago, JoeJ said:

Now unpack:

int32 A = compressed & 0xFF; // mask out the other numbers

int32 B = (compressed>>8) & 0xFF; // bit shift and mask 

int32 C = (compressed>>16); // no mask necessary if there is no 4th number

This made things clearer. It turns out I have been using Bit packing for years now, except to me it's always been Texture packing.

So the same principles I would use to store textures can be used here, that makes sense. Considering that textures are made of integers.

 

23 minutes ago, Hodgman said:

But you don't have to. You can synchronise their initial conditions and any player interactions

This is what I am planning now. I can make some kind of walk states, predefined paths and checkpoints. Then just sent a integer with the number related to the path.

This would allow complex movements with a single integer per zombie.

 

 

I don't know why but this whole time I was over complicating things, some kind of mental block.:P Now everything looks so familiar and makes sense. Finally I can start trying this out for real.

Thanks for all of the help.

Share this post


Link to post
Share on other sites
25 minutes ago, Scouting Ninja said:

This made things clearer. It turns out I have been using Bit packing for years now, except to me it's always been Texture packing.

So the same principles I would use to store textures can be used here, that makes sense. Considering that textures are made of integers.

Ok, but let me make another unrelated example:

vec3 diff = findMe - nodeCenter;

int childIndex = (diff.x>0) | ((diff.y>0)<<1) | ((diff.z>0)<<2);

This way we get 3 bits from 3 comparisons and combine them to a number 0-7. If you order the children of octree in a certain order, this indexes the correct child node in an elegant and effective way. If positions are integers too, this works even without comparisons just by selecting proper bits from position (shift depends on tree level). 

 

Another one is if you have 4 packed values in one, you can add and subtract  to all of them with one op: 0x01020304 + 0x01010101 = 0x02030405. Limited, but there are practical use cases (e.g. to save registers on GPU).

 

What i mean is you should see bit packing more as a subset of bit math, which is a lot more than just compression (but not a total MUST to know).

Edited by JoeJ

Share this post


Link to post
Share on other sites
12 hours ago, Scouting Ninja said:

" 65,542 bytes in a network pack" is what I got from a quick google search. What is the rough amount of 2D zombies I could update with this?

Network packet size differs between networks. Your quoted number looks like the theoretical maximum size, which you only get with very reliable networks, eg loopback over localhost.

LANs are often around 1500. Real Internet, and mobile phone networks are even smaller. Look for the value of "mtu" of a network interface.

If you always want a single network packet for your data, you should look for the minimum guaranteed packet size rather than the largest possible packet size. The former will work on any network.

Share this post


Link to post
Share on other sites

We have had a huge ammount of AI in one game I worked on this year where we needed to simulate a 40*40km highway updating a huge ammount of vehicles. Updating a million AI agents per frame is not to solve without a huge server farm I think but why do you want to do this?

Lets assume you have a world where each player is able to see all zombies all the time, even then a zombie will typically not do anything every frame rather than stay arround most of the time. Zombies as every other AI needs a reaction time to seem realistic that may be a few hundrets of milliseconds so dosent even need to update every frame too. Lets assume this, then you could separate those million AI agents so that each agent has its reaction latency and update only a few of them every frame. Assuming 60 frames you have a frametime of 16.667 milliseconds per frame assuming a standard zombie reaction time of 200 milliseconds you need to update N / 11 zombies each frame. This are round about 91.000 zombies per frame.

Now I do not know anything about your gameplay but I do not think that you need to update 91.000 zombies each frame. Assuming a standard zombie size of 60*60cm would mean that you have a reagion of 2.730.000^2 cm or 27,3^2 km without any level geometry between them, a pure wall block of zombies. Even when your player has a view distance of 14 km, you wont need to simulate more than 10% of the zombies per frame in this region because any other zombie is too far away to be seen.

If I would make a game with such many AI, I would cheat wherever possible. Separate those zombies into different update loops that run every 10th frame for any AI that is visible (or faster like I did every 3rd frame for the vehicle AI depending on the driving speed), run your update every 20th frame for each AI that is near the player but not visible and also every 100th frame for the rest. I would do this as a sub task so every update has enought time to run and collect updates for your tasks like a packet of 20 or 50 agents data to continiously send over the network.

As I do not think you need to rotate in all three directions, you could pass one rotation value as 3 bit, one ID value as 20 bit and 35 bit for X/Z axis (Y axis is much less, maybe 16 bit) would mean 14 byte (112 bit)  per agent. A 512 byte package can then send updates for 35 agents including some overhead bytes.

Then you should reduce this too by only send updates for agents that have changed there state. I think in a real game this would reduce network traffic to a few kilobytes per second

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Announcements

  • Forum Statistics

    • Total Topics
      628342
    • Total Posts
      2982178
  • Similar Content

    • By idzohar
      I am trying to rotate my car sprite just slightly to the left or right depending on input up to a small maximum. When I use this code, the rotation doesn't stop at the maximum i give it. When i try to reset the rotation back to its original state when not pressing any input, the car also jitters between a z axis rotation of 0 and 2.5. I don't understand why the rotation doesn't stop at the given maximums, nor do I understand why its jittering. Can anybody provide me some insight?
              float movementHorizontal = 0f;
              float movementVertical = 0f;
              if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.D))
              {
              }
              else if (Input.GetKey(KeyCode.A))
              {
                  movementHorizontal = -1;
                  if (transform.rotation.z < 10f)
                  {
                      transform.Rotate(zAxis, 2.5f);
                  }            
              }
              else if (Input.GetKey(KeyCode.D))
              {
                  movementHorizontal = 1;
                  if (transform.rotation.z > -10f)
                  {
                      transform.Rotate(zAxis, -2.5f);
                  }
                  
              }
              else
              {
                  if(transform.rotation.z > 0)
                  {
                      transform.Rotate(zAxis, -2.5f);
                      //transform.rotation.Set(0, 0, 0, 0);
                  }
                  else if (transform.rotation.z < 0)
                  {
                      transform.Rotate(zAxis, 2.5f);
                      //transform.rotation.Set(0, 0, 0, 0);
                  }
                  
              }
    • By Madolite
      (Note to Mods: Could "Input" or "Peripherals" be a good Tag to add to the tag list? Just a thought.)

      Hey, I'm currently working on which keys on a keyboard that a user can rebind their actions to. The trick is that I use a Norwegian keyboard, so it's not obvious which keys correspond to the actual C#/XNA Keys enum values.

      Are there any keys from the XNA Keys enum that, in your opinion, I've neglected to add? I don't need all Keys to be bindable, only the "most commonly used keys on a keyboard". Thanks.
      https://pastebin.com/n1cz8Y0u
    • By Eck
      I just saw their courses were knocked down to $10 each and figured I'd share the info here. They have stuff for Unity, Unreal, drawing, business, etc. I haven't used their stuff before, but the previews I looked at seemed pretty good and there is a user review system as well.
      https://www.udemy.com/courses/search/?q=Unity&src=ukw
      - Eck
       
    • By sidbhati32
      I am working on a game in which we control a rectangular box at the bottom of the screen. Three sphere which has alphabets in it fall down. When the game starts, a word is generated from the predefined list of words(which I'll give) and we are supposed to touch the correct sphere having the alphabet based on that word. The question is how to detect if I have touched the correct sphere. 
      secondly, if I have touched a correct sphere before and there is no recurrence of that alphabet in that word then during the second wave the game should not proceed if I touch the same alphabet again.
      Looking forward to your answers, i have to submit this project in a couple of days. please help! (Working on Unity 3D)
      Thanks
    • By zizulot
      Probably one of the hardest Space Shooters you ever play, try your luck
      MOve with arrow keys, SPACE to shot, you can get hit only 3 times.
      Good Luck
      Available HTML5 , Windows,MacOS and Linux ,
      Get it on: https://gamejolt.com/games/spacerecker/297429
  • Popular Now