Skip to content

Commit 3f3fde8

Browse files
committed
feat: worker property on World
Can set the number of workers for the application. This parameter is important, small simulations work best with smaller workers count
1 parent bec7a78 commit 3f3fde8

File tree

3 files changed

+72
-52
lines changed

3 files changed

+72
-52
lines changed

collision.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,22 @@ type CollisionPair struct {
3232
// BroadPhase performs broad-phase collision detection using AABB overlap tests
3333
// It returns pairs of bodies whose AABBs overlap and might be colliding
3434
// This is an O(n²) brute-force approach suitable for small numbers of bodies
35-
func BroadPhase(spatialGrid *SpatialGrid, bodies []*actor.RigidBody) <-chan Pair {
35+
func BroadPhase(spatialGrid *SpatialGrid, bodies []*actor.RigidBody, workersCount int) <-chan Pair {
3636
spatialGrid.Clear()
3737
for i, body := range bodies {
3838
spatialGrid.Insert(i, body)
3939
}
4040
spatialGrid.SortCells()
4141

42-
checkingPairs := spatialGrid.FindPairsParallel(bodies, WORKERS)
42+
checkingPairs := spatialGrid.FindPairsParallel(bodies, workersCount)
4343

4444
return checkingPairs
4545
}
4646

47-
func NarrowPhase(pairs <-chan Pair) []*constraint.ContactConstraint {
47+
func NarrowPhase(pairs <-chan Pair, workersCount int) []*constraint.ContactConstraint {
4848
// Dispatcher: separate pairs with planes, and normal convex objects
49-
planePairs := make(chan Pair, WORKERS)
50-
gjkPairs := make(chan Pair, WORKERS)
49+
planePairs := make(chan Pair, workersCount)
50+
gjkPairs := make(chan Pair, workersCount)
5151

5252
go func() {
5353
defer close(planePairs)
@@ -66,14 +66,14 @@ func NarrowPhase(pairs <-chan Pair) []*constraint.ContactConstraint {
6666
}()
6767

6868
// Canal pour collecter tous les contacts
69-
allContacts := make(chan *constraint.ContactConstraint, WORKERS*2)
69+
allContacts := make(chan *constraint.ContactConstraint, workersCount*2)
7070
var wg sync.WaitGroup
7171
// Path 1: GJK/EPA for convex objects
7272
wg.Add(1)
7373
go func() {
7474
defer wg.Done()
75-
collisionPairs := GJK(gjkPairs)
76-
contactsChan := EPA(collisionPairs)
75+
collisionPairs := GJK(gjkPairs, workersCount)
76+
contactsChan := EPA(collisionPairs, workersCount)
7777
for contact := range contactsChan {
7878
allContacts <- contact
7979
}
@@ -83,7 +83,7 @@ func NarrowPhase(pairs <-chan Pair) []*constraint.ContactConstraint {
8383
wg.Add(1)
8484
go func() {
8585
defer wg.Done()
86-
contactsChan := collidePlane(planePairs)
86+
contactsChan := collidePlane(planePairs, workersCount)
8787
for contact := range contactsChan {
8888
allContacts <- contact
8989
}
@@ -104,14 +104,14 @@ func NarrowPhase(pairs <-chan Pair) []*constraint.ContactConstraint {
104104
return contacts
105105
}
106106

107-
func GJK(pairChan <-chan Pair) <-chan CollisionPair {
108-
collisionChan := make(chan CollisionPair, WORKERS)
107+
func GJK(pairChan <-chan Pair, workersCount int) <-chan CollisionPair {
108+
collisionChan := make(chan CollisionPair, workersCount)
109109

110110
go func() {
111111
var wg sync.WaitGroup
112112
defer close(collisionChan)
113113

114-
for range WORKERS {
114+
for range workersCount {
115115
wg.Add(1)
116116
go func() {
117117
defer wg.Done()
@@ -139,14 +139,14 @@ func GJK(pairChan <-chan Pair) <-chan CollisionPair {
139139
return collisionChan
140140
}
141141

142-
func EPA(p <-chan CollisionPair) <-chan *constraint.ContactConstraint {
143-
ch := make(chan *constraint.ContactConstraint, WORKERS)
142+
func EPA(p <-chan CollisionPair, workersCount int) <-chan *constraint.ContactConstraint {
143+
ch := make(chan *constraint.ContactConstraint, workersCount)
144144

145145
go func() {
146146
var wg sync.WaitGroup
147147
defer close(ch)
148148

149-
for range WORKERS {
149+
for range workersCount {
150150
wg.Add(1)
151151
go func() {
152152
defer wg.Done()
@@ -167,14 +167,14 @@ func EPA(p <-chan CollisionPair) <-chan *constraint.ContactConstraint {
167167
return ch
168168
}
169169

170-
func collidePlane(pairs <-chan Pair) <-chan *constraint.ContactConstraint {
171-
ch := make(chan *constraint.ContactConstraint, WORKERS)
170+
func collidePlane(pairs <-chan Pair, workersCount int) <-chan *constraint.ContactConstraint {
171+
ch := make(chan *constraint.ContactConstraint, workersCount)
172172

173173
go func() {
174174
var wg sync.WaitGroup
175175
defer close(ch)
176176

177-
for range WORKERS {
177+
for range workersCount {
178178
wg.Add(1)
179179
go func() {
180180
defer wg.Done()

0 commit comments

Comments
 (0)