-
Notifications
You must be signed in to change notification settings - Fork 2
/
DynamicsB2Joint.go
484 lines (379 loc) · 9.98 KB
/
DynamicsB2Joint.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
package box2d
var B2JointType = struct {
E_unknownJoint uint8
E_revoluteJoint uint8
E_prismaticJoint uint8
E_distanceJoint uint8
E_pulleyJoint uint8
E_mouseJoint uint8
E_gearJoint uint8
E_wheelJoint uint8
E_weldJoint uint8
E_frictionJoint uint8
E_motorJoint uint8
}{
E_unknownJoint: 1,
E_revoluteJoint: 2,
E_prismaticJoint: 3,
E_distanceJoint: 4,
E_pulleyJoint: 5,
E_mouseJoint: 6,
E_gearJoint: 7,
E_wheelJoint: 8,
E_weldJoint: 9,
E_frictionJoint: 10,
E_motorJoint: 11,
}
type B2Jacobian struct {
Linear B2Vec2
AngularA float64
AngularB float64
}
// A joint edge is used to connect bodies and joints together
// in a joint graph where each body is a node and each joint
// is an edge. A joint edge belongs to a doubly linked list
// maintained in each attached body. Each joint has two joint
// nodes, one for each attached body.
type B2JointEdge struct {
Other *B2Body // provides quick access to the other body attached.
Joint B2JointInterface // the joint; backed by pointer
Prev *B2JointEdge // the previous joint edge in the body's joint list
Next *B2JointEdge // the next joint edge in the body's joint list
}
// Joint definitions are used to construct joints.
type B2JointDef struct {
// The joint type is set automatically for concrete joint types.
Type uint8
// Use this to attach application specific data to your joints.
UserData interface{}
// The first attached body.
BodyA *B2Body
// The second attached body.
BodyB *B2Body
// Set this flag to true if the attached bodies should collide.
CollideConnected bool
}
type B2JointDefInterface interface {
GetType() uint8
SetType(t uint8)
GetUserData() interface{}
SetUserData(userdata interface{})
GetBodyA() *B2Body
SetBodyA(body *B2Body)
GetBodyB() *B2Body
SetBodyB(body *B2Body)
IsCollideConnected() bool
SetCollideConnected(flag bool)
}
// Implementing B2JointDefInterface on B2Joint (used as a base struct)
func (def B2JointDef) GetType() uint8 {
return def.Type
}
func (def *B2JointDef) SetType(t uint8) {
def.Type = t
}
func (def B2JointDef) GetUserData() interface{} {
return def.UserData
}
func (def *B2JointDef) SetUserData(userdata interface{}) {
def.UserData = userdata
}
func (def B2JointDef) GetBodyA() *B2Body {
return def.BodyA
}
func (def *B2JointDef) SetBodyA(body *B2Body) {
def.BodyA = body
}
func (def B2JointDef) GetBodyB() *B2Body {
return def.BodyB
}
func (def *B2JointDef) SetBodyB(body *B2Body) {
def.BodyB = body
}
func (def B2JointDef) IsCollideConnected() bool {
return def.CollideConnected
}
func (def *B2JointDef) SetCollideConnected(flag bool) {
def.CollideConnected = flag
}
func MakeB2JointDef() B2JointDef {
res := B2JointDef{}
res.Type = B2JointType.E_unknownJoint
res.UserData = nil
res.BodyA = nil
res.BodyB = nil
res.CollideConnected = false
return res
}
// Utility to compute linear stiffness values from frequency and damping ratio
func B2LinearStiffness(stiffness *float64, damping *float64, frequencyHertz float64, dampingRatio float64, bodyA *B2Body, bodyB *B2Body) {
massA := bodyA.GetMass()
massB := bodyB.GetMass()
var mass float64
if massA > 0.0 && massB > 0.0 {
mass = massA * massB / (massA + massB)
} else if massA > 0.0 {
mass = massA
} else {
mass = massB
}
omega := 2.0 * B2_pi * frequencyHertz
*stiffness = mass * omega * omega
*damping = 2.0 * mass * dampingRatio * omega
}
// Utility to compute rotational stiffness values frequency and damping ratio
func B2AngularStiffness(stiffness *float64, damping *float64, frequencyHertz float64, dampingRatio float64, bodyA *B2Body, bodyB *B2Body) {
IA := bodyA.GetInertia()
IB := bodyB.GetInertia()
var I float64
if IA > 0.0 && IB > 0.0 {
I = IA * IB / (IA + IB)
} else if IA > 0.0 {
I = IA
} else {
I = IB
}
omega := 2.0 * B2_pi * frequencyHertz
*stiffness = I * omega * omega
*damping = 2.0 * I * dampingRatio * omega
}
// The base joint class. Joints are used to constraint two bodies together in
// various fashions. Some joints also feature limits and motors.
type B2Joint struct {
M_type uint8
M_prev B2JointInterface // has to be backed by pointer
M_next B2JointInterface // has to be backed by pointer
M_edgeA *B2JointEdge
M_edgeB *B2JointEdge
M_bodyA *B2Body
M_bodyB *B2Body
M_index int
M_islandFlag bool
M_collideConnected bool
M_userData interface{}
}
// Dump this joint to the log file.
func (j B2Joint) Dump() {}
// Shift the origin for any points stored in world coordinates.
func (j B2Joint) ShiftOrigin(newOrigin B2Vec2) {}
func (j B2Joint) GetType() uint8 {
return j.M_type
}
// @goadd
func (j *B2Joint) SetType(t uint8) {
j.M_type = t
}
func (j B2Joint) GetBodyA() *B2Body {
return j.M_bodyA
}
// @goadd
func (j *B2Joint) SetBodyA(body *B2Body) {
j.M_bodyA = body
}
func (j B2Joint) GetBodyB() *B2Body {
return j.M_bodyB
}
// @goadd
func (j *B2Joint) SetBodyB(body *B2Body) {
j.M_bodyB = body
}
func (j B2Joint) GetNext() B2JointInterface { // returns pointer
return j.M_next
}
// @goadd
func (j *B2Joint) SetNext(next B2JointInterface) { // has to be backed by pointer
j.M_next = next
}
func (j B2Joint) GetPrev() B2JointInterface { // returns pointer
return j.M_prev
}
// @goadd
func (j *B2Joint) SetPrev(prev B2JointInterface) { // prev has to be backed by pointer
j.M_prev = prev
}
func (j B2Joint) GetUserData() interface{} {
return j.M_userData
}
func (j *B2Joint) SetUserData(data interface{}) {
j.M_userData = data
}
func (j B2Joint) IsCollideConnected() bool {
return j.M_collideConnected
}
// @goadd
func (j *B2Joint) SetCollideConnected(flag bool) {
j.M_collideConnected = flag
}
// @goadd
func (j B2Joint) GetEdgeA() *B2JointEdge {
return j.M_edgeA
}
// @goadd
func (j *B2Joint) SetEdgeA(edge *B2JointEdge) {
j.M_edgeA = edge
}
// @goadd
func (j B2Joint) GetEdgeB() *B2JointEdge {
return j.M_edgeB
}
// @goadd
func (j *B2Joint) SetEdgeB(edge *B2JointEdge) {
j.M_edgeB = edge
}
func B2JointCreate(def B2JointDefInterface) B2JointInterface { // def should be back by pointer; a pointer is returned
var joint *B2Joint = nil
switch def.GetType() {
case B2JointType.E_distanceJoint:
{
if typeddef, ok := def.(*B2DistanceJointDef); ok {
return MakeB2DistanceJoint(typeddef)
}
B2Assert(false)
}
case B2JointType.E_mouseJoint:
{
if typeddef, ok := def.(*B2MouseJointDef); ok {
return MakeB2MouseJoint(typeddef)
}
B2Assert(false)
}
case B2JointType.E_prismaticJoint:
{
if typeddef, ok := def.(*B2PrismaticJointDef); ok {
return MakeB2PrismaticJoint(typeddef)
}
B2Assert(false)
}
case B2JointType.E_revoluteJoint:
{
if typeddef, ok := def.(*B2RevoluteJointDef); ok {
return MakeB2RevoluteJoint(typeddef)
}
B2Assert(false)
}
case B2JointType.E_pulleyJoint:
{
if typeddef, ok := def.(*B2PulleyJointDef); ok {
return MakeB2PulleyJoint(typeddef)
}
B2Assert(false)
}
case B2JointType.E_gearJoint:
{
if typeddef, ok := def.(*B2GearJointDef); ok {
return MakeB2GearJoint(typeddef)
}
B2Assert(false)
}
case B2JointType.E_wheelJoint:
{
if typeddef, ok := def.(*B2WheelJointDef); ok {
return MakeB2WheelJoint(typeddef)
}
B2Assert(false)
}
case B2JointType.E_weldJoint:
{
if typeddef, ok := def.(*B2WeldJointDef); ok {
return MakeB2WeldJoint(typeddef)
}
B2Assert(false)
}
case B2JointType.E_frictionJoint:
{
if typeddef, ok := def.(*B2FrictionJointDef); ok {
return MakeB2FrictionJoint(typeddef)
}
B2Assert(false)
}
case B2JointType.E_motorJoint:
{
if typeddef, ok := def.(*B2MotorJointDef); ok {
return MakeB2MotorJoint(typeddef)
}
B2Assert(false)
}
default:
B2Assert(false)
break
}
return joint
}
func B2JointDestroy(joint B2JointInterface) { // has to be backed by pointer
joint.Destroy()
}
func MakeB2Joint(def B2JointDefInterface) *B2Joint { // def has to be backed by pointer
B2Assert(def.GetBodyA() != def.GetBodyB())
res := B2Joint{}
res.M_type = def.GetType()
res.M_prev = nil
res.M_next = nil
res.M_bodyA = def.GetBodyA()
res.M_bodyB = def.GetBodyB()
res.M_index = 0
res.M_collideConnected = def.IsCollideConnected()
res.M_islandFlag = false
res.M_userData = def.GetUserData()
res.M_edgeA = &B2JointEdge{}
res.M_edgeB = &B2JointEdge{}
return &res
}
// Short-cut function to determine if either body is enabled.
func (j B2Joint) IsEnabled() bool {
return j.M_bodyA.IsEnabled() && j.M_bodyB.IsEnabled()
}
// @goadd
func (j *B2Joint) Destroy() {
}
// @goadd
func (j B2Joint) GetIndex() int {
return j.M_index
}
func (j *B2Joint) SetIndex(index int) {
j.M_index = index
}
func (j *B2Joint) InitVelocityConstraints(data B2SolverData) {}
func (j *B2Joint) SolveVelocityConstraints(data B2SolverData) {}
func (j *B2Joint) SolvePositionConstraints(data B2SolverData) bool {
return false
}
func (j B2Joint) GetIslandFlag() bool {
return j.M_islandFlag
}
func (j *B2Joint) SetIslandFlag(flag bool) {
j.M_islandFlag = flag
}
type B2JointInterface interface {
// Dump this joint to the log file.
Dump()
// Shift the origin for any points stored in world coordinates.
ShiftOrigin(newOrigin B2Vec2)
GetType() uint8
SetType(t uint8)
GetBodyA() *B2Body
SetBodyA(body *B2Body)
GetBodyB() *B2Body
SetBodyB(body *B2Body)
GetIndex() int
SetIndex(index int)
GetNext() B2JointInterface // backed by pointer
SetNext(next B2JointInterface) // backed by pointer
GetPrev() B2JointInterface // backed by pointer
SetPrev(prev B2JointInterface) // backed by pointer
GetEdgeA() *B2JointEdge
SetEdgeA(edge *B2JointEdge)
GetEdgeB() *B2JointEdge
SetEdgeB(edge *B2JointEdge)
GetUserData() interface{}
SetUserData(data interface{})
IsCollideConnected() bool
SetCollideConnected(flag bool)
IsEnabled() bool
//@goadd
Destroy()
InitVelocityConstraints(data B2SolverData)
SolveVelocityConstraints(data B2SolverData)
SolvePositionConstraints(data B2SolverData) bool
GetIslandFlag() bool
SetIslandFlag(flag bool)
}