forked from jakecoffman/cp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
constraint.go
94 lines (74 loc) · 1.7 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
package cp
import "math"
type Constrainer interface {
PreStep(dt float64)
ApplyCachedImpulse(dt_coef float64)
ApplyImpulse(dt float64)
GetImpulse() float64
}
type ConstraintPreSolveFunc func(*Constraint, *Space)
type ConstraintPostSolveFunc func(*Constraint, *Space)
type Constraint struct {
Class Constrainer
space *Space
a, b *Body
next_a, next_b *Constraint
maxForce, errorBias, maxBias float64
collideBodies bool
PreSolve ConstraintPreSolveFunc
PostSolve ConstraintPostSolveFunc
userData interface{}
}
func NewConstraint(class Constrainer, a, b *Body) *Constraint {
return &Constraint{
Class: class,
a: a,
b: 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.a.Activate()
c.b.Activate()
}
func (c Constraint) MaxForce() float64 {
return c.maxForce
}
func (c *Constraint) SetMaxForce(max float64) {
assert(max >= 0.0, "Must be positive")
c.ActivateBodies()
c.maxForce = max
}
func (c Constraint) MaxBias() float64 {
return c.maxBias
}
func (c *Constraint) SetMaxBias(max float64) {
assert(max >= 0, "Must be positive")
c.ActivateBodies()
c.maxBias = max
}
func (c Constraint) ErrorBias() float64 {
return c.errorBias
}
func (c *Constraint) SetErrorBias(errorBias float64) {
assert(errorBias >= 0, "Must be positive")
c.ActivateBodies()
c.errorBias = errorBias
}
func (c *Constraint) Next(body *Body) *Constraint {
if c.a == body {
return c.next_a
} else {
return c.next_b
}
}
func (c *Constraint) SetCollideBodies(collideBodies bool) {
c.ActivateBodies()
c.collideBodies = collideBodies
}