Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion body.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (body *Body) SetType(newType int) {
if body.space == nil {
return
}
assert(body.space.locked == 0, "Space is locked")
assertSpaceUnlocked(body.space)

if oldType != BODY_STATIC {
body.Activate()
Expand Down
4 changes: 4 additions & 0 deletions debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ func assert(truth bool, msg ...interface{}) {
panic(fmt.Sprint("Assertion failed: ", msg))
}
}

func assertSpaceUnlocked(space *Space) {
assert(!space.locked, "This operation cannot be done safely during a call to Space.Step() or during a query. Put these calls into a post-step callback.")
}
2 changes: 2 additions & 0 deletions release.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
package cp

func assert(_ bool, _ ...interface{}) {}

func assertSpaceUnlocked(_ *Space) {}
32 changes: 15 additions & 17 deletions space.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type Space struct {
cachedArbiters *HashSet[ShapePair, *Arbiter]
pooledArbiters sync.Pool

locked int
locked bool

usesWildcards bool
collisionHandlers *HashSet[*CollisionHandler, *CollisionHandler]
Expand All @@ -68,7 +68,7 @@ func NewSpace() *Space {
collisionSlop: 0.1,
collisionBias: math.Pow(0.9, 60),
collisionPersistence: 3,
locked: 0,
locked: false,
stamp: 0,
shapeIDCounter: 1,
staticShapes: NewBBTree(ShapeGetBB, nil),
Expand Down Expand Up @@ -147,7 +147,7 @@ func (space *Space) SetStaticBody(body *Body) {
func (space *Space) Activate(body *Body) {
assert(body.GetType() == BODY_DYNAMIC, "Attempting to activate a non-dynamic body")

if space.locked != 0 {
if space.locked {
if !Contains(space.rousedBodies, body) {
space.rousedBodies = append(space.rousedBodies, body)
}
Expand Down Expand Up @@ -262,7 +262,7 @@ func (space *Space) AddShape(shape *Shape) *Shape {

assert(shape.space != space, "You have already added this shape to this space. You must not add it a second time.")
assert(shape.space == nil, "You have already added this shape to another space. You cannot add it to a second.")
assert(space.locked == 0, "This operation cannot be done safely during a call to cpSpaceStep() or during a query. Put these calls into a post-step callback.")
assertSpaceUnlocked(space)

isStatic := body.GetType() == BODY_STATIC
if !isStatic {
Expand Down Expand Up @@ -299,7 +299,7 @@ func (space *Space) AddBody(body *Body) *Body {
func (space *Space) AddConstraint(constraint *Constraint) *Constraint {
assert(constraint.space != space, "Already added to this space")
assert(constraint.space == nil, "Already added to another space")
assert(space.locked == 0, "Space is locked")
assertSpaceUnlocked(space)

a := constraint.a
b := constraint.b
Expand All @@ -323,7 +323,7 @@ func (space *Space) AddConstraint(constraint *Constraint) *Constraint {

func (space *Space) RemoveConstraint(constraint *Constraint) {
assert(space.ContainsConstraint(constraint), "Constraint not found")
assert(space.locked == 0, "Space is locked")
assertSpaceUnlocked(space)

constraint.a.Activate()
constraint.b.Activate()
Expand All @@ -342,7 +342,7 @@ func (space *Space) RemoveConstraint(constraint *Constraint) {
func (space *Space) RemoveShape(shape *Shape) {
body := shape.body
assert(space.ContainsShape(shape))
assert(space.locked == 0)
assertSpaceUnlocked(space)

isStatic := body.GetType() == BODY_STATIC
if isStatic {
Expand All @@ -365,7 +365,7 @@ func (space *Space) RemoveShape(shape *Shape) {
func (space *Space) RemoveBody(body *Body) {
assert(body != space.StaticBody)
assert(space.ContainsBody(body))
assert(space.locked == 0)
assertSpaceUnlocked(space)

body.Activate()
if body.GetType() == BODY_STATIC {
Expand Down Expand Up @@ -782,17 +782,11 @@ func (space *Space) Step(dt float64) {
}

func (space *Space) Lock() {
space.locked++
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a behavioral change. With the int someone could be doing:

space.Lock()
space.Lock()
space.Unlock()
space.Unlock()

By changing to a boolean this will now error.

I'm not sure why this would matter but I want to keep close to the original source as possible.

space.locked = true
}

func (space *Space) Unlock(runPostStep bool) {
space.locked--

assert(space.locked >= 0, "Space lock underflow")

if space.locked != 0 {
return
}
space.locked = false

for i := 0; i < len(space.rousedBodies); i++ {
space.Activate(space.rousedBodies[i])
Expand Down Expand Up @@ -820,6 +814,10 @@ func (space *Space) Unlock(runPostStep bool) {
}
}

func (space *Space) IsLocked() bool {
return space.locked
}

func (space *Space) UncacheArbiter(arb *Arbiter) {
a := arb.a
b := arb.b
Expand Down Expand Up @@ -1133,7 +1131,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, "You cannot manually reindex objects while the space is locked. Wait until the current query or step is complete.")

shape.CacheBB()

Expand Down