Page 1 of 1

Optimalization for Payload

Posted: Sun Dec 07, 2025 1:56 am
by miso
Optimizations to store angles in a single byte (with small loss +-1.4 degree precision)
Can reduce network payload, as 3 angles xyz fits in 3 bytes instead of 12 byte (floats).

Code: Select all

;Wraps around angles to keep low range (0-360) Works with integers only
;Angle 370 will be 10, angle -10 will be 350
Procedure.i wrapangle(iangle.i)
  ProcedureReturn ((iangle%360)+360)%360
EndProcedure


;Converts a float angle into a single byte to reduce network payload 
;this will be 4 times smaller. x,y,z angles will be 3 bytes instead of 12
Procedure.a AngleToByte(angle.f)
  Protected iangle.i , aangle.a
  iangle.i = Round(angle.f,#PB_Round_Nearest)
  iangle.i = wrapangle(iangle.i)
  aangle = Round(((iangle/360)*255),#PB_Round_Nearest)
  ProcedureReturn aangle
EndProcedure

;converts byte angle back to float. +- 1.4 degree loss predicted, but can save a lot of bandwidth.
Procedure.f ByteToAngle(angle.a)
  Protected fangle.f
  fangle.f = ((angle/255)*360)
  ProcedureReturn fangle
EndProcedure

Debug "Angle 370 will be"
Debug wrapangle(370)
Debug "Angle 370 in byte format will be"
Debug angletobyte(370)
Debug "Angle 370 converted back from byte format to float will be"
Debug ByteToAngle(AngleToByte(370))

Re: Optimalization for Payload

Posted: Sun Dec 07, 2025 4:42 am
by idle
nice but fangles might have something to say about that! :lol:

Re: Optimalization for Payload

Posted: Sun Dec 07, 2025 5:38 am
by miso
I don't want to be sued, should I rename the variable? ;) No goats or pixels had been harmed in the creation process ;)

Re: Optimalization for Payload

Posted: Sun Dec 07, 2025 8:09 am
by Fangbeast
idle wrote: Sun Dec 07, 2025 4:42 am nice but fangles might have something to say about that! :lol:
OMG..That's so corny.

Re: Optimalization for Payload

Posted: Sun Dec 07, 2025 4:06 pm
by minimy
Genial!!! This save a lot of memory.
Thanks for share miso!
miso wrote: Sun Dec 07, 2025 5:38 am I don't want to be sued, should I rename the variable? ;) No goats or pixels had been harmed in the creation process ;)
Are you sure? :lol:
idle wrote: Sun Dec 07, 2025 4:42 am nice but fangles might have something to say about that! :lol:
:lol: