-
Notifications
You must be signed in to change notification settings - Fork 20
/
simplemotor.go
57 lines (42 loc) · 972 Bytes
/
simplemotor.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
package cp
import "math"
type SimpleMotor struct {
*Constraint
Rate float64
iSum, jAcc float64
}
func NewSimpleMotor(a, b *Body, rate float64) *Constraint {
motor := &SimpleMotor{
Rate: rate,
}
motor.Constraint = NewConstraint(motor, a, b)
return motor.Constraint
}
func (motor *SimpleMotor) PreStep(dt float64) {
a := motor.a
b := motor.b
// moment of inertia coefficient
motor.iSum = 1.0 / (a.i_inv + b.i_inv)
}
func (motor *SimpleMotor) ApplyCachedImpulse(dt_coef float64) {
a := motor.a
b := motor.b
j := motor.jAcc * dt_coef
a.w -= j * a.i_inv
b.w += j * b.i_inv
}
func (motor *SimpleMotor) ApplyImpulse(dt float64) {
a := motor.a
b := motor.b
wr := b.w - a.w + motor.Rate
jMax := motor.maxForce * dt
j := -wr * motor.iSum
jOld := motor.jAcc
motor.jAcc = Clamp(jOld+j, -jMax, jMax)
j = motor.jAcc - jOld
a.w -= j * a.i_inv
b.w += j * b.i_inv
}
func (motor *SimpleMotor) GetImpulse() float64 {
return math.Abs(motor.jAcc)
}