Skip to content

Commit 5a23bf9

Browse files
authored
common: add Player.PositionEyes() func + fix Player.Position() docs (#295)
see #236
1 parent 79c99ea commit 5a23bf9

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

pkg/demoinfocs/common/player.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,8 @@ func (p *Player) ViewDirectionY() float32 {
285285
}
286286

287287
// Position returns the in-game coordinates.
288-
// Like the ones you get from cl_showpos 1.
288+
// Note: the Z value is not on the player's eye height but instead at his feet.
289+
// See also PositionEyes().
289290
func (p *Player) Position() r3.Vector {
290291
if p.Entity == nil {
291292
return r3.Vector{}
@@ -294,6 +295,20 @@ func (p *Player) Position() r3.Vector {
294295
return p.Entity.Position()
295296
}
296297

298+
// PositionEyes returns the player's position with the Z value at eye height.
299+
// This is what you get from cl_showpos 1.
300+
// See lso Position().
301+
func (p *Player) PositionEyes() r3.Vector {
302+
if p.Entity == nil {
303+
return r3.Vector{}
304+
}
305+
306+
pos := p.Position()
307+
pos.Z += float64(p.Entity.PropertyValueMust("localdata.m_vecViewOffset[2]").FloatVal)
308+
309+
return pos
310+
}
311+
297312
// Velocity returns the player's velocity.
298313
func (p *Player) Velocity() r3.Vector {
299314
if p.Entity == nil {

pkg/demoinfocs/common/player_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,23 @@ func TestPlayer_Position_EntityNil(t *testing.T) {
353353
assert.Empty(t, pl.Position())
354354
}
355355

356+
func TestPlayer_PositionEyes(t *testing.T) {
357+
entity := entityWithProperty("localdata.m_vecViewOffset[2]", st.PropertyValue{FloatVal: 2})
358+
pos := r3.Vector{X: 1, Y: 2, Z: 3}
359+
360+
entity.On("Position").Return(pos)
361+
362+
pl := &Player{Entity: entity}
363+
364+
assert.Equal(t, r3.Vector{X: 1, Y: 2, Z: 5}, pl.PositionEyes())
365+
}
366+
367+
func TestPlayer_PositionEyes_EntityNil(t *testing.T) {
368+
pl := new(Player)
369+
370+
assert.Empty(t, pl.PositionEyes())
371+
}
372+
356373
func TestPlayer_Velocity(t *testing.T) {
357374
entity := new(stfake.Entity)
358375
entity.On("PropertyValueMust", "localdata.m_vecVelocity[0]").Return(st.PropertyValue{FloatVal: 1})

0 commit comments

Comments
 (0)