Skip to content

Commit

Permalink
Adds *Space ReindexShape (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
emicklei authored Oct 11, 2024
1 parent a95d2e2 commit 77433c8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
8 changes: 7 additions & 1 deletion bbtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}) {
Expand Down
12 changes: 12 additions & 0 deletions space.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
19 changes: 19 additions & 0 deletions space_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 77433c8

Please sign in to comment.