-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathcontact.go
435 lines (337 loc) · 11.6 KB
/
contact.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package box2d
import (
"math"
)
/// Friction mixing law. The idea is to allow either fixture to drive the friction to zero.
/// For example, anything slides on ice.
func MixFriction(friction1, friction2 float64) float64 {
return math.Sqrt(friction1 * friction2)
}
/// Restitution mixing law. The idea is allow for anything to bounce off an inelastic surface.
/// For example, a superball bounces on anything.
func MixRestitution(restitution1, restitution2 float64) float64 {
if restitution1 > restitution2 {
return restitution1
}
return restitution2
}
type ContactInterface interface {
Data() *Contact
Evaluate(*Manifold, Transform, Transform)
}
type ContactCreateFunc func(fixtureA *Fixture, indexA int, fixtureB *Fixture, indexB int) ContactInterface // returned contact should be a pointer
type ContactRegister struct {
CreateFunc ContactCreateFunc
Primary bool
}
/// A contact edge is used to connect bodies and contacts together
/// in a contact graph where each body is a node and each contact
/// is an edge. A contact edge belongs to a doubly linked list
/// maintained in each attached body. Each contact has two contact
/// nodes, one for each attached body.
type ContactEdge struct {
Other *Body ///< provides quick access to the other body attached.
Contact ContactInterface ///< the contact
Prev *ContactEdge ///< the previous contact edge in the body's contact list
Next *ContactEdge ///< the next contact edge in the body's contact list
}
func NewContactEdge() *ContactEdge {
return &ContactEdge{}
}
type ContactFlag uint32
const (
// ContactFlagIsland is used when crawling contact graph when forming islands.
ContactFlagIsland ContactFlag = 0x0001
// ContactFlagTouching is set when the shapes are touching.
ContactFlagTouching ContactFlag = 0x0002
// ContactFlagEnabled means this contact can be disabled (by user)
ContactFlagEnabled ContactFlag = 0x0004
// ContactFlagFilter means this contact needs filtering because a fixture filter was changed.
ContactFlagFilter ContactFlag = 0x0008
// ContactFlagBulletHit means this bullet contact had a TOI event
ContactFlagBulletHit ContactFlag = 0x0010
// ContactFlagToi means this contact has a valid TOI in m_toi
ContactFlagToi ContactFlag = 0x0020
)
// /// The class manages contact between two shapes. A contact exists for each overlapping
// /// AABB in the broad-phase (except if filtered). Therefore a contact object may exist
// /// that has no contact points.
var s_registers [][]ContactRegister
var s_initialized = false
type Contact struct {
Flags ContactFlag
// World pool and list pointers.
Prev ContactInterface //should be backed by a pointer
Next ContactInterface //should be backed by a pointer
// Nodes for connecting bodies.
NodeA *ContactEdge
NodeB *ContactEdge
FixtureA *Fixture
FixtureB *Fixture
IndexA int
IndexB int
Manifold *Manifold
ToiCount int
Toi float64
Friction float64
Restitution float64
TangentSpeed float64
}
func (contact Contact) GetFlags() ContactFlag {
return contact.Flags
}
func (contact *Contact) SetFlags(flags ContactFlag) {
contact.Flags = flags
}
func (contact Contact) GetPrev() ContactInterface {
return contact.Prev
}
func (contact *Contact) SetPrev(prev ContactInterface) {
contact.Prev = prev
}
func (contact Contact) GetNext() ContactInterface {
return contact.Next
}
func (contact *Contact) SetNext(next ContactInterface) {
contact.Next = next
}
func (contact Contact) GetNodeA() *ContactEdge {
return contact.NodeA
}
func (contact *Contact) SetNodeA(node *ContactEdge) {
contact.NodeA = node
}
func (contact Contact) GetNodeB() *ContactEdge {
return contact.NodeB
}
func (contact *Contact) SetNodeB(node *ContactEdge) {
contact.NodeB = node
}
func (contact Contact) GetFixtureA() *Fixture {
return contact.FixtureA
}
func (contact *Contact) SetFixtureA(fixture *Fixture) {
contact.FixtureA = fixture
}
func (contact Contact) GetFixtureB() *Fixture {
return contact.FixtureB
}
func (contact *Contact) SetFixtureB(fixture *Fixture) {
contact.FixtureB = fixture
}
func (contact Contact) GetChildIndexA() int {
return contact.IndexA
}
func (contact *Contact) SetChildIndexA(index int) {
contact.IndexA = index
}
func (contact Contact) GetChildIndexB() int {
return contact.IndexB
}
func (contact *Contact) SetChildIndexB(index int) {
contact.IndexB = index
}
func (contact Contact) GetManifold() *Manifold {
return contact.Manifold
}
func (contact *Contact) SetManifold(manifold *Manifold) {
contact.Manifold = manifold
}
func (contact Contact) GetTOICount() int {
return contact.ToiCount
}
func (contact *Contact) SetTOICount(toiCount int) {
contact.ToiCount = toiCount
}
func (contact Contact) GetTOI() float64 {
return contact.Toi
}
func (contact *Contact) SetTOI(toi float64) {
contact.Toi = toi
}
func (contact Contact) GetFriction() float64 {
return contact.Friction
}
func (contact *Contact) SetFriction(friction float64) {
contact.Friction = friction
}
func (contact *Contact) ResetFriction() {
contact.Friction = MixFriction(contact.FixtureA.Friction, contact.FixtureB.Friction)
}
func (contact Contact) GetRestitution() float64 {
return contact.Restitution
}
func (contact *Contact) SetRestitution(restitution float64) {
contact.Restitution = restitution
}
func (contact *Contact) ResetRestitution() {
contact.Restitution = MixRestitution(contact.FixtureA.Restitution, contact.FixtureB.Restitution)
}
func (contact Contact) GetTangentSpeed() float64 {
return contact.TangentSpeed
}
func (contact *Contact) SetTangentSpeed(speed float64) {
contact.TangentSpeed = speed
}
func (contact Contact) GetWorldManifold(worldManifold *WorldManifold) {
bodyA := contact.FixtureA.GetBody()
bodyB := contact.FixtureB.GetBody()
shapeA := contact.FixtureA.GetShape()
shapeB := contact.FixtureB.GetShape()
worldManifold.Initialize(contact.Manifold, bodyA.GetTransform(), shapeA.GetRadius(), bodyB.GetTransform(), shapeB.GetRadius())
}
func (contact *Contact) SetEnabled(flag bool) {
if flag {
contact.Flags |= ContactFlagEnabled
} else {
contact.Flags &= ^ContactFlagEnabled
}
}
func (contact Contact) IsEnabled() bool {
return (contact.Flags & ContactFlagEnabled) == ContactFlagEnabled
}
func (contact Contact) IsTouching() bool {
return (contact.Flags & ContactFlagTouching) == ContactFlagTouching
}
func (contact *Contact) FlagForFiltering() {
contact.Flags |= ContactFlagFilter
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Contact.cpp
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
func ContactInitializeRegisters() {
s_registers = make([][]ContactRegister, ShapeTypeTypeCount)
for i := 0; i < int(ShapeTypeTypeCount); i++ {
s_registers[i] = make([]ContactRegister, ShapeTypeTypeCount)
}
AddType(PolygonContact_Create, ShapeTypePolygon, ShapeTypePolygon)
AddType(EdgeAndPolygonContact_Create, ShapeTypeEdge, ShapeTypePolygon)
}
func AddType(createFunc ContactCreateFunc, type1 ShapeType, type2 ShapeType) {
s_registers[type1][type2].CreateFunc = createFunc
s_registers[type1][type2].Primary = true
if type1 != type2 {
s_registers[type2][type1].CreateFunc = createFunc
s_registers[type2][type1].Primary = false
}
}
func ContactFactory(fixtureA *Fixture, indexA int, fixtureB *Fixture, indexB int) ContactInterface { // returned contact should be a pointer
if s_initialized == false {
ContactInitializeRegisters()
s_initialized = true
}
type1 := fixtureA.GetType()
type2 := fixtureB.GetType()
createFunc := s_registers[type1][type2].CreateFunc
if createFunc != nil {
if s_registers[type1][type2].Primary {
return createFunc(fixtureA, indexA, fixtureB, indexB)
} else {
return createFunc(fixtureB, indexB, fixtureA, indexA)
}
}
return nil
}
func ContactDestroy(contact ContactInterface) {
contactData := contact.Data()
fixtureA := contactData.FixtureA
fixtureB := contactData.FixtureB
if contactData.GetManifold().PointCount > 0 && fixtureA.IsSensor == false && fixtureB.IsSensor == false {
fixtureA.GetBody().SetAwake(true)
fixtureB.GetBody().SetAwake(true)
}
}
func MakeContact(fA *Fixture, indexA int, fB *Fixture, indexB int) Contact {
contact := Contact{}
contact.Flags = ContactFlagEnabled
contact.FixtureA = fA
contact.FixtureB = fB
contact.IndexA = indexA
contact.IndexB = indexB
contact.Manifold = &Manifold{}
contact.Manifold.PointCount = 0
contact.Prev = nil
contact.Next = nil
contact.NodeA = NewContactEdge()
contact.NodeA.Contact = nil
contact.NodeA.Prev = nil
contact.NodeA.Next = nil
contact.NodeA.Other = nil
contact.NodeB = NewContactEdge()
contact.NodeB.Contact = nil
contact.NodeB.Prev = nil
contact.NodeB.Next = nil
contact.NodeB.Other = nil
contact.ToiCount = 0
contact.Friction = MixFriction(contact.FixtureA.Friction, contact.FixtureB.Friction)
contact.Restitution = MixRestitution(contact.FixtureA.Restitution, contact.FixtureB.Restitution)
contact.TangentSpeed = 0.0
return contact
}
// Update the contact manifold and touching status.
// Note: do not assume the fixture AABBs are overlapping or are valid.
func ContactUpdate(contact ContactInterface, listener ContactListenerInterface) {
contactData := contact.Data()
oldManifold := *contactData.GetManifold()
// Re-enable this contact.
contactData.SetFlags(contactData.GetFlags() | ContactFlagEnabled)
touching := false
wasTouching := (contactData.GetFlags() & ContactFlagTouching) == ContactFlagTouching
sensorA := contactData.GetFixtureA().IsSensor
sensorB := contactData.GetFixtureB().IsSensor
sensor := sensorA || sensorB
bodyA := contactData.GetFixtureA().GetBody()
bodyB := contactData.GetFixtureB().GetBody()
xfA := bodyA.GetTransform()
xfB := bodyB.GetTransform()
// Is this contact a sensor?
if sensor {
shapeA := contactData.GetFixtureA().GetShape()
shapeB := contactData.GetFixtureB().GetShape()
touching = TestOverlapShapes(shapeA, contactData.GetChildIndexA(), shapeB, contactData.GetChildIndexB(), xfA, xfB)
// Sensors don't generate manifolds.
contactData.GetManifold().PointCount = 0
} else {
// ContactInterface is extended by specialized contact structs and mentionned by ContactInterface but not implemented on specialized structs
contact.Evaluate(contactData.GetManifold(), xfA, xfB) // should be evaluated on specialisations of contact (like CircleContact)
touching = contactData.GetManifold().PointCount > 0
// Match old contact ids to new contact ids and copy the
// stored impulses to warm start the solver.
for i := 0; i < contactData.GetManifold().PointCount; i++ {
mp2 := &contactData.GetManifold().Points[i]
mp2.NormalImpulse = 0.0
mp2.TangentImpulse = 0.0
id2 := mp2.Id
for j := 0; j < oldManifold.PointCount; j++ {
mp1 := &oldManifold.Points[j]
if mp1.Id.Key() == id2.Key() {
mp2.NormalImpulse = mp1.NormalImpulse
mp2.TangentImpulse = mp1.TangentImpulse
break
}
}
}
if touching != wasTouching {
bodyA.SetAwake(true)
bodyB.SetAwake(true)
}
}
if touching {
contactData.SetFlags(contactData.GetFlags() | ContactFlagTouching)
} else {
contactData.SetFlags(contactData.GetFlags() & ^ContactFlagTouching)
}
if wasTouching == false && touching == true && listener != nil {
listener.BeginContact(contact)
}
if wasTouching == true && touching == false && listener != nil {
listener.EndContact(contact)
}
if sensor == false && touching && listener != nil {
listener.PreSolve(contact, oldManifold)
}
}