From bce97445a422a9d4751dfbf64e196e63f71d65e9 Mon Sep 17 00:00:00 2001 From: Paulo Feodrippe Date: Wed, 29 Jan 2025 15:06:51 -0500 Subject: [PATCH] wip --- .github/workflows/ci.yml | 2 +- CHANGELOG.md | 1 + src/vybe/flecs.clj | 15 +++++---- src/vybe/game/system.clj | 68 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index de893ea8..07ac5ece 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,7 +4,7 @@ on: push: branches: - main - - develop + # - develop paths-ignore: - '**/README.md' - '**/CHANGELOG.md' diff --git a/CHANGELOG.md b/CHANGELOG.md index ca6a644f..4ca18e40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - Create `vc/eval*` to debug standalone forms - Accept meta `^:void` in `if`/`cond` expressions to signal to the C compiler that we don't care about the return - Move animation-node-player to VybeC + - CPU went from 84% to 63% - Add `vf/defsystem-c` ## v0.7.469 diff --git a/src/vybe/flecs.clj b/src/vybe/flecs.clj index 48bc136b..11c554d1 100644 --- a/src/vybe/flecs.clj +++ b/src/vybe/flecs.clj @@ -2248,8 +2248,7 @@ :sym k :flags flags})] - (and (vector? v) - #_(some #{:* :_} v)) + (vector? v) (if (vector? k) (->> k ;; Index for the vector destructuring. @@ -2269,8 +2268,10 @@ (remove nil?) vec) [(with-meta [(symbol (str k "--arr")) - ;; TODO support `:maybe` - `(vf.c/ecs-field-id ~it ~idx)] + (if (contains? flags :maybe) + `(when (vf.c/ecs-field-is-set ~it ~idx) + (vf.c/ecs-field-id ~it ~idx)) + `(vf.c/ecs-field-id ~it ~idx))] {:idx idx :type nil :sym k @@ -2279,8 +2280,10 @@ ;; Tag branch. (keyword? v) [(with-meta [(symbol (str k "--arr")) - ;; TODO support `:maybe` - `(vf.c/ecs-field-id ~it ~idx)] + (if (contains? flags :maybe) + `(when (vf.c/ecs-field-is-set ~it ~idx) + (vf.c/ecs-field-id ~it ~idx)) + `(vf.c/ecs-field-id ~it ~idx))] {:idx idx :type nil :sym k diff --git a/src/vybe/game/system.clj b/src/vybe/game/system.clj index e3f9b7ab..0ab47eb6 100644 --- a/src/vybe/game/system.clj +++ b/src/vybe/game/system.clj @@ -156,6 +156,74 @@ (when-not raycast [:vg/raycast :vg/enabled])]}))) +(vf/defsystem-c ^:debug update-physics-2 w + [ ;; TODO Derive it from transform-global. + scale vt/Scale + {aabb-min :min aabb-max :max} vt/Aabb + vy-body [:maybe vj/VyBody] + transform-global [vt/Transform :global] + kinematic [:maybe :vg/kinematic] + dynamic [:maybe :vg/dynamic] + sensor [:maybe :vg/sensor] + ;; Used to find if we are setting `[:vg/raycast :vg/disabled]` + ;; in Blender. + raycast [:maybe {:flags #{:up :self}} + [:vg/raycast :*]] + #_ #_phys [:src (root) vj/PhysicsSystem] + #_ #_e :vf/entity + #_ #_it :vf/iter] + #_(println :e (vf/get-name e) :kin kinematic) + #_(let [half #(max (/ (- (% aabb-max) + (% aabb-min)) + 2.0) + 0.1) + center #(+ (* (/ (+ (% aabb-max) + (% aabb-min)) + 2.0))) + scaled #(* (half %) 2 (scale %)) + {:keys [x y z]} (vm/matrix->translation + (-> (vr.c/matrix-translate (center :x) (center :y) (center :z)) + (vr.c/matrix-multiply transform-global))) + body (if vy-body + (do (when kinematic + #_(println :KINEMATIC (matrix->rotation transform-global)) + (vj/move vy-body (vt/Vector3 [x y z]) (vm/matrix->rotation transform-global) (:delta_time it))) + vy-body) + (let [body (vj/body-add phys (vj/BodyCreationSettings + (cond-> {:position #_(vt/Vector4 [0 0 0 1]) + (vt/Vector4 [x y z 1]) + :rotation #_(vt/Rotation [0 0 0 1]) + (vm/matrix->rotation transform-global) + :shape (vj/box (vj/HalfExtent [(half :x) (half :y) (half :z)]) + scale + #_(vt/Vector4 [x y z 1]) + #_(vt/Translation [0 0 0]) + #_(matrix->rotation transform-global))} + kinematic + (assoc :motion_type (jolt/JPC_MOTION_TYPE_KINEMATIC)) + + sensor + (assoc :is_sensor true) + + dynamic + (assoc :motion_type (jolt/JPC_MOTION_TYPE_DYNAMIC) + :object_layer :vj.layer/moving))))] + (when (= (vf/get-name e) (vf/path [:my/model :vg.gltf/my-cube])) + #_(clojure.pprint/pprint (-> (vj/-body-get phys (:id body)) + :motion_properties + #_(vp/p->map vj/MotionProperties)))) + body)) + {:keys [mesh material]} (when-not vy-body + (gen-cube {:x (scaled :x) :y (scaled :y) :z (scaled :z)} + (rand-int 10)))] + (merge w {(body-path body) + [:vg/debug mesh material phys body + (vt/Eid e)] + + e [phys body + (when-not raycast + [:vg/raycast :vg/enabled])]}))) + (vf/defobserver body-removed w [:vf/events #{:remove} body vj/VyBody