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 afcee9f..a3a1bfe 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) +} diff --git a/space_test.go b/space_test.go index a8dec9e..7c668e8 100644 --- a/space_test.go +++ b/space_test.go @@ -23,3 +23,22 @@ 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{})) + 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) + } +}