4b895fc69daf

Mini 2

Add lerp and generalize wrap.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Sat, 09 Apr 2016 23:39:07 +0000
parents 9b5b1ba1336b
children 2f82e9ecb18e
branches/tags (none)
files src/main.lisp src/math.lisp src/particles.lisp

Changes

--- a/src/main.lisp	Sat Apr 09 23:08:04 2016 +0000
+++ b/src/main.lisp	Sat Apr 09 23:39:07 2016 +0000
@@ -33,6 +33,13 @@
      (progn ,@body)
      (pop-matrix)))
 
+(defmacro wrap (place min max)
+  ;; todo: how do i places
+  (with-gensyms (min-val max-val)
+    `(let ((,min-val ,min) (,max-val ,max))
+      (when (< ,place ,min-val) (setf ,place ,max-val))
+      (when (> ,place ,max-val) (setf ,place ,min-val)))))
+
 
 (defun draw-ship (ship angle thrustingp)
   (in-context
@@ -46,12 +53,6 @@
       (ngon 3 0 0 10 10) ; hull
       (ngon 3 6 0 6 3)))) ; cockpit
 
-(defun wrap (position-vec)
-  (when (< (vec-x position-vec) 0) (setf (vec-x position-vec) *width*))
-  (when (> (vec-x position-vec) *width*) (setf (vec-x position-vec) 0))
-  (when (< (vec-y position-vec) 0) (setf (vec-y position-vec) *height*))
-  (when (> (vec-y position-vec) *height*) (setf (vec-y position-vec) 0)))
-
 
 (defsketch cm (:width *width*
                :height *height*
@@ -69,7 +70,8 @@
   (when (zerop (mod frame 20))
     (calc-fps 20))
   (particle-update! ship)
-  (wrap (particle-pos ship))
+  (wrap (particle-x ship) 0 *width*)
+  (wrap (particle-y ship) 0 *height*)
   (when turning-left (decf angle 0.05))
   (when turning-right (incf angle 0.05))
   (when thrusting
--- a/src/math.lisp	Sat Apr 09 23:08:04 2016 +0000
+++ b/src/math.lisp	Sat Apr 09 23:39:07 2016 +0000
@@ -1,11 +1,14 @@
 (in-package #:coding-math)
 
-;;;; Constants
 (defconstant tau (* pi 2))
 
 
-;;;; Maths
 (defun normalize (n min max)
   (/ (- n min)
      (- max min)))
 
+
+(defun lerp (from to n)
+  (+ from
+     (* n (- to from))))
+
--- a/src/particles.lisp	Sat Apr 09 23:08:04 2016 +0000
+++ b/src/particles.lisp	Sat Apr 09 23:39:07 2016 +0000
@@ -20,6 +20,13 @@
   (vec-y (particle-pos particle)))
 
 
+(defun (setf particle-x) (new-value particle)
+  (setf (vec-x (particle-pos particle)) new-value))
+
+(defun (setf particle-y) (new-value particle)
+  (setf (vec-y (particle-pos particle)) new-value))
+
+
 (defun particle-update! (particle)
   (vec-add! (particle-pos particle)
             (particle-vel particle))