-
Notifications
You must be signed in to change notification settings - Fork 20
/
arbiter.go
531 lines (434 loc) · 14 KB
/
arbiter.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
package cp
import "math"
var WILDCARD_COLLISION_TYPE CollisionType = ^CollisionType(0)
// Arbiter struct tracks pairs of colliding shapes.
//
// They are also used in conjuction with collision handler callbacks allowing you to retrieve information on the collision or change it.
// A unique arbiter value is used for each pair of colliding objects. It persists until the shapes separate.
type Arbiter struct {
e, u float64
surface_vr Vector
UserData interface{}
a, b *Shape
body_a, body_b *Body
thread_a, thread_b ArbiterThread
count int
// a slice onto the current buffer array of contacts
contacts []Contact
n Vector
// Regular, wildcard A and wildcard B collision handlers.
handler, handlerA, handlerB *CollisionHandler
swapped bool
stamp uint
state int // Arbiter state enum
}
// Init initializes and returns Arbiter
func (arbiter *Arbiter) Init(a, b *Shape) *Arbiter {
arbiter.handler = nil
arbiter.swapped = false
arbiter.handlerA = nil
arbiter.handlerB = nil
arbiter.e = 0
arbiter.u = 0
arbiter.surface_vr = Vector{}
arbiter.count = 0
arbiter.contacts = nil
arbiter.a = a
arbiter.body_a = a.body
arbiter.b = b
arbiter.body_b = b.body
arbiter.thread_a.next = nil
arbiter.thread_b.next = nil
arbiter.thread_a.prev = nil
arbiter.thread_b.prev = nil
arbiter.stamp = 0
arbiter.state = CP_ARBITER_STATE_FIRST_COLLISION
arbiter.UserData = nil
return arbiter
}
type ArbiterThread struct {
next, prev *Arbiter
}
func (node *Arbiter) Next(body *Body) *Arbiter {
if node.body_a == body {
return node.thread_a.next
} else {
return node.thread_b.next
}
}
func (arbiter *Arbiter) Unthread() {
arbiter.unthreadHelper(arbiter.body_a)
arbiter.unthreadHelper(arbiter.body_b)
}
func (arbiter *Arbiter) unthreadHelper(body *Body) {
thread := arbiter.ThreadForBody(body)
prev := thread.prev
next := thread.next
if prev != nil {
prev.ThreadForBody(body).next = next
} else if body.arbiterList == arbiter {
// IFF prev is NULL and body->arbiterList == arb, is arb at the head of the list.
// This function may be called for an arbiter that was never in a list.
// In that case, we need to protect it from wiping out the body->arbiterList pointer.
body.arbiterList = next
}
if next != nil {
next.ThreadForBody(body).prev = prev
}
thread.next = nil
thread.prev = nil
}
func (arbiter *Arbiter) ThreadForBody(body *Body) *ArbiterThread {
if arbiter.body_a == body {
return &arbiter.thread_a
} else {
return &arbiter.thread_b
}
}
func (arbiter *Arbiter) ApplyCachedImpulse(dt_coef float64) {
if arbiter.IsFirstContact() {
return
}
for i := 0; i < arbiter.count; i++ {
contact := arbiter.contacts[i]
j := arbiter.n.Rotate(Vector{contact.jnAcc, contact.jtAcc})
apply_impulses(arbiter.body_a, arbiter.body_b, contact.r1, contact.r2, j.Mult(dt_coef))
}
}
func (arbiter *Arbiter) ApplyImpulse() {
a := arbiter.body_a
b := arbiter.body_b
n := arbiter.n
surface_vr := arbiter.surface_vr
friction := arbiter.u
for i := 0; i < arbiter.count; i++ {
con := &arbiter.contacts[i]
nMass := con.nMass
r1 := con.r1
r2 := con.r2
vb1 := a.v_bias.Add(r1.Perp().Mult(a.w_bias))
vb2 := b.v_bias.Add(r2.Perp().Mult(b.w_bias))
vr := relative_velocity(a, b, r1, r2).Add(surface_vr)
vbn := vb2.Sub(vb1).Dot(n)
vrn := vr.Dot(n)
vrt := vr.Dot(n.Perp())
jbn := (con.bias - vbn) * nMass
jbnOld := con.jBias
con.jBias = math.Max(jbnOld+jbn, 0)
jn := -(con.bounce + vrn) * nMass
jnOld := con.jnAcc
con.jnAcc = math.Max(jnOld+jn, 0)
jtMax := friction * con.jnAcc
jt := -vrt * con.tMass
jtOld := con.jtAcc
con.jtAcc = Clamp(jtOld+jt, -jtMax, jtMax)
apply_bias_impulses(a, b, r1, r2, n.Mult(con.jBias-jbnOld))
apply_impulses(a, b, r1, r2, n.Rotate(Vector{con.jnAcc - jnOld, con.jtAcc - jtOld}))
}
}
func (arbiter *Arbiter) IsFirstContact() bool {
return arbiter.state == CP_ARBITER_STATE_FIRST_COLLISION
}
func (arb *Arbiter) PreStep(dt, slop, bias float64) {
a := arb.body_a
b := arb.body_b
n := arb.n
bodyDelta := b.p.Sub(a.p)
for i := 0; i < arb.count; i++ {
con := &arb.contacts[i]
// Calculate the mass normal and mass tangent.
con.nMass = 1.0 / k_scalar(a, b, con.r1, con.r2, n)
con.tMass = 1.0 / k_scalar(a, b, con.r1, con.r2, n.Perp())
// Calculate the target bias velocity.
dist := con.r2.Sub(con.r1).Add(bodyDelta).Dot(n)
con.bias = -bias * math.Min(0, dist+slop) / dt
con.jBias = 0.0
// Calculate the target bounce velocity.
con.bounce = normal_relative_velocity(a, b, con.r1, con.r2, n) * arb.e
}
}
func (arb *Arbiter) Update(info *CollisionInfo, space *Space) {
a := info.a
b := info.b
// For collisions between two similar primitive types, the order could have been swapped since the last frame.
arb.a = a
arb.body_a = a.body
arb.b = b
arb.body_b = b.body
// Iterate over the possible pairs to look for hash value matches.
for i := 0; i < info.count; i++ {
con := &info.arr[i]
// r1 and r2 store absolute offsets at init time.
// Need to convert them to relative offsets.
con.r1 = con.r1.Sub(a.body.p)
con.r2 = con.r2.Sub(b.body.p)
// Cached impulses are not zeroed at init time.
con.jnAcc = 0
con.jtAcc = 0
for j := 0; j < arb.count; j++ {
old := arb.contacts[j]
// This could trigger false positives, but is fairly unlikely nor serious if it does.
if con.hash == old.hash {
// Copy the persistent contact information.
con.jnAcc = old.jnAcc
con.jtAcc = old.jtAcc
}
}
}
arb.contacts = info.arr[:info.count]
arb.count = info.count
arb.n = info.n
arb.e = a.e * b.e
arb.u = a.u * b.u
surfaceVr := b.surfaceV.Sub(a.surfaceV)
arb.surface_vr = surfaceVr.Sub(info.n.Mult(surfaceVr.Dot(info.n)))
typeA := info.a.collisionType
typeB := info.b.collisionType
handler := space.LookupHandler(typeA, typeB, space.defaultHandler)
arb.handler = handler
// Check if the types match, but don't swap for a default handler which use the wildcard for type A.
swapped := typeA != handler.TypeA && handler.TypeA != WILDCARD_COLLISION_TYPE
arb.swapped = swapped
if handler != space.defaultHandler || space.usesWildcards {
// The order of the main handler swaps the wildcard handlers too. Uffda.
if swapped {
arb.handlerA = space.LookupHandler(typeB, WILDCARD_COLLISION_TYPE, &CollisionHandlerDoNothing)
arb.handlerB = space.LookupHandler(typeA, WILDCARD_COLLISION_TYPE, &CollisionHandlerDoNothing)
} else {
arb.handlerA = space.LookupHandler(typeA, WILDCARD_COLLISION_TYPE, &CollisionHandlerDoNothing)
arb.handlerB = space.LookupHandler(typeB, WILDCARD_COLLISION_TYPE, &CollisionHandlerDoNothing)
}
}
// mark it as new if it's been cached
if arb.state == CP_ARBITER_STATE_CACHED {
arb.state = CP_ARBITER_STATE_FIRST_COLLISION
}
}
// Ignore marks a collision pair to be ignored until the two objects separate.
//
// Pre-solve and post-solve callbacks will not be called, but the separate callback will be called.
func (arb *Arbiter) Ignore() bool {
arb.state = CP_ARBITER_STATE_IGNORE
return false
}
// CallWildcardBeginA if you want a custom callback to invoke the wildcard callback for the first collision type, you must call this function explicitly.
//
// You must decide how to handle the wildcard's return value since it may disagree with the other wildcard handler's return value or your own.
func (arb *Arbiter) CallWildcardBeginA(space *Space) bool {
handler := arb.handlerA
return handler.BeginFunc(arb, space, handler.UserData)
}
// CallWildcardBeginB If you want a custom callback to invoke the wildcard callback for the second collision type, you must call this function explicitly.
func (arb *Arbiter) CallWildcardBeginB(space *Space) bool {
handler := arb.handlerB
arb.swapped = !arb.swapped
retVal := handler.BeginFunc(arb, space, handler.UserData)
arb.swapped = !arb.swapped
return retVal
}
// CallWildcardPreSolveA If you want a custom callback to invoke the wildcard callback for the first collision type, you must call this function explicitly.
func (arb *Arbiter) CallWildcardPreSolveA(space *Space) bool {
handler := arb.handlerA
return handler.PreSolveFunc(arb, space, handler.UserData)
}
// CallWildcardPreSolveB If you want a custom callback to invoke the wildcard callback for the second collision type, you must call this function explicitly.
func (arb *Arbiter) CallWildcardPreSolveB(space *Space) bool {
handler := arb.handlerB
arb.swapped = !arb.swapped
retval := handler.PreSolveFunc(arb, space, handler.UserData)
arb.swapped = !arb.swapped
return retval
}
func (arb *Arbiter) CallWildcardPostSolveA(space *Space) {
handler := arb.handlerA
handler.PostSolveFunc(arb, space, handler.UserData)
}
func (arb *Arbiter) CallWildcardPostSolveB(space *Space) {
handler := arb.handlerB
arb.swapped = !arb.swapped
handler.PostSolveFunc(arb, space, handler.UserData)
arb.swapped = !arb.swapped
}
func (arb *Arbiter) CallWildcardSeparateA(space *Space) {
handler := arb.handlerA
handler.SeparateFunc(arb, space, handler.UserData)
}
func (arb *Arbiter) CallWildcardSeparateB(space *Space) {
handler := arb.handlerB
arb.swapped = !arb.swapped
handler.SeparateFunc(arb, space, handler.UserData)
arb.swapped = !arb.swapped
}
func apply_impulses(a, b *Body, r1, r2, j Vector) {
b.v.X += j.X * b.m_inv
b.v.Y += j.Y * b.m_inv
b.w += b.i_inv * (r2.X*j.Y - r2.Y*j.X)
j.X = -j.X
j.Y = -j.Y
a.v.X += j.X * a.m_inv
a.v.Y += j.Y * a.m_inv
a.w += a.i_inv * (r1.X*j.Y - r1.Y*j.X)
}
func apply_impulse(body *Body, j, r Vector) {
body.v.X += j.X * body.m_inv
body.v.Y += j.Y * body.m_inv
body.w += body.i_inv * r.Cross(j)
}
func apply_bias_impulses(a, b *Body, r1, r2, j Vector) {
b.v_bias.X += j.X * b.m_inv
b.v_bias.Y += j.Y * b.m_inv
b.w_bias += b.i_inv * (r2.X*j.Y - r2.Y*j.X)
j.X = -j.X
j.Y = -j.Y
a.v_bias.X += j.X * a.m_inv
a.v_bias.Y += j.Y * a.m_inv
a.w_bias += a.i_inv * (r1.X*j.Y - r1.Y*j.X)
}
func relative_velocity(a, b *Body, r1, r2 Vector) Vector {
return r2.Perp().Mult(b.w).Add(b.v).Sub(r1.Perp().Mult(a.w).Add(a.v))
}
var CollisionHandlerDoNothing = CollisionHandler{
WILDCARD_COLLISION_TYPE,
WILDCARD_COLLISION_TYPE,
AlwaysCollide,
AlwaysCollide,
DoNothing,
DoNothing,
nil,
}
var CollisionHandlerDefault = CollisionHandler{
WILDCARD_COLLISION_TYPE,
WILDCARD_COLLISION_TYPE,
DefaultBegin,
DefaultPreSolve,
DefaultPostSolve,
DefaultSeparate,
nil,
}
func AlwaysCollide(_ *Arbiter, _ *Space, _ interface{}) bool {
return true
}
func DoNothing(_ *Arbiter, _ *Space, _ interface{}) {
}
func DefaultBegin(arb *Arbiter, space *Space, _ interface{}) bool {
return arb.CallWildcardBeginA(space) && arb.CallWildcardBeginB(space)
}
func DefaultPreSolve(arb *Arbiter, space *Space, _ interface{}) bool {
return arb.CallWildcardPreSolveA(space) && arb.CallWildcardPreSolveB(space)
}
func DefaultPostSolve(arb *Arbiter, space *Space, _ interface{}) {
arb.CallWildcardPostSolveA(space)
arb.CallWildcardPostSolveB(space)
}
func DefaultSeparate(arb *Arbiter, space *Space, _ interface{}) {
arb.CallWildcardSeparateA(space)
arb.CallWildcardSeparateB(space)
}
// TotalImpulse calculates the total impulse including the friction that was applied by this arbiter.
//
// This function should only be called from a post-solve, post-step or EachArbiter callback.
func (arb *Arbiter) TotalImpulse() Vector {
var sum Vector
count := arb.Count()
for i := 0; i < count; i++ {
con := arb.contacts[i]
sum = sum.Add(arb.n.Rotate(Vector{con.jnAcc, con.jtAcc}))
}
if arb.swapped {
return sum
}
return sum.Neg()
}
func (arb *Arbiter) Count() int {
if arb.state < CP_ARBITER_STATE_CACHED {
return int(arb.count)
}
return 0
}
// Shapes return the colliding shapes involved for this arbiter.
// The order of their space.CollisionType values will match the order set when the collision handler was registered.
func (arb *Arbiter) Shapes() (*Shape, *Shape) {
if arb.swapped {
return arb.b, arb.a
} else {
return arb.a, arb.b
}
}
// Bodies returns the colliding bodies involved for this arbiter.
// The order of the space.CollisionType the bodies are associated with values will match the order set when the collision handler was registered.
func (arb *Arbiter) Bodies() (*Body, *Body) {
shapeA, shapeB := arb.Shapes()
return shapeA.body, shapeB.body
}
func (arb *Arbiter) Normal() Vector {
if arb.swapped {
return arb.n.Mult(-1)
} else {
return arb.n
}
}
// ContactPointSet wraps up the important collision data for an arbiter.
type ContactPointSet struct {
// Count is the number of contact points in the set.
Count int
// Normal is the normal of the collision.
Normal Vector
Points [MAX_CONTACTS_PER_ARBITER]struct {
// The position of the contact on the surface of each shape.
PointA, PointB Vector
// Distance is penetration distance of the two shapes. Overlapping means it will be negative.
//
// This value is calculated as p2.Sub(p1).Dot(n) and is ignored by Arbiter.SetContactPointSet().
Distance float64
}
}
// ContactPointSet returns ContactPointSet
func (arb *Arbiter) ContactPointSet() ContactPointSet {
var set ContactPointSet
set.Count = arb.Count()
swapped := arb.swapped
n := arb.n
if swapped {
set.Normal = n.Neg()
} else {
set.Normal = n
}
for i := 0; i < set.Count; i++ {
// Contact points are relative to body CoGs;
p1 := arb.body_a.p.Add(arb.contacts[i].r1)
p2 := arb.body_b.p.Add(arb.contacts[i].r2)
if swapped {
set.Points[i].PointA = p2
set.Points[i].PointB = p1
} else {
set.Points[i].PointA = p1
set.Points[i].PointB = p2
}
set.Points[i].Distance = p2.Sub(p1).Dot(n)
}
return set
}
// SetContactPointSet replaces the contact point set.
//
// This can be a very powerful feature, but use it with caution!
func (arb *Arbiter) SetContactPointSet(set *ContactPointSet) {
count := set.Count
assert(count == int(arb.count))
swapped := arb.swapped
if swapped {
arb.n = set.Normal.Neg()
} else {
arb.n = set.Normal
}
for i := 0; i < count; i++ {
p1 := set.Points[i].PointA
p2 := set.Points[i].PointB
if swapped {
arb.contacts[i].r1 = p2.Sub(arb.body_a.p)
arb.contacts[i].r2 = p1.Sub(arb.body_b.p)
} else {
arb.contacts[i].r1 = p1.Sub(arb.body_a.p)
arb.contacts[i].r2 = p2.Sub(arb.body_b.p)
}
}
}