From 4dd1ac35a9561e2da204575485edac0709f4dd20 Mon Sep 17 00:00:00 2001 From: Rasmus Madsen Date: Mon, 4 Nov 2024 11:04:23 +0100 Subject: [PATCH 1/5] exported spatial hashing --- Chipmunk2D | 1 - pymunk/space.py | 5 +++++ pymunk_cffi/chipmunk_cdef.h | 6 ++++++ 3 files changed, 11 insertions(+), 1 deletion(-) delete mode 160000 Chipmunk2D diff --git a/Chipmunk2D b/Chipmunk2D deleted file mode 160000 index 7f091aad..00000000 --- a/Chipmunk2D +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 7f091aadb1b9e6c0e0a1705c1ed71c1e375eb64d diff --git a/pymunk/space.py b/pymunk/space.py index a59df1d8..97c7df56 100644 --- a/pymunk/space.py +++ b/pymunk/space.py @@ -554,6 +554,11 @@ def use_spatial_hash(self, dim: float, count: int) -> None: """ cp.cpSpaceUseSpatialHash(self._space, dim, count) + def get_space_hash_objects_per_cell(self) -> List: + cell_counts = ffi.new(f'int[{cp.cpSpaceGetSpatialHashTotalCells(self._space)}]') + cp.cpSpaceGetSpatialHashObjectCounts(self._space, cell_counts) + return cell_counts + def step(self, dt: float) -> None: """Update the space for the given time step. diff --git a/pymunk_cffi/chipmunk_cdef.h b/pymunk_cffi/chipmunk_cdef.h index 3e00ecd0..76b420d6 100644 --- a/pymunk_cffi/chipmunk_cdef.h +++ b/pymunk_cffi/chipmunk_cdef.h @@ -1233,6 +1233,12 @@ void cpSpaceReindexShapesForBody(cpSpace *space, cpBody *body); /// Switch the space to use a spatial has as it's spatial index. void cpSpaceUseSpatialHash(cpSpace *space, cpFloat dim, int count); +// MARK: P7 (Crowd Crushing) + +int cpSpaceGetSpatialHashTotalCells(cpSpace * space); + +void cpSpaceGetSpatialHashObjectCounts(cpSpace * space, int *cell_counts); + // MARK: Time Stepping /// Step the space forward in time by @c dt. From 1c4393decefee67b8641823978795d861414b2ac Mon Sep 17 00:00:00 2001 From: Rasmus Madsen Date: Mon, 4 Nov 2024 11:06:38 +0100 Subject: [PATCH 2/5] Added submodule --- .gitmodules | 2 +- Chipmunk2D | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) create mode 160000 Chipmunk2D diff --git a/.gitmodules b/.gitmodules index 4d311954..193471cc 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "Chipmunk2D"] path = Chipmunk2D - url = git@github.com:viblo/Chipmunk2D.git + url = https://github.com/imMadsen/Chipmunk2D diff --git a/Chipmunk2D b/Chipmunk2D new file mode 160000 index 00000000..433c7036 --- /dev/null +++ b/Chipmunk2D @@ -0,0 +1 @@ +Subproject commit 433c70361b8cb656ac0c918e5092fa6964987aa2 From 49017ac5d8b2a6fd0448462b0b989d348c4e8daf Mon Sep 17 00:00:00 2001 From: Rasmus Madsen <76999316+imMadsen@users.noreply.github.com> Date: Wed, 6 Nov 2024 09:16:41 +0100 Subject: [PATCH 3/5] Rewrote Spatial Hashing --- pymunk/space.py | 16 ++++++++++++---- pymunk_cffi/chipmunk_cdef.h | 6 +++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/pymunk/space.py b/pymunk/space.py index 97c7df56..02d7bdcd 100644 --- a/pymunk/space.py +++ b/pymunk/space.py @@ -554,11 +554,19 @@ def use_spatial_hash(self, dim: float, count: int) -> None: """ cp.cpSpaceUseSpatialHash(self._space, dim, count) - def get_space_hash_objects_per_cell(self) -> List: - cell_counts = ffi.new(f'int[{cp.cpSpaceGetSpatialHashTotalCells(self._space)}]') - cp.cpSpaceGetSpatialHashObjectCounts(self._space, cell_counts) - return cell_counts + def get_space_hash_indicies(self, hash_indicies_len, l, r, b, t) -> List: + hash_indicies = ffi.new(f'int[{hash_indicies_len}]') + cp.cpSpaceGetSpatialHashIndices(self._space, hash_indicies, l, r, b, t) + return hash_indicies + + def create_space_hash_objects_count(self, hash_indicies_len) -> List: + hash_indicies = ffi.new(f'int[{hash_indicies_len}]') + return hash_indicies + + def get_space_hash_objects_count(self, cell_counts, hash_indicies, hash_indicies_len) -> None: + cp.cpSpaceGetSpatialHashObjectCounts(self._space, cell_counts, hash_indicies, hash_indicies_len) + def step(self, dt: float) -> None: """Update the space for the given time step. diff --git a/pymunk_cffi/chipmunk_cdef.h b/pymunk_cffi/chipmunk_cdef.h index 76b420d6..8a04194c 100644 --- a/pymunk_cffi/chipmunk_cdef.h +++ b/pymunk_cffi/chipmunk_cdef.h @@ -1233,11 +1233,11 @@ void cpSpaceReindexShapesForBody(cpSpace *space, cpBody *body); /// Switch the space to use a spatial has as it's spatial index. void cpSpaceUseSpatialHash(cpSpace *space, cpFloat dim, int count); -// MARK: P7 (Crowd Crushing) +//MARK: P7 (Crowd Crushing) -int cpSpaceGetSpatialHashTotalCells(cpSpace * space); +void cpSpaceGetSpatialHashIndices(cpSpace *space, int *hash_indices, int l, int r, int b, int t); -void cpSpaceGetSpatialHashObjectCounts(cpSpace * space, int *cell_counts); +void cpSpaceGetSpatialHashObjectCounts(cpSpace *space, int *cell_counts, int *hash_indices, int hash_indices_len); // MARK: Time Stepping From ab63a2b1bff29388736446c7a469746e5c25c5af Mon Sep 17 00:00:00 2001 From: Rasmus Madsen <76999316+imMadsen@users.noreply.github.com> Date: Wed, 6 Nov 2024 09:40:03 +0100 Subject: [PATCH 4/5] a --- Chipmunk2D | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Chipmunk2D b/Chipmunk2D index 433c7036..ec227c3f 160000 --- a/Chipmunk2D +++ b/Chipmunk2D @@ -1 +1 @@ -Subproject commit 433c70361b8cb656ac0c918e5092fa6964987aa2 +Subproject commit ec227c3fc1995e9936ffaf4d8e612048fdbe4f24 From eec530da5dba97ba3dfcaf94541f06c455bf6f33 Mon Sep 17 00:00:00 2001 From: Rasmus Madsen <76999316+imMadsen@users.noreply.github.com> Date: Wed, 6 Nov 2024 10:11:33 +0100 Subject: [PATCH 5/5] Test --- Chipmunk2D | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Chipmunk2D b/Chipmunk2D index ec227c3f..401e98e7 160000 --- a/Chipmunk2D +++ b/Chipmunk2D @@ -1 +1 @@ -Subproject commit ec227c3fc1995e9936ffaf4d8e612048fdbe4f24 +Subproject commit 401e98e78e1f78e4718d0d2301bcf1ad06b3d035