-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidrawer.go
193 lines (159 loc) · 4.66 KB
/
idrawer.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
package cm
import (
"fmt"
"github.com/setanarut/vec"
)
// Draw flags
const (
DrawShapes = 1 << 0
DrawConstraints = 1 << 1
DrawCollisionPoints = 1 << 2
)
// 16 bytes
type FColor struct {
R, G, B, A float32
}
type IDrawer interface {
DrawCircle(pos vec.Vec2, angle, radius float64, outline, fill FColor, data any)
DrawSegment(a, b vec.Vec2, fill FColor, data any)
DrawFatSegment(a, b vec.Vec2, radius float64, outline, fill FColor, data any)
DrawPolygon(count int, verts []vec.Vec2, radius float64, outline, fill FColor, data any)
DrawDot(size float64, pos vec.Vec2, fill FColor, data any)
Flags() uint
OutlineColor() FColor
ShapeColor(shape *Shape, data any) FColor
ConstraintColor() FColor
CollisionPointColor() FColor
Data() any
}
// DrawShape draws shapes with the drawer implementation
func DrawShape(shape *Shape, drawer IDrawer) {
body := shape.Body
data := drawer.Data()
outline := drawer.OutlineColor()
fill := drawer.ShapeColor(shape, data)
switch shape.Class.(type) {
case *Circle:
circle := shape.Class.(*Circle)
drawer.DrawCircle(circle.transformC, body.angle, circle.radius, outline, fill, data)
case *Segment:
seg := shape.Class.(*Segment)
drawer.DrawFatSegment(seg.transformA, seg.transformB, seg.radius, outline, fill, data)
case *PolyShape:
poly := shape.Class.(*PolyShape)
count := poly.count
planes := poly.Planes
verts := make([]vec.Vec2, count)
for i := 0; i < count; i++ {
verts[i] = planes[i].V0
}
drawer.DrawPolygon(count, verts, poly.Radius, outline, fill, data)
default:
panic("Unknown shape type")
}
}
var springVerts = []vec.Vec2{
{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},
}
// DrawConstraint draws constraints with the drawer implementation
func DrawConstraint(constraint *Constraint, drawer IDrawer) {
data := drawer.Data()
color := drawer.ConstraintColor()
bodyA := constraint.bodyA
bodyB := constraint.bodyB
switch constraint.Class.(type) {
case *PinJoint:
joint := constraint.Class.(*PinJoint)
a := bodyA.transform.Apply(joint.AnchorA)
b := bodyB.transform.Apply(joint.AnchorB)
drawer.DrawDot(5, a, color, data)
drawer.DrawDot(5, b, color, data)
drawer.DrawSegment(a, b, color, data)
case *SlideJoint:
joint := constraint.Class.(*SlideJoint)
a := bodyA.transform.Apply(joint.AnchorA)
b := bodyB.transform.Apply(joint.AnchorB)
drawer.DrawDot(5, a, color, data)
drawer.DrawDot(5, b, color, data)
drawer.DrawSegment(a, b, color, data)
case *PivotJoint:
joint := constraint.Class.(*PivotJoint)
a := bodyA.transform.Apply(joint.AnchorA)
b := bodyB.transform.Apply(joint.AnchorB)
drawer.DrawDot(5, a, color, data)
drawer.DrawDot(5, b, color, data)
case *GrooveJoint:
joint := constraint.Class.(*GrooveJoint)
a := bodyA.transform.Apply(joint.GrooveA)
b := bodyA.transform.Apply(joint.GrooveB)
c := bodyB.transform.Apply(joint.AnchorB)
drawer.DrawDot(5, c, color, data)
drawer.DrawSegment(a, b, color, data)
case *DampedSpring:
spring := constraint.Class.(*DampedSpring)
a := bodyA.transform.Apply(spring.AnchorA)
b := bodyB.transform.Apply(spring.AnchorB)
drawer.DrawDot(5, a, color, data)
drawer.DrawDot(5, b, color, data)
delta := b.Sub(a)
cos := delta.X
sin := delta.Y
s := 1.0 / delta.Mag()
r1 := vec.Vec2{cos, -sin * s}
r2 := vec.Vec2{sin, cos * s}
verts := []vec.Vec2{}
for i := 0; i < len(springVerts); i++ {
v := springVerts[i]
verts = append(verts, vec.Vec2{v.Dot(r1) + a.X, v.Dot(r2) + a.Y})
}
for i := 0; i < len(springVerts)-1; i++ {
drawer.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))
}
}
// DrawSpace draws all shapes in space with the drawer implementation
func DrawSpace(space *Space, drawer IDrawer) {
space.dynamicShapes.class.Each(func(obj *Shape) {
DrawShape(obj, drawer)
})
space.staticShapes.class.Each(func(obj *Shape) {
DrawShape(obj, drawer)
})
for _, constraint := range space.constraints {
DrawConstraint(constraint, drawer)
}
drawSeg := drawer.DrawSegment
data := drawer.Data()
for _, arb := range space.Arbiters {
n := arb.normal
for j := 0; j < arb.count; j++ {
p1 := arb.bodyA.position.Add(arb.Contacts[j].R1)
p2 := arb.bodyB.position.Add(arb.Contacts[j].R2)
a := p1.Add(n.Scale(-2))
b := p2.Add(n.Scale(2))
drawSeg(a, b, drawer.CollisionPointColor(), data)
}
}
}