Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nariakiiwatani/2eb290c538d19d5b94635d57e78f6fdc to your computer and use it in GitHub Desktop.
Save nariakiiwatani/2eb290c538d19d5b94635d57e78f6fdc to your computer and use it in GitHub Desktop.
(defn fix-distance
[base desire distance]
(let [angle (vec2/angle (vec2/- desire base))]
(vec2/rotate base angle (vec2/+ base [distance 0]))))
(defn make-chain
{:params [{:label "src" :type "any"}
{:label "step" :type "number"}
{:overwrap "overwrap" :type "number" :default 0}]
}
[src step overwrap]
(for [base (range 0 (- (count src) (dec step)) (- step overwrap))]
(vec (slice src base (+ base step)))
)
)
(defn ik-bones
{:handles
{:draw
(fn [{:params [root & params]}]
(reduce (fn [a b]
(concat a (let [index (count a)
length (first b)
angle (second b)
lastPos (get (last a) :pos)
newPos (vec2/+ lastPos (vec2/scale [(cos angle) (sin angle)] length))]
[{:id index
:type "point"
:pos newPos}])))
[{:id 0 :type "point" :pos root}] params)
)
:drag
(fn [{:id id :pos p :prev-pos pp :params [root & params]}]
(let [delta (vec2/- p pp)
pos (reduce (fn [a b]
(concat a (let [
length (first b)
angle (second b)
lastPos (last a)
newPos (vec2/+ lastPos (vec2/scale [(cos angle) (sin angle)] length))
]
[newPos]))) [root] params)
parents (reverse (reduce (fn [a b] (let [length (first (nth params(- id (count a))))] (concat a [(fix-distance (last a) b length)]))) [p] (reverse (take id pos))))
children (reduce (fn [a b] (let [length (first (nth params (+ id (dec (count a)))))] (concat a [(fix-distance (last a) b length)]))) [p] (drop (inc id) pos))
moved (concat parents (rest children))
]
(concat [root] (reduce (fn [a b]
(let [index (count a)
from b
to (nth moved (inc index))
sub (vec2/- to from)
]
(concat a [[(vec2/len sub) (vec2/angle sub)]])
))
[] (butlast moved)))
)
)
}}
[root & params]
(let [pos (reduce (fn [a b]
(concat a (let [
length (first b)
angle (second b)
lastPos (last a)
newPos (vec2/+ lastPos (vec2/scale [(cos angle) (sin angle)] length))
]
[newPos]))) [root] params)]
(map (fn [[a b]] (line a b)) (make-chain pos 2 1)))
)
:start-sketch
(style (stroke "salmon" 10)
(ik-bones [0 0] [100.00000000000009 (deg -23.76411514057926)] [80 (deg -167.53793434757057)] [60.00000000000031 (deg 167.08024675208864)] [40.00000000000007 (deg 38.303389912357815)] [20.000000000000288 (deg -1.5189524288203164)])
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment