36 lines
978 B
Go
Raw Normal View History

2024-01-24 20:57:31 +01:00
package game
2024-01-26 00:29:05 +01:00
import (
"encoding/binary"
"math"
)
// Vector3 represents a 3-dimensional vector using 32-bit float precision.
2024-01-24 20:57:31 +01:00
type Vector3 struct {
X float32 `json:"x"`
Y float32 `json:"y"`
Z float32 `json:"z"`
}
2024-01-26 00:29:05 +01:00
2024-01-26 17:14:20 +01:00
// AppendVector3 appends the raw bits of the vector to the given byte slice in XYZ order.
func AppendVector3(data []byte, vector Vector3) []byte {
2024-01-30 11:33:32 +01:00
data = AppendFloat(data, vector.X)
data = AppendFloat(data, vector.Y)
data = AppendFloat(data, vector.Z)
return data
2024-01-26 00:29:05 +01:00
}
2024-01-26 17:14:20 +01:00
// AppendFloat appends the raw bits of the float to the given byte slice in XYZ order.
func AppendFloat(data []byte, value float32) []byte {
bits := math.Float32bits(value)
return binary.LittleEndian.AppendUint32(data, bits)
}
func Vector3FromBytes(data []byte) Vector3 {
return Vector3{
X: math.Float32frombits(binary.LittleEndian.Uint32(data)),
Y: math.Float32frombits(binary.LittleEndian.Uint32(data[4:])),
Z: math.Float32frombits(binary.LittleEndian.Uint32(data[8:])),
}
}