Skip to content

Commit 3f0762c

Browse files
committed
Add naive position smoothing
1 parent 5311d42 commit 3f0762c

File tree

6 files changed

+59
-12
lines changed

6 files changed

+59
-12
lines changed

client/src/game-client.lisp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
(rot (rotation-of player)))
2222
(run (-> (client :command :player-info
2323
:name name
24+
:timestamp (real-time-seconds)
2425
:position (list (x pos) (y pos))
2526
:rotation (list (x rot) (y rot)))
2627
()))))

client/src/game-server.lisp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,16 @@
1515
(with-slots (players) *connector*
1616
(with-message (name) message
1717
(with-instance-lock-held (*connector*)
18-
(let ((player (make-instance 'player)))
18+
(let ((player (make-instance 'proxy)))
1919
(setf (gethash name players) player)
2020
(post (make-player-added-event player) (events))))))
2121
nil)
2222

2323

2424
(defmethod process-command ((command (eql :player-info)) message)
2525
(with-slots (players) *connector*
26-
(with-message (name position rotation) message
26+
(with-message (name position rotation timestamp) message
2727
(with-instance-lock-held (*connector*)
2828
(when-let ((player (gethash name players)))
29-
(setf (position-of player) (sequence->vec2 position)
30-
(rotation-of player) (sequence->vec2 rotation))))))
29+
(update-proxy player (sequence->vec2 position) (sequence->vec2 rotation) timestamp)))))
3130
nil)

client/src/main.lisp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33

44
(define-constant +framestep+ 0.017)
5-
(define-constant +player-speed+ 20.0)
65
(defvar *main-latch* (mt:make-latch))
76

87

@@ -187,7 +186,10 @@
187186
(with-slots (remote-server game-client game-server) this
188187
(dolist (server (list remote-server game-client game-server))
189188
(when server
190-
(disconnect-from-server server)))))
189+
(disconnect-from-server server)))
190+
(setf remote-server nil
191+
game-client nil
192+
game-server nil)))
191193

192194

193195
(defun start (configuration-path)

client/src/player.lisp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
(in-package :mortar-combat)
22

3+
(define-constant +player-speed+ 20.0)
34

45
(defvar *forward-gaze* (vec3 0.0 0.0 -1.0))
56

67

8+
(defgeneric position-of (player))
9+
(defgeneric rotation-of (player))
10+
11+
712
(defclass player ()
813
((position :initform (vec2)) ; f(x,y) field space = f(x,-z) global space
914
(rotation :initform (vec2) :accessor rotation-of)
@@ -27,12 +32,6 @@
2732
position))
2833

2934

30-
(defmethod (setf position-of) ((value vec2) (this player))
31-
(with-slots (position updated-at) this
32-
(setf position value
33-
updated-at (real-time-seconds))))
34-
35-
3635
(defun gaze-of (player)
3736
"In global coords"
3837
(with-slots (rotation) player

client/src/proxy.lisp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
(in-package :mortar-combat)
2+
3+
4+
(defclass proxy ()
5+
((position :initform (vec2)) ; f(x,y) field space = f(x,-z) global space
6+
(rotation :initform (vec2) :accessor rotation-of)
7+
(updated-at :initform (real-time-seconds))
8+
9+
(next-position :initform (vec2))
10+
(next-rotation :initform (vec2))
11+
(next-at :initform (real-time-seconds))
12+
13+
(correction :initform 0.0)))
14+
15+
16+
(defun proxy-lerp-factor (proxy)
17+
(with-slots (updated-at next-at correction) proxy
18+
(let* ((now (- (real-time-seconds) correction))
19+
(delta (- next-at updated-at)))
20+
(if (= delta 0.0) 1.0 (/ (- now next-at) delta)))))
21+
22+
23+
(defmethod position-of ((this proxy))
24+
(with-slots (position next-position) this
25+
(lerp position next-position (proxy-lerp-factor this))))
26+
27+
28+
(defmethod rotation-of ((this proxy))
29+
(with-slots (rotation next-rotation) this
30+
(lerp rotation next-rotation (proxy-lerp-factor this))))
31+
32+
33+
(defun update-proxy (proxy pos rot timestamp)
34+
(with-slots (next-position next-at position updated-at
35+
correction rotation next-rotation)
36+
proxy
37+
(setf correction (- (real-time-seconds) timestamp)
38+
39+
position (position-of proxy)
40+
rotation (rotation-of proxy)
41+
updated-at next-at
42+
43+
next-position pos
44+
next-rotation rot
45+
next-at timestamp)))

mortar-combat.asd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
(:file "utils")
3131
(:file "keymap")
3232
(:file "player")
33+
(:file "proxy")
3334
(:file "events")
3435
(:file "camera")
3536
(:file "room")

0 commit comments

Comments
 (0)