36 lines
978 B
Go
36 lines
978 B
Go
package game
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"math"
|
|
)
|
|
|
|
// Vector3 represents a 3-dimensional vector using 32-bit float precision.
|
|
type Vector3 struct {
|
|
X float32 `json:"x"`
|
|
Y float32 `json:"y"`
|
|
Z float32 `json:"z"`
|
|
}
|
|
|
|
// AppendVector3 appends the raw bits of the vector to the given byte slice in XYZ order.
|
|
func AppendVector3(data []byte, vector Vector3) []byte {
|
|
data = AppendFloat(data, vector.X)
|
|
data = AppendFloat(data, vector.Y)
|
|
data = AppendFloat(data, vector.Z)
|
|
return data
|
|
}
|
|
|
|
// 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:])),
|
|
}
|
|
}
|