forked from jakecoffman/cp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.go
188 lines (154 loc) · 4.46 KB
/
draw.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
package cp
import "fmt"
//Draw flags
const (
DRAW_SHAPES = 1 << 0
DRAW_CONSTRAINTS = 1 << 1
DRAW_COLLISION_POINTS = 1 << 2
)
// 16 bytes
type FColor struct {
R, G, B, A float32
}
type Drawer interface {
DrawCircle(pos Vector, angle, radius float64, outline, fill FColor, data interface{})
DrawSegment(a, b Vector, fill FColor, data interface{})
DrawFatSegment(a, b Vector, radius float64, outline, fill FColor, data interface{})
DrawPolygon(count int, verts []Vector, radius float64, outline, fill FColor, data interface{})
DrawDot(size float64, pos Vector, fill FColor, data interface{})
Flags() uint
OutlineColor() FColor
ShapeColor(shape *Shape, data interface{}) FColor
ConstraintColor() FColor
CollisionPointColor() FColor
Data() interface{}
}
func DrawShape(shape *Shape, options Drawer) {
body := shape.body
data := options.Data()
outline := options.OutlineColor()
fill := options.ShapeColor(shape, data)
switch shape.Class.(type) {
case *Circle:
circle := shape.Class.(*Circle)
options.DrawCircle(circle.tc, body.a, circle.r, outline, fill, data)
case *Segment:
seg := shape.Class.(*Segment)
options.DrawFatSegment(seg.ta, seg.tb, seg.r, outline, fill, data)
case *PolyShape:
poly := shape.Class.(*PolyShape)
count := poly.count
planes := poly.planes
verts := make([]Vector, count)
for i := 0; i < count; i++ {
verts[i] = planes[i].v0
}
options.DrawPolygon(count, verts, poly.r, outline, fill, data)
default:
panic("Unknown shape type")
}
}
var springVerts = []Vector{
{0.00, 0.0},
{0.20, 0.0},
{0.25, 3.0},
{0.30, -6.0},
{0.35, 6.0},
{0.40, -6.0},
{0.45, 6.0},
{0.50, -6.0},
{0.55, 6.0},
{0.60, -6.0},
{0.65, 6.0},
{0.70, -3.0},
{0.75, 6.0},
{0.80, 0.0},
{1.00, 0.0},
}
func DrawConstraint(constraint *Constraint, options Drawer) {
data := options.Data()
color := options.ConstraintColor()
body_a := constraint.a
body_b := constraint.b
switch constraint.Class.(type) {
case *PinJoint:
joint := constraint.Class.(*PinJoint)
a := body_a.transform.Point(joint.AnchorA)
b := body_b.transform.Point(joint.AnchorB)
options.DrawDot(5, a, color, data)
options.DrawDot(5, b, color, data)
options.DrawSegment(a, b, color, data)
case *SlideJoint:
joint := constraint.Class.(*SlideJoint)
a := body_a.transform.Point(joint.AnchorA)
b := body_b.transform.Point(joint.AnchorB)
options.DrawDot(5, a, color, data)
options.DrawDot(5, b, color, data)
options.DrawSegment(a, b, color, data)
case *PivotJoint:
joint := constraint.Class.(*PivotJoint)
a := body_a.transform.Point(joint.AnchorA)
b := body_b.transform.Point(joint.AnchorB)
options.DrawDot(5, a, color, data)
options.DrawDot(5, b, color, data)
case *GrooveJoint:
joint := constraint.Class.(*GrooveJoint)
a := body_a.transform.Point(joint.GrooveA)
b := body_a.transform.Point(joint.GrooveB)
c := body_b.transform.Point(joint.AnchorB)
options.DrawDot(5, c, color, data)
options.DrawSegment(a, b, color, data)
case *DampedSpring:
spring := constraint.Class.(*DampedSpring)
a := body_a.transform.Point(spring.AnchorA)
b := body_b.transform.Point(spring.AnchorB)
options.DrawDot(5, a, color, data)
options.DrawDot(5, b, color, data)
delta := b.Sub(a)
cos := delta.X
sin := delta.Y
s := 1.0 / delta.Length()
r1 := Vector{cos, -sin * s}
r2 := Vector{sin, cos * s}
verts := []Vector{}
for i := 0; i < len(springVerts); i++ {
v := springVerts[i]
verts = append(verts, Vector{v.Dot(r1) + a.X, v.Dot(r2) + a.Y})
}
for i := 0; i < len(springVerts)-1; i++ {
options.DrawSegment(verts[i], verts[i+1], color, data)
}
// these aren't drawn in Chipmunk, so they aren't drawn here
case *GearJoint:
case *SimpleMotor:
case *DampedRotarySpring:
case *RotaryLimitJoint:
case *RatchetJoint:
default:
panic(fmt.Sprintf("Implement me: %#v", constraint.Class))
}
return
}
func DrawSpace(space *Space, options Drawer) {
space.dynamicShapes.class.Each(func(obj *Shape) {
DrawShape(obj, options)
})
space.staticShapes.class.Each(func(obj *Shape) {
DrawShape(obj, options)
})
for _, constraint := range space.constraints {
DrawConstraint(constraint, options)
}
drawSeg := options.DrawSegment
data := options.Data()
for _, arb := range space.arbiters {
n := arb.n
for j := 0; j < arb.count; j++ {
p1 := arb.body_a.p.Add(arb.contacts[j].r1)
p2 := arb.body_b.p.Add(arb.contacts[j].r2)
a := p1.Add(n.Mult(-2))
b := p2.Add(n.Mult(2))
drawSeg(a, b, options.CollisionPointColor(), data)
}
}
}