Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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