-
Notifications
You must be signed in to change notification settings - Fork 48
/
DynamicsB2ContactManager.go
262 lines (212 loc) · 6.13 KB
/
DynamicsB2ContactManager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package box2d
type B2ContactManager struct {
M_broadPhase B2BroadPhase
M_contactList B2ContactInterface
M_contactCount int
M_contactFilter B2ContactFilterInterface
M_contactListener B2ContactListenerInterface
}
var b2_defaultFilter = &B2ContactFilter{}
var b2_defaultListener B2ContactListenerInterface
func MakeB2ContactManager() B2ContactManager {
return B2ContactManager{
M_broadPhase: MakeB2BroadPhase(),
M_contactList: nil,
M_contactCount: 0,
M_contactFilter: b2_defaultFilter,
M_contactListener: b2_defaultListener,
}
}
func NewB2ContactManager() *B2ContactManager {
res := MakeB2ContactManager()
return &res
}
func (mgr *B2ContactManager) Destroy(c B2ContactInterface) {
fixtureA := c.GetFixtureA()
fixtureB := c.GetFixtureB()
bodyA := fixtureA.GetBody()
bodyB := fixtureB.GetBody()
if mgr.M_contactListener != nil && c.IsTouching() {
mgr.M_contactListener.EndContact(c)
}
// Remove from the world.
if c.GetPrev() != nil {
c.GetPrev().SetNext(c.GetNext())
}
if c.GetNext() != nil {
c.GetNext().SetPrev(c.GetPrev())
}
if c == mgr.M_contactList {
mgr.M_contactList = c.GetNext()
}
// Remove from body 1
if c.GetNodeA().Prev != nil {
c.GetNodeA().Prev.Next = c.GetNodeA().Next
}
if c.GetNodeA().Next != nil {
c.GetNodeA().Next.Prev = c.GetNodeA().Prev
}
if c.GetNodeA() == bodyA.M_contactList {
bodyA.M_contactList = c.GetNodeA().Next
}
// Remove from body 2
if c.GetNodeB().Prev != nil {
c.GetNodeB().Prev.Next = c.GetNodeB().Next
}
if c.GetNodeB().Next != nil {
c.GetNodeB().Next.Prev = c.GetNodeB().Prev
}
if c.GetNodeB() == bodyB.M_contactList {
bodyB.M_contactList = c.GetNodeB().Next
}
// Call the factory.
B2ContactDestroy(c)
mgr.M_contactCount--
}
// This is the top level collision call for the time step. Here
// all the narrow phase collision is processed for the world
// contact list.
func (mgr *B2ContactManager) Collide() {
// Update awake contacts.
c := mgr.M_contactList
for c != nil {
fixtureA := c.GetFixtureA()
fixtureB := c.GetFixtureB()
indexA := c.GetChildIndexA()
indexB := c.GetChildIndexB()
bodyA := fixtureA.GetBody()
bodyB := fixtureB.GetBody()
// Is this contact flagged for filtering?
if (c.GetFlags() & B2Contact_Flag.E_filterFlag) != 0x0000 {
// Should these bodies collide?
if bodyB.ShouldCollide(bodyA) == false {
cNuke := c
c = cNuke.GetNext()
mgr.Destroy(cNuke)
continue
}
// Check user filtering.
if mgr.M_contactFilter != nil && mgr.M_contactFilter.ShouldCollide(fixtureA, fixtureB) == false {
cNuke := c
c = cNuke.GetNext()
mgr.Destroy(cNuke)
continue
}
// Clear the filtering flag.
c.SetFlags(c.GetFlags() & ^B2Contact_Flag.E_filterFlag)
}
activeA := bodyA.IsAwake() && bodyA.M_type != B2BodyType.B2_staticBody
activeB := bodyB.IsAwake() && bodyB.M_type != B2BodyType.B2_staticBody
// At least one body must be awake and it must be dynamic or kinematic.
if activeA == false && activeB == false {
c = c.GetNext()
continue
}
proxyIdA := fixtureA.M_proxies[indexA].ProxyId
proxyIdB := fixtureB.M_proxies[indexB].ProxyId
overlap := mgr.M_broadPhase.TestOverlap(proxyIdA, proxyIdB)
// Here we destroy contacts that cease to overlap in the broad-phase.
if overlap == false {
cNuke := c
c = cNuke.GetNext()
mgr.Destroy(cNuke)
continue
}
// The contact persists.
B2ContactUpdate(c, mgr.M_contactListener)
c = c.GetNext()
}
}
func (mgr *B2ContactManager) FindNewContacts() {
mgr.M_broadPhase.UpdatePairs(mgr.AddPair)
}
func (mgr *B2ContactManager) AddPair(proxyUserDataA interface{}, proxyUserDataB interface{}) {
proxyA := proxyUserDataA.(*B2FixtureProxy)
proxyB := proxyUserDataB.(*B2FixtureProxy)
fixtureA := proxyA.Fixture
fixtureB := proxyB.Fixture
indexA := proxyA.ChildIndex
indexB := proxyB.ChildIndex
bodyA := fixtureA.GetBody()
bodyB := fixtureB.GetBody()
// Are the fixtures on the same body?
if bodyA == bodyB {
return
}
// TODO_ERIN use a hash table to remove a potential bottleneck when both
// bodies have a lot of contacts.
// Does a contact already exist?
edge := bodyB.GetContactList()
for edge != nil {
if edge.Other == bodyA {
fA := edge.Contact.GetFixtureA()
fB := edge.Contact.GetFixtureB()
iA := edge.Contact.GetChildIndexA()
iB := edge.Contact.GetChildIndexB()
if fA == fixtureA && fB == fixtureB && iA == indexA && iB == indexB {
// A contact already exists.
return
}
if fA == fixtureB && fB == fixtureA && iA == indexB && iB == indexA {
// A contact already exists.
return
}
}
edge = edge.Next
}
// Does a joint override collision? Is at least one body dynamic?
if bodyB.ShouldCollide(bodyA) == false {
return
}
// Check user filtering.
if mgr.M_contactFilter != nil && mgr.M_contactFilter.ShouldCollide(fixtureA, fixtureB) == false {
return
}
// Call the factory.
c := B2ContactFactory(fixtureA, indexA, fixtureB, indexB)
if c == nil {
return
}
// Contact creation may swap fixtures.
fixtureA = c.GetFixtureA()
fixtureB = c.GetFixtureB()
indexA = c.GetChildIndexA()
indexB = c.GetChildIndexB()
bodyA = fixtureA.GetBody()
bodyB = fixtureB.GetBody()
// Insert into the world.
c.SetPrev(nil)
c.SetNext(mgr.M_contactList)
if mgr.M_contactList != nil {
mgr.M_contactList.SetPrev(c)
}
mgr.M_contactList = c
// Connect to island graph.
// Connect to body A
// fmt.Printf("getNode(): %p\n", c.GetNodeA())
// fmt.Printf("getNode(): %p\n", c.GetNodeA())
// fmt.Printf("getNode(): %p\n", c.GetNodeA())
c.GetNodeA().Contact = c
c.GetNodeA().Other = bodyB
c.GetNodeA().Prev = nil
c.GetNodeA().Next = bodyA.M_contactList
if bodyA.M_contactList != nil {
bodyA.M_contactList.Prev = c.GetNodeA()
}
bodyA.M_contactList = c.GetNodeA()
// Connect to body B
c.GetNodeB().Contact = c
c.GetNodeB().Other = bodyA
c.GetNodeB().Prev = nil
c.GetNodeB().Next = bodyB.M_contactList
if bodyB.M_contactList != nil {
bodyB.M_contactList.Prev = c.GetNodeB()
}
bodyB.M_contactList = c.GetNodeB()
// Wake up the bodies
if fixtureA.IsSensor() == false && fixtureB.IsSensor() == false {
bodyA.SetAwake(true)
bodyB.SetAwake(true)
}
mgr.M_contactCount++
}