From c3323d6d4e14a39d382f4012f7b95dc5d14a2541 Mon Sep 17 00:00:00 2001 From: Ernest Micklei Date: Fri, 11 Oct 2024 09:14:29 +0200 Subject: [PATCH 1/3] add ReindexShape --- space.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/space.go b/space.go index afcee9f..878c72f 100644 --- a/space.go +++ b/space.go @@ -1129,3 +1129,15 @@ func (space *Space) ShapeQuery(shape *Shape, callback func(shape *Shape, points return anyCollision } + +// ReindexShape re-computes the hash of the shape in both the dynamic and static list. +func (space *Space) ReindexShape(shape *Shape) { + + assert(space.locked > 0, "You cannot manually reindex objects while the space is locked. Wait until the current query or step is complete.") + + shape.CacheBB() + + // attempt to rehash the shape in both hashes + space.dynamicShapes.class.ReindexObject(shape, shape.hashid) + space.staticShapes.class.ReindexObject(shape, shape.hashid) +} From 480fa9ae499ecb44d0486713003f0e1d55c5b200 Mon Sep 17 00:00:00 2001 From: Ernest Micklei Date: Fri, 11 Oct 2024 13:18:44 +0200 Subject: [PATCH 2/3] add test, add BBTree>ReindexObject --- bbtree.go | 8 +++++++- space.go | 2 +- space_test.go | 8 ++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/bbtree.go b/bbtree.go index 26fd6e6..e2c20e6 100644 --- a/bbtree.go +++ b/bbtree.go @@ -320,7 +320,13 @@ func (tree *BBTree) Reindex() { } func (tree *BBTree) ReindexObject(obj *Shape, hashId HashValue) { - panic("implement me") + leaf := tree.leaves.Find(hashId, obj) + if leaf != nil { + if tree.LeafUpdate(leaf) { + tree.LeafAddPairs(leaf) + } + tree.IncrementStamp() + } } func (tree *BBTree) ReindexQuery(f SpatialIndexQuery, data interface{}) { diff --git a/space.go b/space.go index 878c72f..a3a1bfe 100644 --- a/space.go +++ b/space.go @@ -1133,7 +1133,7 @@ func (space *Space) ShapeQuery(shape *Shape, callback func(shape *Shape, points // ReindexShape re-computes the hash of the shape in both the dynamic and static list. func (space *Space) ReindexShape(shape *Shape) { - assert(space.locked > 0, "You cannot manually reindex objects while the space is locked. Wait until the current query or step is complete.") + assert(space.locked == 0, "You cannot manually reindex objects while the space is locked. Wait until the current query or step is complete.") shape.CacheBB() diff --git a/space_test.go b/space_test.go index a8dec9e..d0ec7c4 100644 --- a/space_test.go +++ b/space_test.go @@ -23,3 +23,11 @@ func TestSpace_ShapeQuery(t *testing.T) { t.Error("Box should be just out of range") }) } + +func TestSpace_ReindexShape(t *testing.T) { + space := NewSpace() + circle := space.AddShape(NewCircle(space.StaticBody, 1, Vector{})) + space.ReindexShape(circle) + circle.body.SetPosition(Vector{X: 12.0, Y: 34.0}) + space.ReindexShape(circle) +} From 2e1201b8768b978dcadbc04850d9c7e4e5b2e919 Mon Sep 17 00:00:00 2001 From: Ernest Micklei Date: Fri, 11 Oct 2024 13:27:01 +0200 Subject: [PATCH 3/3] add bb test --- space_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/space_test.go b/space_test.go index d0ec7c4..7c668e8 100644 --- a/space_test.go +++ b/space_test.go @@ -27,7 +27,18 @@ func TestSpace_ShapeQuery(t *testing.T) { func TestSpace_ReindexShape(t *testing.T) { space := NewSpace() circle := space.AddShape(NewCircle(space.StaticBody, 1, Vector{})) + bb1 := circle.bb space.ReindexShape(circle) + bb2 := circle.bb + // check unchanged + if got, want := bb1.String(), bb2.String(); got != want { + t.Errorf("got [%[1]v:%[1]T] want [%[2]v:%[2]T]", got, want) + } circle.body.SetPosition(Vector{X: 12.0, Y: 34.0}) space.ReindexShape(circle) + bb3 := circle.bb + // check changed + if got, want := bb2.String(), bb3.String(); got == want { + t.Errorf("got [%[1]v:%[1]T] want [%[2]v:%[2]T]", got, want) + } }