Skip to content

Instantly share code, notes, and snippets.

@the2bears
Last active August 14, 2016 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save the2bears/ca5bd41e8ac363bbecfd8f054f56b94f to your computer and use it in GitHub Desktop.
Save the2bears/ca5bd41e8ac363bbecfd8f054f56b94f to your computer and use it in GitHub Desktop.
(ns play-clj-splines.core
(:require [play-clj.core :refer :all]
[play-clj.ui :refer :all]
[play-clj.core :refer :all]
[play-clj.ui :refer :all]
[play-clj.g2d-physics :refer :all]
[play-clj.g2d :refer :all]
[play-clj.math :refer [ b-spline b-spline! bezier bezier! catmull-rom-spline catmull-rom-spline! vector-2 vector-2!]]))
(declare add-vector-2 to-vector-2 update-spline)
(def speed 15.0)
(def d-time (/ 1.0 60))
(defn add-vector-2 [p]
(for [point p]
(assoc (shape :filled :set-color (color :yellow) :rect -2.5 -2.5 5 5)
:point? true :x (x point) :y (y point))))
(defn to-vector-2 [p]
(into [] (map (fn[[x y]]
(vector-2 x y)) p)))
(def button-hook
[[100 400][100 400][100 450][150 450][150 350][150 200][150 100][150 100]])
(def pts
[[100 100][100 100][100 400][200 400][200 100][300 100][300 400][300 400]])
(def catmull (catmull-rom-spline (to-vector-2 button-hook) false))
(defscreen main-screen
:on-show
(fn [screen entities]
(update! screen :renderer (stage))
(let [square (shape :filled
:set-color (color :green)
:rect -7.5 -7.5 15 15)
p (to-vector-2 button-hook)]
[(assoc square
:x 100 :y 400 :square? true
:current-time 0 :previous-time 0)
(add-vector-2 p)]))
:on-render
(fn [screen entities]
(clear!)
(->> entities
(update-spline screen)
(render! screen))))
(defgame play-clj-splines-game
:on-create
(fn [this]
(set-screen! this main-screen)))
(defn update-spline [screen entities]
(let [delta-time d-time]
(->> entities
(map (fn [entity]
(cond (:square? entity)
(let [current-time (if (> (:current-time entity) 1)
(- (:current-time entity) 1)
(:current-time entity))
previous-time (:previous-time entity)
v (catmull-rom-spline! catmull :value-at (vector-2 0 0) current-time)
dv (catmull-rom-spline! catmull :derivative-at (vector-2 0 0) current-time);(+ previous-time (* delta-time speed)))
l (vector-2! dv :len)
a (vector-2! dv :angle)
new-delta (/ (* delta-time speed) l)
x (x v)
y (y v)]
(assoc entity :x x :y y :angle a :previous-time current-time :current-time (+ current-time new-delta)))
:else entity)))
)
))
@the2bears
Copy link
Author

Showing smooth path traversal on a catmull-rom spline in play-clj.

@the2bears
Copy link
Author

When rotation is taken into account, the corners look quicker/snappier, but if not speed looks steady. Ugh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment