78529b68fe97

Mini 4: Clamp
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Sun, 17 Apr 2016 21:57:14 +0000
parents dcf7b53a54df
children 540972fdbaab
branches/tags (none)
files package.lisp src/main.lisp src/math.lisp

Changes

--- a/package.lisp	Sun Apr 17 21:16:39 2016 +0000
+++ b/package.lisp	Sun Apr 17 21:57:14 2016 +0000
@@ -1,6 +1,7 @@
 (defpackage #:coding-math.utils
   (:use #:cl #:coding-math.quickutils)
   (:export
+    #:mulf
     #:dividesp))
 
 (defpackage #:coding-math
--- a/src/main.lisp	Sun Apr 17 21:16:39 2016 +0000
+++ b/src/main.lisp	Sun Apr 17 21:57:14 2016 +0000
@@ -64,45 +64,45 @@
     ((mx 0)
      (my 0)
      (frame 1)
-     (particle (make-particle center-x *height*
-                              :gravity 0.025
-                              :speed (random-range 4.0 6.0)
-                              :direction (random tau)
-                              :radius 5))
+     (rect-w 300)
+     (rect-h 200)
+     (rect-x (- center-x (/ 300 2)))
+     (rect-y (- center-y (/ 200 2)))
+     (cx 0)
+     (cy 0)
+     (cr 10)
      (bounce -0.7))
   (background (gray 1))
   (incf frame)
   ;;
+  (with-pen (make-pen :stroke (gray 0.3) :fill (gray 0.8))
+    (rect (- rect-x cr)
+          (- rect-y cr)
+          (+ rect-w cr cr)
+          (+ rect-h cr cr)))
   (with-pen (make-pen :stroke (gray 0) :fill (gray 0.5))
-    (draw-particle particle)
-    (mulf (vec-magnitude (particle-vel particle)) 0.998)
-    (particle-update! particle)
-    (let ((r (particle-radius particle)))
-      (when (> (+ (particle-x particle) r) *width*)
-        (setf (particle-x particle) (- *width* r))
-        (mulf (vec-x (particle-vel particle)) bounce))
-      (when (< (- (particle-x particle) r) 0)
-        (setf (particle-x particle) r)
-        (mulf (vec-x (particle-vel particle)) bounce))
-      (when (> (+ (particle-y particle) r) *height*)
-        (setf (particle-y particle) (- *height* r))
-        (mulf (vec-y (particle-vel particle)) bounce))
-      (when (< (- (particle-y particle) r) 0)
-        (setf (particle-y particle) r)
-        (mulf (vec-y (particle-vel particle)) bounce))))
+    (circle cx cy cr))
   ;;
   (when (zerop (mod frame 20))
     (calc-fps 20))
-  (draw-fps)
-  )
+  (draw-fps))
 
 
 ;;;; Mouse
 (defmethod kit.sdl2:mousemotion-event ((window cm) ts b x y xrel yrel)
   (declare (ignore ts b xrel yrel))
-  (with-slots (mx my) window
+  (with-slots (mx my rect-x rect-y rect-w rect-h cx cy cr) window
     (setf mx x)
-    (setf my y)))
+    (setf my y)
+    ;;
+    (setf cx (clamp rect-x
+                    (+ rect-x rect-w)
+                    x))
+    (setf cy (clamp rect-y
+                    (+ rect-y rect-h)
+                    y))
+    ;;
+    ))
 
 
 ;;;; Keyboard
--- a/src/math.lisp	Sun Apr 17 21:16:39 2016 +0000
+++ b/src/math.lisp	Sun Apr 17 21:57:14 2016 +0000
@@ -43,6 +43,12 @@
   (lerp dest-from dest-to
         (normalize source-from source-to source-val)))
 
+(defun clamp (min max n)
+  (cond
+    ((> n max) max)
+    ((< n min) min)
+    (t n)))
+
 
 ;; Wrapping
 (defun wrap-zero (max val)