#### Archived

This topic is now archived and is closed to further replies.

# Converting float to short, save size

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

## Recommended Posts

Hi I''ve been thinking of changeing my (velocity) code. First lets say we want to move from (13,16) to (40,21), first I pass those values to a function that calculates the incx and incy to move towards that location. results P.vX: 1 P.vY: 0.185185 The fact is that I only need values between -1.0 and +1.0 in inc iny. So i was thinking of converting it to a short to save sending time. Im thinking of doing it this way:

sVPoint P;

short x,y;

P = fVelo(13,16,40,21);

cout << "P.vX: " << P.vX << endl;
cout << "P.vY: " << P.vY << endl;

x = (short)(100000.0*P.vX);
y = (short)(100000.0*P.vY);

cout << "x: " << x << endl;
cout << "y: " << y << endl;

P.vX = (float)(x/100000.0);
P.vY = (float)(y/100000.0);

cout << "P.vX: " << P.vX << endl;
cout << "P.vY: " << P.vY << endl;


Is there any negative effect with this? What is the precision on float? 5 or 6

##### Share on other sites
By "sending time" do you mean for a multiplayer game? If so then consider this: you will be stuffing this and other information into a packet of some type. For Windows systems you should try to make these structures 32 byte because the OS is designed to operate on data of that size (at least I think it is 32 bytes). From what I''ve seen in books and such is that if a structure doesn''t have enough information to fill 32 bytes then extra useless information is added to them for padding. The little space you save from going from float to short isn''t going to be a great deal anyway and the speed increase you will get will probably be worth doing.

This does not come from personal experience but purely from messages here and a couple programming books (LostLogic''s and the one by Jim Adams to be exact). I don''t have the books with me ATM or I''d look it up and give you the exact passage. I''m sure someone here will correct me on the size if I''m wrong.

Hope this helps,
Webby

##### Share on other sites
Structures are aligned to 4 byte (32 *bits*) boundaries unless changed with the #pragma pack(1) directive.

Yes CPU''s do work more efficiently on datatypes that are aligned to 4 byte boundaries, however you really only need to worry about this if its in the inner loop of some mathmatically complex function or if you''re moving a ton of data from one place to the other like copying a vertex buffer to AGP memory or something of that nature.

For something as simple as keeping track of the velocity of an object and sending it across the network, I dont think the performance increase if any is worth it.

If you can shave 2 byes off a packet for a realatively small price in computation for the conversion I''d say go for it. 2 bytes certainly isnt much, but 2 bytes here and 4 bytes there can really add up.

Another thing is I have to question is development this far along or is bandwidth usage that much that you need to start shaving bytes off packets? If not I wouldnt worry about it till later.

-=[ Megahertz ]=-

##### Share on other sites
No absolutely not, im being a bit silly. Velocity is sent one time and that is when you request movement. And player position is sending every 100 ms.

But if I where doing a fast 3d action multiplayer game this might be important.

##### Share on other sites
32 bits, 4 bytes, yeah, that''s what I said right? Sorry, it was early :D

Webby

##### Share on other sites
Convert to base 256.

(128-x)/128

Now you only need one byte (0-255) to represent values from -1 to 1

Ben

##### Share on other sites

Anyone with any network experience knows saving 1 byte is enough to spend a week coding. If you can save 1 byte per character, and you have a large number of characters you will increase you''re network capacity several times over. Arguing about slowdown on the client end is rediculous, because the bottleneck is in the network not in the computer, most computers nowdays have memory bandwidths thousands of times higher than network bandwidths (2GB/sec on board versus 10Mbps on a sweet internet connection). Saving any amount of space in a packet is absolutely essential, and as long as the algorythem to do it isn''t too complex I''d say go for it.

Also check out about delta-compressing you''re data.

Remember, its allways nice to have you''re game be playable to 56k''s as well as broadbanders ~SPH

##### Share on other sites
The bottleneck on the server maybe, but not on the client. I will probebly optimize the code later. But when im only sending velocity once per move request ill stick to the current code.

1. 1
2. 2
3. 3
4. 4
5. 5
Rutin
18

• 11
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631420
• Total Posts
2999990
×

## Important Information

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!