-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstraint.go
108 lines (88 loc) · 1.88 KB
/
constraint.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
package cm
import (
"math"
)
type Constrainer interface {
PreStep(dt float64)
ApplyCachedImpulse(dtCoef float64)
ApplyImpulse(dt float64)
GetImpulse() float64
}
type ConstraintPreSolveFunc func(*Constraint, *Space)
type ConstraintPostSolveFunc func(*Constraint, *Space)
type Constraint struct {
Class Constrainer
space *Space
bodyA, bodyB *Body
nextA, nextB *Constraint
maxForce, errorBias, maxBias float64
collideBodies bool
PreSolve ConstraintPreSolveFunc
PostSolve ConstraintPostSolveFunc
UserData any
}
func NewConstraint(class Constrainer, a, b *Body) *Constraint {
return &Constraint{
Class: class,
bodyA: a,
bodyB: b,
space: nil,
maxForce: infinity,
errorBias: math.Pow(1.0-0.1, 60.0),
maxBias: infinity,
collideBodies: true,
PreSolve: nil,
PostSolve: nil,
}
}
func (c *Constraint) ActivateBodies() {
c.bodyA.Activate()
c.bodyB.Activate()
}
func (c *Constraint) BodyA() *Body {
return c.bodyA
}
func (c *Constraint) BodyB() *Body {
return c.bodyB
}
func (c Constraint) MaxForce() float64 {
return c.maxForce
}
func (c *Constraint) SetMaxForce(max float64) {
// if max < 0.0 {
// log.Fatalln("Must be positive")
// }
c.ActivateBodies()
c.maxForce = max
}
func (c Constraint) MaxBias() float64 {
return c.maxBias
}
func (c *Constraint) SetMaxBias(max float64) {
// if max < 0 {
// log.Fatalln("Must be positive")
// }
c.ActivateBodies()
c.maxBias = max
}
func (c Constraint) ErrorBias() float64 {
return c.errorBias
}
func (c *Constraint) SetErrorBias(errorBias float64) {
// if errorBias < 0 {
// log.Fatalln("Must be positive")
// }
c.ActivateBodies()
c.errorBias = errorBias
}
func (c *Constraint) Next(body *Body) *Constraint {
if c.bodyA == body {
return c.nextA
} else {
return c.nextB
}
}
func (c *Constraint) SetCollideBodies(collideBodies bool) {
c.ActivateBodies()
c.collideBodies = collideBodies
}