Skip to content

Commit

Permalink
Merge pull request #232 from kaveh808/kaveh-devel-7
Browse files Browse the repository at this point in the history
Added spirograph with spring-mass
  • Loading branch information
kaveh808 authored Jul 26, 2023
2 parents 12018ec + bdc7289 commit db8dde9
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/plugins/parametric-curve.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,61 @@ NOTE: This won't work with the existing procedural mixin set up, because
(add-shape scene curve)
(add-motion scene anim)
curve))

;;; spirograph setup with spring and mass attached to arm

(defun make-spirograph-spring (scene ring-radius gear-radius arm-length gear-inside-ring?
rotation-offset rotation-increment
spring-rest-length spring-end-mass spring-stiffness
&key (show-assembly? nil) (color nil)
(do-collisions? nil) (force-fields '()))
(let* ((gear-rotation-step (* (/ ring-radius gear-radius)
rotation-increment
(if gear-inside-ring? -1.0 1.0)))
(gear-offset (if gear-inside-ring?
(- ring-radius gear-radius)
(+ ring-radius gear-radius)))
(ring (make-circle (* 2 ring-radius) 64))
(gear (make-circle (* 2 gear-radius) 16))
(pen-location (p! 0 arm-length 0))
(arm (make-line-curve (p! 0 0 0) pen-location 1))
(gear-assembly (translate-by (make-shape-group (list gear arm))
(p! 0 gear-offset 0)))
(top-assembly (make-shape-group (list gear-assembly)))
;; spring
(spring (make-line-curve (p! 0 (+ gear-offset arm-length) 0)
(p! 0 (+ gear-offset arm-length spring-rest-length) 0)
1))
(flex-anim (make-flex-animator spring))
;; generated curve with point colors
(curve (allocate-point-colors (make-instance 'curve :is-closed-curve? nil)))
(anim (make-instance
'animator
:setup-fn (lambda ()
(rotate-by top-assembly (p! 0 0 rotation-offset)))
:update-fn (lambda ()
(rotate-by top-assembly (p! 0 0 rotation-increment))
(rotate-by gear-assembly (p! 0 0 gear-rotation-step))
(let ((loc (shape-global-point scene gear pen-location)))
(p-set! (aref (points spring) 0) (p:x loc) (p:y loc) (p:z loc)))
(append-point curve
(aref (points spring) 1)
color)))))
;; spring
(set-point-colors spring (c! 1 0 0))
(setf (pinned? (aref (vertices flex-anim) 0)) t)
(set-flex-vertex-attr flex-anim 'do-collisions? do-collisions?)
(set-flex-vertex-attr flex-anim 'mass spring-end-mass)
(set-flex-spring-attr flex-anim 'stiffness spring-stiffness)
(set-flex-spring-attr flex-anim 'rest-length spring-rest-length)
(setf (force-fields flex-anim) force-fields)
(add-shape scene spring)
(add-motion scene flex-anim)
;; spirograph
(add-shape scene top-assembly)
(add-shape scene ring)
(setf (is-visible? top-assembly) show-assembly?)
(setf (is-visible? ring) show-assembly?)
(add-shape scene curve)
(add-motion scene anim)
curve))
39 changes: 39 additions & 0 deletions test/demo-parametric-curve.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,45 @@ Make sure you have opened the graphics window by doing:
;;; run complete cycle
(update-scene *scene* 1264)

;;; spirograph with spring -----------------------------------------------------

;;; spring
(with-clear-scene
(setf (end-frame *scene*) 10000) ;arbitrarily long scene duration
(make-spirograph-spring *scene* 2.5 0.7 0.6 t 0.0 2.0 2.0 1.0 0.5
:show-assembly? t :color (c! 0 0 1)
:do-collisions? nil
:force-fields '()))
;;; hold down space key in 3D view to run animation

;;; run simulation
(update-scene *scene* 500)

;;; spring, force-field
(with-clear-scene
(setf (end-frame *scene*) 10000) ;arbitrarily long scene duration
(make-spirograph-spring *scene* 2.5 0.7 0.6 t 0.0 2.0 2.0 1.0 0.5
:show-assembly? t :color (c! 0 0 1)
:do-collisions? nil
:force-fields (list (make-instance 'constant-force-field
:force-vector (p! .1 0 0)))))
;;; hold down space key in 3D view to run animation

;;; run simulation
(update-scene *scene* 500)

;;; spring, force-field, ground collision
(with-clear-scene
(setf (end-frame *scene*) 10000) ;arbitrarily long scene duration
(make-spirograph-spring *scene* 2.5 0.7 0.6 t 0.0 2.0 2.0 1.0 0.5
:show-assembly? t :color (c! 0 0 1)
:do-collisions? t
:force-fields (list (make-instance 'constant-force-field
:force-vector (p! .1 0 0)))))
;;; hold down space key in 3D view to run animation

;;; run simulation
(update-scene *scene* 500)

;;;; END ========================================================================

0 comments on commit db8dde9

Please sign in to comment.