forked from jakecoffman/cp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
body.go
574 lines (467 loc) · 11.9 KB
/
body.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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
package cp
import (
"fmt"
"math"
)
/// Rigid body velocity update function type.
type BodyVelocityFunc func(body *Body, gravity Vector, damping float64, dt float64)
/// Rigid body position update function type.
type BodyPositionFunc func(body *Body, dt float64)
type Body struct {
id int
// Integration functions
velocity_func BodyVelocityFunc
position_func BodyPositionFunc
// mass and it's inverse
m float64
m_inv float64
// moment of inertia and it's inverse
i float64
i_inv float64
// center of gravity
cog Vector
// position, velocity, force
p Vector
v Vector
f Vector
// Angle, angular velocity, torque (radians)
a float64
w float64
t float64
transform Transform
UserData interface{}
// "pseudo-velocities" used for eliminating overlap.
// Erin Catto has some papers that talk about what these are.
v_bias Vector
w_bias float64
space *Space
shapeList []*Shape
arbiterList *Arbiter
constraintList *Constraint
sleepingRoot *Body
sleepingNext *Body
sleepingIdleTime float64
}
func (b Body) String() string {
return fmt.Sprint("Body ", b.id)
}
var bodyCur int = 0
func NewBody(mass, moment float64) *Body {
body := &Body{
id: bodyCur,
cog: Vector{},
p: Vector{},
v: Vector{},
f: Vector{},
v_bias: Vector{},
transform: NewTransformIdentity(),
velocity_func: BodyUpdateVelocity,
position_func: BodyUpdatePosition,
}
bodyCur++
body.SetMass(mass)
body.SetMoment(moment)
body.SetAngle(0)
return body
}
func NewStaticBody() *Body {
body := NewBody(0, 0)
body.SetType(BODY_STATIC)
return body
}
func NewKinematicBody() *Body {
body := NewBody(0, 0)
body.SetType(BODY_KINEMATIC)
return body
}
func (body *Body) SetAngle(angle float64) {
body.Activate()
body.a = angle
body.SetTransform(body.p, angle)
}
func (body Body) Moment() float64 {
return body.i
}
func (body *Body) SetMoment(moment float64) {
body.Activate()
body.i = moment
body.i_inv = 1 / moment
}
func (body *Body) Mass() float64 {
return body.m
}
func (body *Body) SetMass(mass float64) {
body.Activate()
body.m = mass
body.m_inv = 1 / mass
}
// body types
const (
BODY_DYNAMIC = iota
BODY_KINEMATIC
BODY_STATIC
)
func (body *Body) IdleTime() float64 {
return body.sleepingIdleTime
}
func (body *Body) SetType(newType int) {
oldType := body.GetType()
if oldType == newType {
return
}
if newType == BODY_STATIC {
body.sleepingIdleTime = INFINITY
} else {
body.sleepingIdleTime = 0
}
if newType == BODY_DYNAMIC {
body.m = 0
body.i = 0
body.m_inv = INFINITY
body.i_inv = INFINITY
body.AccumulateMassFromShapes()
} else {
body.m = INFINITY
body.i = INFINITY
body.m_inv = 0
body.i_inv = 0
body.v = Vector{}
body.w = 0
}
// If the body is added to a space already, we'll need to update some space data structures.
if body.space == nil {
return
}
assert(body.space.locked == 0, "Space is locked")
if oldType != BODY_STATIC {
body.Activate()
}
if oldType == BODY_STATIC {
for i, b := range body.space.staticBodies {
if b == body {
body.space.staticBodies = append(body.space.staticBodies[:i], body.space.staticBodies[i+1:]...)
break
}
}
body.space.dynamicBodies = append(body.space.dynamicBodies, body)
} else if newType == BODY_STATIC {
for i, b := range body.space.dynamicBodies {
if b == body {
body.space.dynamicBodies = append(body.space.dynamicBodies[:i], body.space.dynamicBodies[i+1:]...)
break
}
}
body.space.staticBodies = append(body.space.staticBodies, body)
}
var fromIndex, toIndex *SpatialIndex
if oldType == BODY_STATIC {
fromIndex = body.space.staticShapes
} else {
fromIndex = body.space.dynamicShapes
}
if newType == BODY_STATIC {
toIndex = body.space.staticShapes
} else {
toIndex = body.space.dynamicShapes
}
if oldType != newType {
for _, shape := range body.shapeList {
fromIndex.class.Remove(shape, shape.hashid)
toIndex.class.Insert(shape, shape.hashid)
}
}
}
func (body *Body) GetType() int {
if body.sleepingIdleTime == INFINITY {
return BODY_STATIC
}
if body.m == INFINITY {
return BODY_KINEMATIC
}
return BODY_DYNAMIC
}
// Should *only* be called when shapes with mass info are modified, added or removed.
func (body *Body) AccumulateMassFromShapes() {
if body == nil || body.GetType() != BODY_DYNAMIC {
return
}
body.m = 0
body.i = 0
body.cog = Vector{}
// cache position, realign at the end
pos := body.Position()
for _, shape := range body.shapeList {
info := shape.MassInfo()
m := info.m
if info.m > 0 {
msum := body.m + m
body.i += m*info.i + body.cog.DistanceSq(info.cog)*(m*body.m)/msum
body.cog = body.cog.Lerp(info.cog, m/msum)
body.m = msum
}
}
body.m_inv = 1.0 / body.m
body.i_inv = 1.0 / body.i
body.SetPosition(pos)
}
func (body Body) CenterOfGravity() Vector {
return body.cog
}
func (body *Body) Angle() float64 {
return body.a
}
func (body *Body) Rotation() Vector {
return Vector{body.transform.a, body.transform.b}
}
func (body *Body) Position() Vector {
return body.transform.Point(Vector{})
}
func (body *Body) SetPosition(position Vector) {
body.Activate()
body.p = body.transform.Vect(body.cog).Add(position)
body.SetTransform(body.p, body.a)
}
func (body *Body) Velocity() Vector {
return body.v
}
func (body *Body) SetVelocity(x, y float64) {
body.Activate()
body.v = Vector{x, y}
}
func (body *Body) SetVelocityVector(v Vector) {
body.Activate()
body.v = v
}
func (body *Body) UpdateVelocity(gravity Vector, damping, dt float64) {
if body.GetType() == BODY_KINEMATIC {
return
}
assert(body.m > 0 && body.i > 0, "Body's mass and moment must be positive")
body.v = body.v.Mult(damping).Add(gravity.Add(body.f.Mult(body.m_inv)).Mult(dt))
body.w = body.w*damping + body.t*body.i_inv*dt
body.f = Vector{}
body.t = 0
}
func (body *Body) Force() Vector {
return body.f
}
func (body *Body) SetForce(force Vector) {
body.Activate()
body.f = force
}
func (body *Body) AngularVelocity() float64 {
return body.w
}
func (body *Body) SetAngularVelocity(angularVelocity float64) {
body.Activate()
body.w = angularVelocity
}
func (body *Body) SetTransform(p Vector, a float64) {
rot := Vector{math.Cos(a), math.Sin(a)}
c := body.cog
body.transform = NewTransformTranspose(
rot.X, -rot.Y, p.X-(c.X*rot.X-c.Y*rot.Y),
rot.Y, rot.X, p.Y-(c.X*rot.Y+c.Y*rot.X),
)
}
func (body *Body) Activate() {
if !(body != nil && body.GetType() == BODY_DYNAMIC) {
return
}
body.sleepingIdleTime = 0
root := body.ComponentRoot()
if root != nil && root.IsSleeping() {
assert(root.GetType() == BODY_DYNAMIC, "Non-dynamic root")
space := root.space
// in the chipmunk code they shadow body, so here I am not
bodyToo := root
for bodyToo != nil {
next := bodyToo.sleepingNext
bodyToo.sleepingIdleTime = 0
bodyToo.sleepingRoot = nil
bodyToo.sleepingNext = nil
space.Activate(bodyToo)
bodyToo = next
}
for i := 0; i < len(space.sleepingComponents); i++ {
if space.sleepingComponents[i] == root {
space.sleepingComponents = append(space.sleepingComponents[:i], space.sleepingComponents[i+1:]...)
break
}
}
}
for arbiter := body.arbiterList; arbiter != nil; arbiter = arbiter.Next(body) {
// Reset the idle timer of things the body is touching as well.
// That way things don't get left hanging in the air.
var other *Body
if arbiter.body_a == body {
other = arbiter.body_b
} else {
other = arbiter.body_a
}
if other.GetType() != BODY_STATIC {
other.sleepingIdleTime = 0
}
}
}
func (body *Body) ActivateStatic(filter *Shape) {
assert(body.GetType() == BODY_STATIC)
for arb := body.arbiterList; arb != nil; arb = arb.Next(body) {
if filter == nil || filter == arb.a || filter == arb.b {
if arb.body_a == body {
arb.body_b.Activate()
} else {
arb.body_a.Activate()
}
}
}
}
func (body *Body) IsSleeping() bool {
return body.sleepingRoot != nil
}
func (body *Body) AddShape(shape *Shape) *Shape {
body.shapeList = append(body.shapeList, shape)
if shape.MassInfo().m > 0 {
body.AccumulateMassFromShapes()
}
return shape
}
func (body *Body) KineticEnergy() float64 {
// Need to do some fudging to avoid NaNs
vsq := body.v.Dot(body.v)
wsq := body.w * body.w
var a, b float64
if vsq != 0 {
a = vsq * body.m
}
if wsq != 0 {
b = wsq * body.i
}
return a + b
}
func (body *Body) PushArbiter(arb *Arbiter) {
next := body.arbiterList
arb.ThreadForBody(body).next = next
if next != nil {
next.ThreadForBody(body).prev = arb
}
body.arbiterList = arb
}
func (root *Body) ComponentAdd(body *Body) {
body.sleepingRoot = root
if body != root {
body.sleepingNext = root.sleepingNext
root.sleepingNext = body
}
}
func (body *Body) ComponentRoot() *Body {
if body != nil {
return body.sleepingRoot
}
return nil
}
func (body *Body) WorldToLocal(point Vector) Vector {
return NewTransformRigidInverse(body.transform).Point(point)
}
func (body *Body) LocalToWorld(point Vector) Vector {
return body.transform.Point(point)
}
func (body *Body) ApplyForceAtWorldPoint(force, point Vector) {
body.Activate()
body.f = body.f.Add(force)
r := point.Sub(body.transform.Point(body.cog))
body.t += r.Cross(force)
}
func (body *Body) ApplyForceAtLocalPoint(force, point Vector) {
body.ApplyForceAtWorldPoint(body.transform.Vect(force), body.transform.Point(point))
}
func (body *Body) ApplyImpulseAtWorldPoint(impulse, point Vector) {
body.Activate()
r := point.Sub(body.transform.Point(body.cog))
apply_impulse(body, impulse, r)
}
func (body *Body) ApplyImpulseAtLocalPoint(impulse, point Vector) {
body.ApplyImpulseAtWorldPoint(body.transform.Vect(impulse), body.transform.Point(point))
}
func (body *Body) VelocityAtLocalPoint(point Vector) Vector {
r := body.transform.Vect(point.Sub(body.cog))
return body.v.Add(r.Perp().Mult(body.w))
}
func (body *Body) VelocityAtWorldPoint(point Vector) Vector {
r := point.Sub(body.transform.Point(body.cog))
return body.v.Add(r.Perp().Mult(body.w))
}
func BodyUpdateVelocity(body *Body, gravity Vector, damping, dt float64) {
if body.GetType() == BODY_KINEMATIC {
return
}
body.v = body.v.Mult(damping).Add(gravity.Add(body.f.Mult(body.m_inv)).Mult(dt))
body.w = body.w*damping + body.t*body.i_inv*dt
body.f = Vector{}
body.t = 0
}
func BodyUpdatePosition(body *Body, dt float64) {
body.p = body.p.Add(body.v.Add(body.v_bias).Mult(dt))
body.a = body.a + (body.w+body.w_bias)*dt
body.SetTransform(body.p, body.a)
body.v_bias = Vector{}
body.w_bias = 0
}
func (body *Body) RemoveConstraint(constraint *Constraint) {
body.constraintList = filterConstraints(body.constraintList, body, constraint)
}
func (body *Body) RemoveShape(shape *Shape) {
for i, s := range body.shapeList {
if s == shape {
// leak-free delete from slice
last := len(body.shapeList) - 1
body.shapeList[i] = body.shapeList[last]
body.shapeList[last] = nil
body.shapeList = body.shapeList[:last]
break
}
}
if body.GetType() == BODY_DYNAMIC && shape.massInfo.m > 0 {
body.AccumulateMassFromShapes()
}
}
func filterConstraints(node *Constraint, body *Body, filter *Constraint) *Constraint {
if node == filter {
return node.Next(body)
} else if node.a == body {
node.next_a = filterConstraints(node.next_a, body, filter)
} else {
node.next_b = filterConstraints(node.next_b, body, filter)
}
return node
}
func (body *Body) SetVelocityUpdateFunc(f BodyVelocityFunc) {
body.velocity_func = f
}
func (body *Body) SetPositionUpdateFunc(f BodyPositionFunc) {
body.position_func = f
}
func (body *Body) EachArbiter(f func(*Arbiter)) {
arb := body.arbiterList
for arb != nil {
next := arb.Next(body)
swapped := arb.swapped
arb.swapped = body == arb.body_b
f(arb)
arb.swapped = swapped
arb = next
}
}
func (body *Body) EachShape(f func(*Shape)) {
for i := 0; i < len(body.shapeList); i++ {
f(body.shapeList[i])
}
}
func (body *Body) EachConstraint(f func(*Constraint)) {
constraint := body.constraintList
for constraint != nil {
next := constraint.Next(body)
f(constraint)
constraint = next
}
}