Skip to content

Commit c2ae3b1

Browse files
committed
Add Entity.Position() helper func
1 parent 8451182 commit c2ae3b1

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

sendtables/entity.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"sync"
66

7+
"github.com/golang/geo/r3"
8+
79
bit "github.com/markus-wa/demoinfocs-golang/bitread"
810
)
911

@@ -118,6 +120,28 @@ func (e *Entity) InitializeBaseline(r *bit.BitReader) map[int]PropValue {
118120
return baseline
119121
}
120122

123+
const maxCoordInt = 16384
124+
125+
// Position returns the entity's position in world coordinates.
126+
func (e *Entity) Position() r3.Vector {
127+
cellWidth := 1 << uint(e.FindProperty("m_cellbits").value.IntVal)
128+
cellX := e.FindProperty("m_cellX").value.IntVal
129+
cellY := e.FindProperty("m_cellY").value.IntVal
130+
cellZ := e.FindProperty("m_cellZ").value.IntVal
131+
offset := e.FindProperty("m_vecOrigin").value.VectorVal
132+
133+
return r3.Vector{
134+
X: coordFromCell(cellX, cellWidth, offset.X),
135+
Y: coordFromCell(cellY, cellWidth, offset.Y),
136+
Z: coordFromCell(cellZ, cellWidth, offset.Z),
137+
}
138+
}
139+
140+
// Returns a coordinate from a cell + offset
141+
func coordFromCell(cell, cellWidth int, offset float64) float64 {
142+
return float64(cell*cellWidth-maxCoordInt) + offset
143+
}
144+
121145
// NewEntity creates a new Entity with a given id and ServerClass and returns it.
122146
func NewEntity(id int, serverClass *ServerClass) *Entity {
123147
props := make([]PropertyEntry, len(serverClass.FlattenedProps))

0 commit comments

Comments
 (0)