forked from jakecoffman/cp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
joints.go
225 lines (185 loc) · 7.65 KB
/
joints.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"math"
. "github.com/jakecoffman/cp"
"github.com/jakecoffman/cp/examples"
)
func main() {
space := NewSpace()
space.Iterations = 10
space.SetGravity(Vector{0, -100})
space.SleepTimeThreshold = 0.5
walls := []Vector{
{-320, 240}, {320, 240},
{-320, 120}, {320, 120},
{-320, 0}, {320, 0},
{-320, -120}, {320, -120},
{-320, -240}, {320, -240},
{-320, -240}, {-320, 240},
{-160, -240}, {-160, 240},
{0, -240}, {0, 240},
{160, -240}, {160, 240},
{320, -240}, {320, 240},
}
for i := 0; i < len(walls)-1; i += 2 {
shape := space.AddShape(NewSegment(space.StaticBody, walls[i], walls[i+1], 0))
shape.SetElasticity(1)
shape.SetFriction(1)
shape.SetFilter(examples.NotGrabbableFilter)
}
var boxOffset Vector
var body1, body2 *Body
posA := Vector{50, 60}
posB := Vector{110, 60}
// Pin Joints - Link shapes with a solid bar or pin.
// Keeps the anchor points the same distance apart from when the joint was created.
boxOffset = Vector{-320, -240}
body1 = addBall(space, posA, boxOffset)
body2 = addBall(space, posB, boxOffset)
space.AddConstraint(NewPinJoint(body1, body2, Vector{15, 0}, Vector{-15, 0}))
// Slide Joints - Like pin joints but with a min/max distance.
// Can be used for a cheap approximation of a rope.
boxOffset = Vector{-160, -240}
body1 = addBall(space, posA, boxOffset)
body2 = addBall(space, posB, boxOffset)
space.AddConstraint(NewSlideJoint(body1, body2, Vector{15, 0}, Vector{-15, 0}, 20, 40))
// Pivot Joints - Holds the two anchor points together. Like a swivel.
boxOffset = Vector{0, -240}
body1 = addBall(space, posA, boxOffset)
body2 = addBall(space, posB, boxOffset)
space.AddConstraint(NewPivotJoint(body1, body2, boxOffset.Add(Vector{80, 60})))
// cpPivotJointNew() takes it's anchor parameter in world coordinates. The anchors are calculated from that
// cpPivotJointNew2() lets you specify the two anchor points explicitly
// Groove Joints - Like a pivot joint, but one of the anchors is a line segment that the pivot can slide in
boxOffset = Vector{160, -240}
body1 = addBall(space, posA, boxOffset)
body2 = addBall(space, posB, boxOffset)
space.AddConstraint(NewGrooveJoint(body1, body2, Vector{30, 30}, Vector{30, -30}, Vector{-30, 0}))
// Damped Springs
boxOffset = Vector{-320, -120}
body1 = addBall(space, posA, boxOffset)
body2 = addBall(space, posB, boxOffset)
space.AddConstraint(NewDampedSpring(body1, body2, Vector{15, 0}, Vector{-15, 0}, 20, 5, 0.3))
// Damped Rotary Springs
boxOffset = Vector{-160, -120}
body1 = addBar(space, posA, boxOffset)
body2 = addBar(space, posB, boxOffset)
space.AddConstraint(NewPivotJoint(body1, space.StaticBody, boxOffset.Add(posA)))
space.AddConstraint(NewPivotJoint(body2, space.StaticBody, boxOffset.Add(posB)))
space.AddConstraint(NewDampedRotarySpring(body1, body2, 0, 3000, 60))
// Rotary Limit Joint
boxOffset = Vector{0, -120}
body1 = addLever(space, posA, boxOffset)
body2 = addLever(space, posB, boxOffset)
space.AddConstraint(NewPivotJoint(body1, space.StaticBody, boxOffset.Add(posA)))
space.AddConstraint(NewPivotJoint(body2, space.StaticBody, boxOffset.Add(posB)))
// Hold their rotation within 90 degrees of each other.
space.AddConstraint(NewRotaryLimitJoint(body1, body2, -math.Pi/2.0, math.Pi/2.0))
// Ratchet Joint - A rotary ratchet, like a socket wrench
boxOffset = Vector{160, -120}
body1 = addLever(space, posA, boxOffset)
body2 = addLever(space, posB, boxOffset)
space.AddConstraint(NewPivotJoint(body1, space.StaticBody, boxOffset.Add(posA)))
space.AddConstraint(NewPivotJoint(body2, space.StaticBody, boxOffset.Add(posB)))
// Ratchet every 90 degrees
space.AddConstraint(NewRatchetJoint(body1, body2, 0, math.Pi/2.0))
// Gear Joint - Maintain a specific angular velocity ratio
boxOffset = Vector{-320, 0}
body1 = addBar(space, posA, boxOffset)
body2 = addBar(space, posB, boxOffset)
space.AddConstraint(NewPivotJoint(body1, space.StaticBody, boxOffset.Add(posA)))
space.AddConstraint(NewPivotJoint(body2, space.StaticBody, boxOffset.Add(posB)))
// Force one to sping 2x as fast as the other
space.AddConstraint(NewGearJoint(body1, body2, 0, 2))
// Simple Motor - Maintain a specific angular relative velocity
boxOffset = Vector{-160, 0}
body1 = addBar(space, posA, boxOffset)
body2 = addBar(space, posB, boxOffset)
space.AddConstraint(NewPivotJoint(body1, space.StaticBody, boxOffset.Add(posA)))
space.AddConstraint(NewPivotJoint(body2, space.StaticBody, boxOffset.Add(posB)))
// Make them spin at 1/2 revolution per second in relation to each other.
space.AddConstraint(NewSimpleMotor(body1, body2, math.Pi))
// Make a car with some nice soft suspension
boxOffset = Vector{}
wheel1 := addWheel(space, posA, boxOffset)
wheel2 := addWheel(space, posB, boxOffset)
chassis := addChassis(space, Vector{80, 100}, boxOffset)
space.AddConstraint(NewGrooveJoint(chassis, wheel1, Vector{-30, -10}, Vector{-30, -40}, Vector{}))
space.AddConstraint(NewGrooveJoint(chassis, wheel2, Vector{30, -10}, Vector{30, -40}, Vector{}))
space.AddConstraint(NewDampedSpring(chassis, wheel1, Vector{-30, 0}, Vector{}, 50, 20, 10))
space.AddConstraint(NewDampedSpring(chassis, wheel2, Vector{30, 0}, Vector{}, 50, 20, 10))
examples.Main(space, 1.0/60.0, update, examples.DefaultDraw)
}
func addBall(space *Space, pos, boxOffset Vector) *Body {
radius := 15.0
mass := 1.0
body := space.AddBody(NewBody(mass, MomentForCircle(mass, 0, radius, Vector{})))
body.SetPosition(pos.Add(boxOffset))
shape := space.AddShape(NewCircle(body, radius, Vector{}))
shape.SetElasticity(0)
shape.SetFriction(0.7)
return body
}
func addLever(space *Space, pos, boxOffset Vector) *Body {
mass := 1.0
a := Vector{0, 15}
b := Vector{0, -15}
body := space.AddBody(NewBody(mass, MomentForSegment(mass, a, b, 0)))
body.SetPosition(pos.Add(boxOffset.Add(Vector{0, -15})))
shape := space.AddShape(NewSegment(body, a, b, 5))
shape.SetElasticity(0)
shape.SetFriction(0.7)
return body
}
func addBar(space *Space, pos, boxOffset Vector) *Body {
mass := 2.0
a := Vector{0, 30}
b := Vector{0, -30}
body := space.AddBody(NewBody(mass, MomentForSegment(mass, a, b, 0)))
body.SetPosition(pos.Add(boxOffset))
shape := space.AddShape(NewSegment(body, a, b, 5))
shape.SetElasticity(0)
shape.SetFriction(0.7)
shape.SetFilter(NewShapeFilter(1, ALL_CATEGORIES, ALL_CATEGORIES))
return body
}
func addWheel(space *Space, pos, boxOffset Vector) *Body {
radius := 15.0
mass := 1.0
body := space.AddBody(NewBody(mass, MomentForCircle(mass, 0, radius, Vector{})))
body.SetPosition(pos.Add(boxOffset))
shape := space.AddShape(NewCircle(body, radius, Vector{}))
shape.SetElasticity(0)
shape.SetFriction(0.7)
shape.SetFilter(NewShapeFilter(1, ALL_CATEGORIES, ALL_CATEGORIES))
return body
}
func addChassis(space *Space, pos, boxOffset Vector) *Body {
mass := 5.0
width := 80.0
height := 30.0
body := space.AddBody(NewBody(mass, MomentForBox(mass, width, height)))
body.SetPosition(pos.Add(boxOffset))
shape := space.AddShape(NewBox(body, width, height, 0))
shape.SetElasticity(0)
shape.SetFriction(0.7)
shape.SetFilter(NewShapeFilter(1, ALL_CATEGORIES, ALL_CATEGORIES))
return body
}
func update(space *Space, dt float64) {
draw(Vector{-320, -240}, "Pin Joints")
draw(Vector{-160, -240}, "Slide Joints")
draw(Vector{0, -240}, "Pivot Joints")
draw(Vector{160, -240}, "Groove Joints")
draw(Vector{-320, -120}, "Damped Spring")
draw(Vector{-160, -120}, "Damped Rotary Spring")
draw(Vector{0, -120}, "Rotary Limit Joint")
draw(Vector{160, -120}, "Ratchet Joints")
draw(Vector{-320, 0}, "Gear Joint")
draw(Vector{-160, 0}, "Simple Motor")
draw(Vector{0, 0}, "Car")
space.Step(dt)
}
func draw(v Vector, words string) {
examples.DrawString(Vector{v.X + 10, v.Y + 105}, words)
}