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).
;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))