Skip to content

Commit

Permalink
Added time-varying-force-field
Browse files Browse the repository at this point in the history
  • Loading branch information
kaveh808 committed Aug 3, 2023
1 parent 35f7293 commit b1048e1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/kernel/noise.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@
(* y1 (+ (* x0 p012) (* x1 p112) (* x2 p212)))
(* y2 (+ (* x0 p022) (* x1 p122) (* x2 p222))))))))

(defun float-noise (v)
(noise (p! v 0 0)))

(defun turbulence (p n-octaves)
(let ((sum 0.0)
(scale 1.0))
Expand All @@ -108,6 +111,9 @@
(dz (- (noise (p! x y (+ z delta))) (noise (p! x y (- z delta))))))
(p! dx dy dz)))

(defun float-noise-gradient (v &optional (delta 0.01))
(noise-gradient (p! v 0 0) delta))

(defun color-noise (p &optional (delta 0.01))
(let ((pn (p:normalize (noise-gradient p delta))))
(c! (abs (p:x pn)) (abs (p:y pn)) (abs (p:z pn)))))
Expand Down
20 changes: 16 additions & 4 deletions src/plugins/force-field.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
;;;; constant-force-field ======================================================

(defclass constant-force-field (force-field)
((force-vector :accessor force-vector :initarg :force-vector :initform (p! 0 -9.81 0))))
((force-vector :accessor force-vector :initarg :force-vector :initform (p! 0 0 0))))

(defmethod field-value ((field constant-force-field) point time)
(declare (ignore point time))
Expand All @@ -33,13 +33,25 @@
(p! 0 0 0)
(p:scale dir (/ (magnitude field) (* dist dist))))))

;;;; noise-force-field =========================================================
;;;; time-varying-force-field ==================================================

(defclass noise-force-field (force-field)
(defclass time-varying-force-field (constant-force-field)
((noise-frequency :accessor noise-frequency :initarg :noise-frequency :initform 1.0)
(noise-amplitude :accessor noise-amplitude :initarg :noise-amplitude :initform 1.0)))

(defmethod field-value ((field noise-force-field) point time)
(defmethod field-value ((field time-varying-force-field) point time)
(declare (ignore point))
(p+ (force-vector field)
(p:scale (float-noise-gradient (* time (noise-frequency field)))
(noise-amplitude field))))

;;;; 3d-noise-force-field =========================================================

(defclass 3d-noise-force-field (force-field)
((noise-frequency :accessor noise-frequency :initarg :noise-frequency :initform 1.0)
(noise-amplitude :accessor noise-amplitude :initarg :noise-amplitude :initform 1.0)))

(defmethod field-value ((field 3d-noise-force-field) point time)
(declare (ignore time))
(p:scale (noise-gradient (p:scale point (noise-frequency field)))
(noise-amplitude field)))
13 changes: 9 additions & 4 deletions test/demo-parametric-curve.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,10 @@ Make sure you have opened the graphics window by doing:
(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)))))
:force-fields (list (make-instance 'time-varying-force-field
:force-vector (p! 0 0 0)
:noise-frequency 10.0
:noise-amplitude 8.0))))
;;; hold down space key in 3D view to run animation

;;; run simulation
Expand All @@ -168,8 +170,11 @@ Make sure you have opened the graphics window by doing:
(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)))))
:force-fields (list (make-instance 'time-varying-force-field
:force-vector (p! 0 0 0)
:noise-frequency 10.0
:noise-amplitude 8.0))))

;;; hold down space key in 3D view to run animation

;;; run simulation
Expand Down
32 changes: 31 additions & 1 deletion test/demo-particle.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ force field.
'dynamic-particle
:life-span -1 ;infinite life-span
:do-collisions? nil
:force-fields (list (make-instance 'noise-force-field
:force-fields (list (make-instance '3d-noise-force-field
:noise-frequency 0.2
:noise-amplitude 0.2)))))
(add-shape *scene* shape)
Expand Down Expand Up @@ -348,6 +348,36 @@ Create a row particle systems with varrying attributes.
(update-scene *scene* 30) ;do update for batch testing


#|
(Demo 14 particle) dynamic particle system =====================================
Dynamic particles growing from a superquadric with a time-varying force field.
|#

(format t " particle-system 14...~%") (finish-output)

(with-clear-scene
(let ((p-source (freeze-transform (make-superquadric 16 8 2.0 1.0 1.0))))
(setf (point-source-use-face-centers? p-source) t) ;comment out to use poly points
(let ((p-sys (make-particle-system-from-point-source
p-source
(lambda (v) (p:scale v 0.2))
'dynamic-particle
:life-span -1 ;infinite life-span
:do-collisions? nil
:force-fields (list (make-instance 'time-varying-force-field
:force-vector (p! 0 -0.0 0)
:noise-frequency 100
:noise-amplitude 4.0)))))
(add-shape *scene* p-source)
(add-shape *scene* p-sys)
(add-motion *scene* p-sys))))
;;; hold down space key in 3D view to run animation

;;; for automated testing
(update-scene *scene* 30)


#|
END ============================================================================
|#

0 comments on commit b1048e1

Please sign in to comment.