summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson@vistahigherlearning.com>2020-12-16 07:54:21 -0500
committerDavid Thompson <dthompson@vistahigherlearning.com>2020-12-16 07:54:21 -0500
commitd8bf901c7647d9e4e458801c20a429666dee209e (patch)
treed6d09dff095a2378d806b9cdc0ee069d7e31079c
parent8ffb4e17e9912f25b9b79c497fb1f64a1faa205c (diff)
node-2d: Fix camera target tracking.
-rw-r--r--starling/node-2d.scm54
1 files changed, 21 insertions, 33 deletions
diff --git a/starling/node-2d.scm b/starling/node-2d.scm
index f7a40df..1559a54 100644
--- a/starling/node-2d.scm
+++ b/starling/node-2d.scm
@@ -134,9 +134,8 @@
(define-class <camera-2d> ()
(target #:accessor target #:init-form #f #:init-keyword #:target)
- (offset #:getter offset #:init-form (vec2 0.0 0.0) #:init-keyword #:offset)
+ (offset #:accessor offset #:init-form (vec2 0.0 0.0) #:init-keyword #:offset)
(position #:getter position #:init-form (vec2 0.0 0.0))
- (last-position #:getter last-position #:init-form (vec2 0.0 0.0))
(resolution #:getter resolution #:init-keyword #:resolution
#:init-thunk default-resolution)
(viewport #:getter viewport #:init-keyword #:viewport
@@ -155,31 +154,25 @@
;; This method can be overridden by subclasses to create custom camera
;; movement.
-(define-method (follow-target (camera <camera-2d>) dt)
- (let ((pos (position camera))
- (target-pos (position (target camera)))
- (offset (offset camera)))
- (set-vec2-x! pos (- (vec2-x offset) (vec2-x target-pos)))
- (set-vec2-y! pos (- (vec2-y offset) (vec2-y target-pos)))))
-
-(define-method (update (camera <camera-2d>) dt)
- (when (target camera)
- (let ((pos (position camera))
- (last-pos (last-position camera))
- (m (view-matrix camera)))
- (vec2-copy! pos last-pos)
- (follow-target camera dt)
- (unless (and (= (vec2-x pos) (vec2-x last-pos))
- (= (vec2-y pos) (vec2-y last-pos)))
- (matrix4-translate! m pos)
- (matrix4-mult! m m (projection-matrix camera))))))
+(define-method (follow-target (camera <camera-2d>))
+ (let ((p (position camera)))
+ (set-vec2! p 0.0 0.0)
+ (transform! (world-matrix (target camera)) p)
+ (vec2-mult! p -1.0)
+ (vec2-add! p (offset camera))))
(define-syntax-rule (with-camera camera body ...)
- (with-viewport (viewport camera)
- (with-projection (if (target camera)
- (view-matrix camera)
- (projection-matrix camera))
- body ...)))
+ (begin
+ (when (target camera)
+ (follow-target camera)
+ (let ((m (view-matrix camera)))
+ (matrix4-translate! m (position camera))
+ (matrix4-mult! m m (projection-matrix camera))))
+ (with-viewport (viewport camera)
+ (with-projection (if (target camera)
+ (view-matrix camera)
+ (projection-matrix camera))
+ body ...))))
;;;
@@ -193,19 +186,14 @@
(cameras #:accessor cameras #:init-form (list (make <camera-2d>))
#:init-keyword #:cameras))
-(define-method (update (canvas <canvas>) dt)
- (for-each (lambda (camera)
- (update camera dt))
- (cameras canvas)))
-
(define-method (render-tree (canvas <canvas>) alpha)
(render canvas alpha)
;; Draw scene from the viewpoint of each camera.
(for-each (lambda (camera)
(with-camera camera
- (for-each-child (lambda (child)
- (render-tree child alpha))
- canvas)))
+ (for-each-child (lambda (child)
+ (render-tree child alpha))
+ canvas)))
(cameras canvas)))