-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_motor.go
57 lines (42 loc) · 1.07 KB
/
simple_motor.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 cm
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.bodyA
b := motor.bodyB
// moment of inertia coefficient
motor.iSum = 1.0 / (a.momentOfInertiaInverse + b.momentOfInertiaInverse)
}
func (motor *SimpleMotor) ApplyCachedImpulse(dtCoef float64) {
a := motor.bodyA
b := motor.bodyB
j := motor.jAcc * dtCoef
a.w -= j * a.momentOfInertiaInverse
b.w += j * b.momentOfInertiaInverse
}
func (motor *SimpleMotor) ApplyImpulse(dt float64) {
a := motor.bodyA
b := motor.bodyB
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.momentOfInertiaInverse
b.w += j * b.momentOfInertiaInverse
}
func (motor *SimpleMotor) GetImpulse() float64 {
return math.Abs(motor.jAcc)
}