# HG changeset patch # User Steve Losh # Date 1460245147 0 # Node ID 4b895fc69dafbe9d0589fa9a0895320513768fb4 # Parent 9b5b1ba1336b0561c38c448fa2d3065fe6588820 Mini 2 Add lerp and generalize wrap. diff -r 9b5b1ba1336b -r 4b895fc69daf src/main.lisp --- 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 diff -r 9b5b1ba1336b -r 4b895fc69daf src/math.lisp --- 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)))) + diff -r 9b5b1ba1336b -r 4b895fc69daf src/particles.lisp --- 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))