Created
November 20, 2012 15:56
-
-
Save zachstronaut/4118766 to your computer and use it in GitHub Desktop.
JavaScript Networked Games: x,y game coordinates in combined 4 byte integer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Entity coordinates. Note requirement that 0 < x < 65535 ... same for y! | |
x = 54321; | |
y = 12345; | |
// x,y now stored in single 4 byte integer | |
pos = (x << 16) + y; | |
// Get back x and y | |
x = pos >>> 16; // unsigned right shift | |
y = pos & 65535; | |
// Important: until WebSockets support a binary send mode, this is probably *not* going to save you bandwidth over a naive "x,y" | |
// One can in the meantime convert their x,y to hexadecimal before sending to save 1-2 bytes per packet. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment