Skip to content

Commit

Permalink
Calculate camera position together with players.
Browse files Browse the repository at this point in the history
Keep the same lock during the calculation, so that the camera position
is consistent with player positions. Otherwise, they might not match
during one frame.
  • Loading branch information
dmitshur committed May 24, 2018
1 parent d2dabad commit 1236d90
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions eX0-go/camera.go
Expand Up @@ -39,19 +39,19 @@ type PlayerCamera struct {
pos playerPosVel
}

// CalculateForFrame calculates camera position for frame at gameMoment.
// c.logic.playersStateMu must be held.
func (c *PlayerCamera) CalculateForFrame(gameMoment gameMoment) {
c.logic.playersStateMu.Lock()
ps := c.logic.playersState[c.playerID]
if (ps.conn != nil && ps.conn.JoinStatus < IN_GAME) || ps.Team == packet.Spectator {
c.logic.playersStateMu.Unlock()
return
}
// TODO: Consider using same position as calculated for all players
// during "Calculate player positions for this frame" step,
// instead of our own copy (which might not match).
// This is better now that we're using same gameMoment.
// This is better now that we're using same gameMoment
// and locking playersStateMu outside.
c.pos = ps.InterpolatedOrDead(gameMoment, c.playerID)
c.logic.playersStateMu.Unlock()
}

func (c *PlayerCamera) ModelView() mgl32.Mat4 {
Expand Down
4 changes: 2 additions & 2 deletions eX0-go/view.go
Expand Up @@ -147,10 +147,11 @@ func (v *view) initAndMainLoop() {

gl.Clear(gl.COLOR_BUFFER_BIT)

// Calculate player positions for this frame.
// Calculate camera and player positions for this frame.
var players []visiblePlayer
state.Lock()
v.logic.playersStateMu.Lock()
v.cameras[v.activeCamera].CalculateForFrame(gameMoment)
for id, ps := range v.logic.playersState {
if ps.conn != nil && ps.conn.JoinStatus < IN_GAME { // TODO: Fix JoinStatus race with line server.go:719.
continue
Expand All @@ -170,7 +171,6 @@ func (v *view) initAndMainLoop() {
v.logic.playersStateMu.Unlock()
state.Unlock()

v.cameras[v.activeCamera].CalculateForFrame(gameMoment)
pMatrix := mgl32.Ortho2D(0, float32(v.windowSize[0]), 0, float32(v.windowSize[1]))
mvMatrix := v.cameras[v.activeCamera].ModelView()

Expand Down

0 comments on commit 1236d90

Please sign in to comment.