forked from jakecoffman/cp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arbiter.go
494 lines (398 loc) · 11.7 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
package cp
import "math"
var WILDCARD_COLLISION_TYPE CollisionType = ^CollisionType(0)
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
}
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
}
}
func (arb *Arbiter) Ignore() bool {
arb.state = CP_ARBITER_STATE_IGNORE
return false
}
func (arb *Arbiter) CallWildcardBeginA(space *Space) bool {
handler := arb.handlerA
return handler.BeginFunc(arb, space, handler.UserData)
}
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
}
func (arb *Arbiter) CallWildcardPreSolveA(space *Space) bool {
handler := arb.handlerA
return handler.PreSolveFunc(arb, space, handler.UserData)
}
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) {
apply_impulse(a, j.Neg(), r1)
apply_impulse(b, j, r2)
}
func apply_bias_impulses(a, b *Body, r1, r2, j Vector) {
apply_bias_impulse(a, j.Neg(), r1)
apply_bias_impulse(b, j, r2)
}
func apply_bias_impulse(body *Body, j, r Vector) {
body.v_bias = body.v_bias.Add(j.Mult(body.m_inv))
body.w_bias += body.i_inv * r.Cross(j)
}
func apply_impulse(body *Body, j, r Vector) {
body.v = body.v.Add(j.Mult(body.m_inv))
body.w += body.i_inv * r.Cross(j)
}
func relative_velocity(a, b *Body, r1, r2 Vector) Vector {
v1_sum := a.v.Add(r1.Perp().Mult(a.w))
v2_sum := b.v.Add(r2.Perp().Mult(b.w))
return v2_sum.Sub(v1_sum)
}
var CollisionHandlerDoNothing CollisionHandler = CollisionHandler{
WILDCARD_COLLISION_TYPE,
WILDCARD_COLLISION_TYPE,
AlwaysCollide,
AlwaysCollide,
DoNothing,
DoNothing,
nil,
}
var CollisionHandlerDefault CollisionHandler = 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)
}
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
}
func (arb *Arbiter) Shapes() (*Shape, *Shape) {
if arb.swapped {
return arb.b, arb.a
} else {
return arb.a, arb.b
}
}
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
}
}
type ContactPointSet struct {
Count int
Normal Vector
Points [MAX_CONTACTS_PER_ARBITER]struct {
/// The position of the contact on the surface of each shape.
PointA, PointB Vector
/// Penetration distance of the two shapes. Overlapping means it will be negative.
/// This value is calculated as cpvdot(cpvsub(point2, point1), normal) and is ignored by cpArbiterSetContactPointSet().
Distance float64
}
}
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
}
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)
}
}
}