Files
jak-project/goal_src/engine/collide/collide-mesh-h.gc
T
water111 be74613332 cleanup and bug fix (#1161)
* cleanup and bug fix

* crashing

* fix crash bug

* fix tests
2022-02-13 13:03:30 -05:00

142 lines
4.4 KiB
Common Lisp

;;-*-Lisp-*-
(in-package goal)
;; name: collide-mesh-h.gc
;; name in dgo: collide-mesh-h
;; dgos: GAME, ENGINE
;; The collide-mesh system is used for _moving_ meshes, like a platform.
;; It's not used for the full level (that's collide-frag-mesh)
;;;;;;;;;;;;;;;;;;;;
;; result
;;;;;;;;;;;;;;;;;;;;
;; The triangle involved in collision
;; Note: this is reused for the background collision system.
(deftype collide-tri-result (structure)
((vertex vector 3 :inline :offset-assert 0)
(intersect vector :inline :offset-assert 48)
(normal vector :inline :offset-assert 64)
(pat pat-surface :offset-assert 80)
)
:method-count-assert 9
:size-assert #x54
:flag-assert #x900000054
)
;;;;;;;;;;;;;;;;;;;;
;; static mesh data
;;;;;;;;;;;;;;;;;;;;
;; A triangle, part of a foreground collision mesh.
;; The vertex indices index into the collide-mesh vertex-data array.
;; Due to using uint8's you only get 256 vertices.
(deftype collide-mesh-tri (structure)
((vertex-index uint8 3 :offset-assert 0)
(unused uint8 :offset-assert 3)
(pat pat-surface :offset-assert 4)
)
:pack-me
:method-count-assert 9
:size-assert #x8
:flag-assert #x900000008
)
(declare-type collide-mesh-cache-tri structure)
;; A collision mesh. Note that's it's bound to a specific joint.
(deftype collide-mesh (basic)
((joint-id int32 :offset-assert 4)
(num-tris uint32 :offset-assert 8)
(num-verts uint32 :offset-assert 12)
(vertex-data (inline-array vector) :offset-assert 16)
(tris collide-mesh-tri 1 :inline :offset 32)
)
:method-count-assert 16
:size-assert #x28
:flag-assert #x1000000028
(:methods
(debug-draw-tris (_type_ process-drawable int) none 9)
(overlap-test (_type_ collide-mesh-cache-tri vector) symbol 10)
(should-push-away-test (_type_ collide-mesh-cache-tri collide-tri-result vector float) float 11) ;; spat
(sphere-on-platform-test (_type_ collide-mesh-cache-tri collide-tri-result vector float) float 12) ;; sopt
(populate-cache! (_type_ collide-mesh-cache-tri matrix) none 13)
(collide-mesh-math-1 (_type_ object object) none 14)
(collide-mesh-math-2 (_type_ object object object) none 15)
)
)
;;;;;;;;;;;;;;;;;;;;
;; cache
;;;;;;;;;;;;;;;;;;;;
;; this is not the main collide cache, but instead a smaller cache for unpacking and
;; transforming foreground meshes. It's not useful when filling/probing the real collide-cache,
;; but is useful for the "loop through all the things that aren't me and see if I collide"
(defconstant COLLIDE_MESH_CACHE_SIZE #xa000)
(deftype collide-mesh-cache (basic)
((used-size uint32 :offset-assert 4)
(max-size uint32 :offset-assert 8)
(id uint64 :offset-assert 16)
(data uint8 #xa000 :offset 32)
)
:method-count-assert 12
:size-assert #xa020
:flag-assert #xc0000a020
(:methods
(allocate! (_type_ int) int 9)
(is-id? (_type_ int) symbol 10)
(next-id! (_type_) uint 11)
)
)
(defmethod next-id! collide-mesh-cache ((obj collide-mesh-cache))
"Reset all used entries in the cache and increment the id.
If the id is zero, set it to 1"
;; ld v1, 12(a0)
(let ((v1 (-> obj id)))
;; sw r0, 0(a0)
(set! (-> obj used-size) 0)
;; daddiu v0, v1, 1
(let ((v0 (+ v1 1)))
;; beql v0, r0, L3
;; addiu v0, r0, 1 (only taken if v0 = 0)
(if (= v0 0)
(set! v0 (the uint 1))
)
;; L3:
;; sd v0, 12(a0)
(set! (-> obj id) v0)
v0
)
)
)
(defmethod is-id? collide-mesh-cache ((obj collide-mesh-cache) (arg0 int))
"Is this our id?"
(= (-> obj id) arg0)
)
;; possibly this is stored in the data of the collide-mesh-cache
(deftype collide-mesh-cache-tri (structure)
((vertex vector 3 :inline :offset-assert 0)
(normal vector :inline :offset-assert 48)
(bbox4w bounding-box4w :inline :offset-assert 64)
(pat pat-surface :offset 60)
)
:method-count-assert 9
:size-assert #x60
:flag-assert #x900000060
)
;; only allocate if we don't have an existing one.
(define-perm *collide-mesh-cache* collide-mesh-cache (new 'global 'collide-mesh-cache))
;; in all cases, re-init.
(set! (-> *collide-mesh-cache* id) 1)
(set! (-> *collide-mesh-cache* used-size) 0)
(set! (-> *collide-mesh-cache* max-size) COLLIDE_MESH_CACHE_SIZE)