Skip to content

Commit ec6e66b

Browse files
committed
Introduce body sleep
1 parent 5b0958d commit ec6e66b

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

body.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,3 +641,50 @@ func BodyUpdatePosition(body *Body, dt float64) {
641641
body.v_bias = Vector{}
642642
body.w_bias = 0
643643
}
644+
645+
// Sleep force the body not grouped with any other sleeping bodies to sleep immediately.
646+
func (body *Body) Sleep() {
647+
body.SleepWithGroup(nil)
648+
}
649+
650+
// SleepWithGroup force the body to sleep immediately.
651+
func (body *Body) SleepWithGroup(group *Body) {
652+
assert(body.GetType() == BODY_DYNAMIC, "Non-dynamic bodies cannot be put to sleep.")
653+
654+
assert(!body.space.IsLocked(), "Bodies cannot be put to sleep during a query or a call to Space.Step(). Put these calls into a post-step callback.")
655+
assert(body.space.SleepTimeThreshold < INFINITY, "Sleeping is not enabled on the space. You cannot sleep a body without setting a sleep time threshold on the space.")
656+
assert(group == nil || group.IsSleeping(), "Cannot use a non-sleeping body as a group identifier.")
657+
658+
if body.IsSleeping() {
659+
assert(body.ComponentRoot() == group.ComponentRoot(), "The body is already sleeping and it's group cannot be reassigned.")
660+
return
661+
}
662+
663+
for _, shape := range body.shapeList {
664+
shape.CacheBB()
665+
}
666+
body.space.Deactivate(body)
667+
668+
if group != nil {
669+
root := group.ComponentRoot()
670+
671+
body.sleepingRoot = root
672+
body.sleepingNext = root.sleepingNext
673+
body.sleepingIdleTime = 0
674+
675+
root.sleepingNext = body
676+
} else {
677+
body.sleepingRoot = body
678+
body.sleepingNext = nil
679+
body.sleepingIdleTime = 0
680+
681+
body.space.sleepingComponents = append(body.space.sleepingComponents, body)
682+
}
683+
684+
for i, v := range body.space.dynamicBodies {
685+
if v == body {
686+
body.space.dynamicBodies = append(body.space.dynamicBodies[:i], body.space.dynamicBodies[i+1:]...)
687+
break
688+
}
689+
}
690+
}

0 commit comments

Comments
 (0)