forked from jakecoffman/cp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pump.go
127 lines (97 loc) · 3.35 KB
/
pump.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
package main
import (
"math"
. "github.com/jakecoffman/cp"
"github.com/jakecoffman/cp/examples"
)
const numBalls = 5
var motor *SimpleMotor
var balls [numBalls]*Body
func main() {
space := NewSpace()
space.SetGravity(Vector{0, -600})
walls := []Vector{
{-256, 16}, {-256, 300},
{-256, 16}, {-192, 0},
{-192, 0}, {-192, -64},
{-128, -64}, {-122, 144},
{-192, 80}, {-192, 176},
{-192, 176}, {-128, 240},
{-128, 144}, {192, 64},
}
var shape *Shape
for i := 0; i < len(walls)-1; i += 2 {
shape = space.AddShape(NewSegment(space.StaticBody, walls[i], walls[i+1], 2))
shape.SetElasticity(0)
shape.SetFriction(0.5)
shape.SetFilter(examples.NotGrabbableFilter)
}
verts := []Vector{
{-30, -80},
{-30, 80},
{30, 64},
{30, -80},
}
plunger := space.AddBody(NewBody(1, INFINITY))
plunger.SetPosition(Vector{-160, -80})
shape = space.AddShape(NewPolyShape(plunger, len(verts), verts, NewTransformIdentity(), 0))
shape.SetElasticity(1)
shape.SetFriction(0.5)
shape.SetFilter(NewShapeFilter(NO_GROUP, 1, 1))
for i := 0; i < numBalls; i++ {
balls[i] = addBall(space, Vector{-224 + float64(i), 80 + 64*float64(i)})
}
smallGear := space.AddBody(NewBody(10, MomentForCircle(10, 80, 0, Vector{})))
smallGear.SetPosition(Vector{-160, -160})
smallGear.SetAngle(-math.Pi / 2)
shape = space.AddShape(NewCircle(smallGear, 80, Vector{}))
shape.SetFilter(SHAPE_FILTER_NONE)
space.AddConstraint(NewPivotJoint2(space.StaticBody, smallGear, Vector{-160, -160}, Vector{}))
bigGear := space.AddBody(NewBody(40, MomentForCircle(40, 160, 0, Vector{})))
bigGear.SetPosition(Vector{80, -160})
bigGear.SetAngle(math.Pi / 2)
shape = space.AddShape(NewCircle(bigGear, 160, Vector{}))
shape.SetFilter(SHAPE_FILTER_NONE)
space.AddConstraint(NewPivotJoint2(space.StaticBody, bigGear, Vector{80, -160}, Vector{}))
space.AddConstraint(NewPinJoint(smallGear, plunger, Vector{80, 0}, Vector{}))
space.AddConstraint(NewGearJoint(smallGear, bigGear, -math.Pi/2, -2))
bottom := -300.0
top := 32.0
feeder := space.AddBody(NewBody(1, MomentForSegment(1, Vector{-224, bottom}, Vector{-224, top}, 0)))
feeder.SetPosition(Vector{-224, (bottom + top) / 2})
length := top - bottom
shape = space.AddShape(NewSegment(feeder, Vector{0, length / 2}, Vector{0, -length / 2}, 20))
shape.SetFilter(examples.GrabFilter)
space.AddConstraint(NewPivotJoint2(space.StaticBody, feeder, Vector{-224, bottom}, Vector{0, -length / 2}))
anchr := feeder.WorldToLocal(Vector{-224, -160})
space.AddConstraint(NewPinJoint(feeder, smallGear, anchr, Vector{0, 80}))
motor = space.AddConstraint(NewSimpleMotor(space.StaticBody, bigGear, 3)).Class.(*SimpleMotor)
examples.Main(space, 1.0/120.0, update, examples.DefaultDraw)
}
func update(space *Space, dt float64) {
coef := (2.0 + examples.Keyboard.Y) / 3
rate := examples.Keyboard.X * 30 * coef
motor.Rate = rate
if rate != 0 {
motor.SetMaxForce(1000000)
} else {
motor.SetMaxForce(0)
}
space.Step(dt)
for i := 0; i < numBalls; i++ {
ball := balls[i]
pos := ball.Position()
if pos.X > 320 {
ball.SetVelocityVector(Vector{})
ball.SetPosition(Vector{-224, 200})
}
}
}
func addBall(space *Space, pos Vector) *Body {
body := space.AddBody(NewBody(1, MomentForCircle(1, 30, 0, Vector{})))
body.SetPosition(pos)
shape := space.AddShape(NewCircle(body, 30, Vector{}))
shape.SetElasticity(0)
shape.SetFriction(0.5)
return body
}