Skip to content

Instantly share code, notes, and snippets.

@zachstronaut
Created November 20, 2012 15:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save zachstronaut/4118766 to your computer and use it in GitHub Desktop.
Save zachstronaut/4118766 to your computer and use it in GitHub Desktop.
JavaScript Networked Games: x,y game coordinates in combined 4 byte integer
// 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