Skip to content

Commit

Permalink
Add dynamic-entity-p predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinmera committed Nov 27, 2024
1 parent 23b4103 commit 5544659
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
3 changes: 2 additions & 1 deletion package.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,8 @@
#:impart-collision-impact
#:velocity-eps
#:depth-eps
#:accelerated-rigidbody-system)
#:accelerated-rigidbody-system
#:dynamic-entity-p)
;; physics/rigidbody.lisp
(:export
#:rigid-shape
Expand Down
22 changes: 12 additions & 10 deletions physics/resolution.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -424,25 +424,28 @@
(global-primitives :initform (make-array 128 :fill-pointer 0) :accessor global-primitives)
(pending-inserts :initform (make-array 128 :fill-pointer 0) :accessor pending-inserts)))

(defmethod dynamic-entity-p ((body rigid-shape))
(< 0 (the single-float (inverse-mass body))))

(defmethod enter :before ((body rigid-shape) (system accelerated-rigidbody-system))
(cond ((= 0 (length (physics-primitives body)))
(unless (find body (pending-inserts system))
(vector-push-extend body (pending-inserts system))))
(T
(start-frame body)
(loop with structure = (if (= 0 (inverse-mass body))
(static-acceleration-structure system)
(dynamic-acceleration-structure system))
(loop with structure = (if (dynamic-entity-p body)
(dynamic-acceleration-structure system)
(static-acceleration-structure system))
for primitive across (physics-primitives body)
do (if (typep primitive 'all-space)
(vector-push-extend primitive (global-primitives system))
(3ds:enter primitive structure))))))

(defmethod leave :after ((body rigid-shape) (system accelerated-rigidbody-system))
(unless (array-utils:vector-pop-element* (pending-inserts system) body)
(loop with structure = (if (= 0 (inverse-mass body))
(static-acceleration-structure system)
(dynamic-acceleration-structure system))
(loop with structure = (if (dynamic-entity-p body)
(dynamic-acceleration-structure system)
(static-acceleration-structure system))
for primitive across (physics-primitives body)
do (if (typep primitive 'all-space)
(array-utils:vector-pop-element* (global-primitives system) primitive)
Expand All @@ -452,7 +455,7 @@
(declare (optimize speed))
(loop with structure = (dynamic-acceleration-structure system)
for object across (%objects system)
do (when (< 0 (the single-float (inverse-mass object)))
do (when (dynamic-entity-p object)
(loop for primitive across (physics-primitives object)
do (unless (typep primitive 'all-space)
(3ds:update primitive structure))))))
Expand Down Expand Up @@ -482,8 +485,7 @@
(return-from generate-hits start))))
(loop with structure = (static-acceleration-structure system)
for object across (%objects system)
do (when (and (< 0 (the single-float (inverse-mass object)))
(awake-p object))
do (when (and (dynamic-entity-p object) (awake-p object))
(loop for a across (physics-primitives object)
do (3ds:do-overlapping (b structure a)
(when (< 0 (logand (collision-mask a) (collision-mask b)
Expand Down Expand Up @@ -511,7 +513,7 @@
(static-changed-p NIL))
(loop while (< 0 (length pending-inserts))
for body = (vector-pop pending-inserts)
for structure = (if (= 0 (inverse-mass body)) static dynamic)
for structure = (if (dynamic-entity-p body) dynamic static)
do (start-frame body)
(when (eq structure static) (setf static-changed-p T))
(loop for primitive across (physics-primitives body)
Expand Down

0 comments on commit 5544659

Please sign in to comment.