Optimalization for Payload

Advanced game related topics
miso
Enthusiast
Enthusiast
Posts: 572
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Optimalization for Payload

Post 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))
User avatar
idle
Always Here
Always Here
Posts: 6101
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Optimalization for Payload

Post by idle »

nice but fangles might have something to say about that! :lol:
miso
Enthusiast
Enthusiast
Posts: 572
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: Optimalization for Payload

Post 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 ;)
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4795
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Optimalization for Payload

Post 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.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
minimy
Enthusiast
Enthusiast
Posts: 753
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: Optimalization for Payload

Post 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:
If translation=Error: reply="Sorry, Im Spanish": Endif
Post Reply