|
4 | 4 | "fmt" |
5 | 5 | "sync" |
6 | 6 |
|
| 7 | + "github.com/golang/geo/r3" |
| 8 | + |
7 | 9 | bit "github.com/markus-wa/demoinfocs-golang/bitread" |
8 | 10 | ) |
9 | 11 |
|
@@ -118,6 +120,28 @@ func (e *Entity) InitializeBaseline(r *bit.BitReader) map[int]PropValue { |
118 | 120 | return baseline |
119 | 121 | } |
120 | 122 |
|
| 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 | + |
121 | 145 | // NewEntity creates a new Entity with a given id and ServerClass and returns it. |
122 | 146 | func NewEntity(id int, serverClass *ServerClass) *Entity { |
123 | 147 | props := make([]PropertyEntry, len(serverClass.FlattenedProps)) |
|
0 commit comments