nathhB / nbnet

single header C(99) library to implement client-server network code for games
zlib License
433 stars 26 forks source link

Jitter should be cast to double before dividing by 1000 #64

Closed web3gamedev closed 1 month ago

web3gamedev commented 1 month ago

I am trying to simulate jitter but noticed it was always being set to 0. After digging into the code I can see that jitter is divided by 1000 to convert it back into seconds, but because it's an int it gets set to 0.

int jitter = packet_simulator->jitter * 1000;

jitter = (jitter > 0) ? (rand() % (jitter * 2)) - jitter : 0;

NBN_PacketSimulatorEntry *entry = (NBN_PacketSimulatorEntry *)MemoryManager_Alloc(NBN_MEM_PACKET_SIMULATOR_ENTRY);

entry->delay = packet_simulator->ping + jitter / 1000; /* and converted back to seconds */

changing it to (double)jitter / 1000 fixes the issue for me. Am I misunderstanding how to use jitter?

I'm setting it like this:

NBN_GameClient_SetJitter(0.5)

nathhB commented 1 month ago

@web3gamedev you are understanding it properly, this is a bug. Could you create a PR with your fix please?

web3gamedev commented 1 month ago

here is the PR: #65