Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add naive position smoothing
  • Loading branch information
borodust committed Apr 22, 2017
1 parent 5311d42 commit 3f0762c
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 12 deletions.
1 change: 1 addition & 0 deletions client/src/game-client.lisp
Expand Up @@ -21,6 +21,7 @@
(rot (rotation-of player)))
(run (-> (client :command :player-info
:name name
:timestamp (real-time-seconds)
:position (list (x pos) (y pos))
:rotation (list (x rot) (y rot)))
()))))
7 changes: 3 additions & 4 deletions client/src/game-server.lisp
Expand Up @@ -15,17 +15,16 @@
(with-slots (players) *connector*
(with-message (name) message
(with-instance-lock-held (*connector*)
(let ((player (make-instance 'player)))
(let ((player (make-instance 'proxy)))
(setf (gethash name players) player)
(post (make-player-added-event player) (events))))))
nil)


(defmethod process-command ((command (eql :player-info)) message)
(with-slots (players) *connector*
(with-message (name position rotation) message
(with-message (name position rotation timestamp) message
(with-instance-lock-held (*connector*)
(when-let ((player (gethash name players)))
(setf (position-of player) (sequence->vec2 position)
(rotation-of player) (sequence->vec2 rotation))))))
(update-proxy player (sequence->vec2 position) (sequence->vec2 rotation) timestamp)))))
nil)
6 changes: 4 additions & 2 deletions client/src/main.lisp
Expand Up @@ -2,7 +2,6 @@


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


Expand Down Expand Up @@ -187,7 +186,10 @@
(with-slots (remote-server game-client game-server) this
(dolist (server (list remote-server game-client game-server))
(when server
(disconnect-from-server server)))))
(disconnect-from-server server)))
(setf remote-server nil
game-client nil
game-server nil)))


(defun start (configuration-path)
Expand Down
11 changes: 5 additions & 6 deletions client/src/player.lisp
@@ -1,9 +1,14 @@
(in-package :mortar-combat)

(define-constant +player-speed+ 20.0)

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


(defgeneric position-of (player))
(defgeneric rotation-of (player))


(defclass player ()
((position :initform (vec2)) ; f(x,y) field space = f(x,-z) global space
(rotation :initform (vec2) :accessor rotation-of)
Expand All @@ -27,12 +32,6 @@
position))


(defmethod (setf position-of) ((value vec2) (this player))
(with-slots (position updated-at) this
(setf position value
updated-at (real-time-seconds))))


(defun gaze-of (player)
"In global coords"
(with-slots (rotation) player
Expand Down
45 changes: 45 additions & 0 deletions client/src/proxy.lisp
@@ -0,0 +1,45 @@
(in-package :mortar-combat)


(defclass proxy ()
((position :initform (vec2)) ; f(x,y) field space = f(x,-z) global space
(rotation :initform (vec2) :accessor rotation-of)
(updated-at :initform (real-time-seconds))

(next-position :initform (vec2))
(next-rotation :initform (vec2))
(next-at :initform (real-time-seconds))

(correction :initform 0.0)))


(defun proxy-lerp-factor (proxy)
(with-slots (updated-at next-at correction) proxy
(let* ((now (- (real-time-seconds) correction))
(delta (- next-at updated-at)))
(if (= delta 0.0) 1.0 (/ (- now next-at) delta)))))


(defmethod position-of ((this proxy))
(with-slots (position next-position) this
(lerp position next-position (proxy-lerp-factor this))))


(defmethod rotation-of ((this proxy))
(with-slots (rotation next-rotation) this
(lerp rotation next-rotation (proxy-lerp-factor this))))


(defun update-proxy (proxy pos rot timestamp)
(with-slots (next-position next-at position updated-at
correction rotation next-rotation)
proxy
(setf correction (- (real-time-seconds) timestamp)

position (position-of proxy)
rotation (rotation-of proxy)
updated-at next-at

next-position pos
next-rotation rot
next-at timestamp)))
1 change: 1 addition & 0 deletions mortar-combat.asd
Expand Up @@ -30,6 +30,7 @@
(:file "utils")
(:file "keymap")
(:file "player")
(:file "proxy")
(:file "events")
(:file "camera")
(:file "room")
Expand Down

0 comments on commit 3f0762c

Please sign in to comment.