-
Notifications
You must be signed in to change notification settings - Fork 27
/
contact_manager.go
265 lines (215 loc) · 6.38 KB
/
contact_manager.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
263
264
265
package box2d
type ContactManager struct {
BroadPhase BroadPhase
ContactList ContactInterface
ContactCount int
ContactFilter ContactFilterInterface
ContactListener ContactListenerInterface
}
var b2_defaultFilter ContactFilterInterface
var b2_defaultListener ContactListenerInterface
func MakeContactManager() ContactManager {
return ContactManager{
BroadPhase: MakeBroadPhase(),
ContactList: nil,
ContactCount: 0,
ContactFilter: b2_defaultFilter,
ContactListener: b2_defaultListener,
}
}
func NewContactManager() *ContactManager {
res := MakeContactManager()
return &res
}
func (mgr *ContactManager) Destroy(contact ContactInterface) {
c := contact.Data()
fixtureA := c.GetFixtureA()
fixtureB := c.GetFixtureB()
bodyA := fixtureA.GetBody()
bodyB := fixtureB.GetBody()
if mgr.ContactListener != nil && c.IsTouching() {
mgr.ContactListener.EndContact(contact)
}
// Remove from the world.
if c.GetPrev() != nil {
c.GetPrev().Data().SetNext(c.GetNext())
}
if c.GetNext() != nil {
c.GetNext().Data().SetPrev(c.GetPrev())
}
if contact == mgr.ContactList {
mgr.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.ContactList {
bodyA.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.ContactList {
bodyB.ContactList = c.GetNodeB().Next
}
// Call the factory.
ContactDestroy(contact)
mgr.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 *ContactManager) Collide() {
// Update awake contacts.
c := mgr.ContactList
for c != nil {
contactData := c.Data()
fixtureA := contactData.GetFixtureA()
fixtureB := contactData.GetFixtureB()
indexA := contactData.GetChildIndexA()
indexB := contactData.GetChildIndexB()
bodyA := fixtureA.GetBody()
bodyB := fixtureB.GetBody()
// Is this contact flagged for filtering?
if (contactData.GetFlags() & ContactFlagFilter) != 0x0000 {
// Should these bodies collide?
if bodyB.ShouldCollide(bodyA) == false {
cNuke := c
c = cNuke.Data().GetNext()
mgr.Destroy(cNuke)
continue
}
// Check user filtering.
if mgr.ContactFilter != nil && mgr.ContactFilter.ShouldCollide(fixtureA, fixtureB) == false {
cNuke := c
c = cNuke.Data().GetNext()
mgr.Destroy(cNuke)
continue
}
// Clear the filtering flag.
contactData.SetFlags(contactData.GetFlags() & ^ContactFlagFilter)
}
activeA := bodyA.IsAwake() && bodyA.Type != BodyTypeStaticBody
activeB := bodyB.IsAwake() && bodyB.Type != BodyTypeStaticBody
// At least one body must be awake and it must be dynamic or kinematic.
if activeA == false && activeB == false {
c = contactData.GetNext()
continue
}
proxyIdA := fixtureA.Proxies[indexA].ProxyId
proxyIdB := fixtureB.Proxies[indexB].ProxyId
overlap := mgr.BroadPhase.TestOverlap(proxyIdA, proxyIdB)
// Here we destroy contacts that cease to overlap in the broad-phase.
if overlap == false {
cNuke := c
c = cNuke.Data().GetNext()
mgr.Destroy(cNuke)
continue
}
// The contact persists.
ContactUpdate(c, mgr.ContactListener)
c = contactData.GetNext()
}
}
func (mgr *ContactManager) FindNewContacts() {
mgr.BroadPhase.UpdatePairs(mgr.AddPair)
}
func (mgr *ContactManager) AddPair(proxyUserDataA interface{}, proxyUserDataB interface{}) {
proxyA := proxyUserDataA.(*FixtureProxy)
proxyB := proxyUserDataB.(*FixtureProxy)
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.Data().GetFixtureA()
fB := edge.Contact.Data().GetFixtureB()
iA := edge.Contact.Data().GetChildIndexA()
iB := edge.Contact.Data().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.ContactFilter != nil && mgr.ContactFilter.ShouldCollide(fixtureA, fixtureB) == false {
return
}
// Call the factory.
c := ContactFactory(fixtureA, indexA, fixtureB, indexB)
if c == nil {
return
}
contactData := c.Data()
// Contact creation may swap fixtures.
fixtureA = contactData.GetFixtureA()
fixtureB = contactData.GetFixtureB()
indexA = contactData.GetChildIndexA()
indexB = contactData.GetChildIndexB()
bodyA = fixtureA.GetBody()
bodyB = fixtureB.GetBody()
// Insert into the world.
contactData.SetPrev(nil)
contactData.SetNext(mgr.ContactList)
if mgr.ContactList != nil {
mgr.ContactList.Data().SetPrev(c)
}
mgr.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())
contactData.GetNodeA().Contact = c
contactData.GetNodeA().Other = bodyB
contactData.GetNodeA().Prev = nil
contactData.GetNodeA().Next = bodyA.ContactList
if bodyA.ContactList != nil {
bodyA.ContactList.Prev = contactData.GetNodeA()
}
bodyA.ContactList = contactData.GetNodeA()
// Connect to body B
contactData.GetNodeB().Contact = c
contactData.GetNodeB().Other = bodyA
contactData.GetNodeB().Prev = nil
contactData.GetNodeB().Next = bodyB.ContactList
if bodyB.ContactList != nil {
bodyB.ContactList.Prev = contactData.GetNodeB()
}
bodyB.ContactList = contactData.GetNodeB()
// Wake up the bodies
if !fixtureA.IsSensor && !fixtureB.IsSensor {
bodyA.SetAwake(true)
bodyB.SetAwake(true)
}
mgr.ContactCount++
}