-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsketchy.lua
493 lines (426 loc) · 13.6 KB
/
sketchy.lua
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
local lg = love.graphics
local math = math
-- shape types
local shapeMethodByType = {
chain = 'drawChainShape',
circle = 'drawCircleShape',
edge = 'drawEdgeShape',
polygon = 'drawPolygonShape',
}
local function drawCircleShape (self, shape, fixture)
local body = fixture:getBody()
local x, y = body:getWorldPoint(shape:getPoint())
local r = shape:getRadius()
lg.circle('fill', x, y, r, math.max(10, r * self.scale))
end
local function drawPolygonShape (self, shape, fixture)
local body = fixture:getBody()
lg.polygon('fill', body:getWorldPoints(shape:getPoints()))
end
local function drawChainShape (self, shape, fixture)
if self.drawnObjects[fixture] then return end
self.drawnObjects[fixture] = true
local body = fixture:getBody()
local p = { body:getWorldPoints(shape:getPoints()) }
for i = 1, #p - 2, 2 do
lg.line(p[i], p[i + 1], p[i + 2], p[i + 3])
end
end
local function drawEdgeShape (self, shape, fixture)
local body = fixture:getBody()
lg.line(body:getWorldPoints(shape:getPoints()))
end
-- joint types
local jointMethodByType = {
distance = 'drawDistanceJoint',
friction = 'drawFrictionJoint',
gear = 'drawGearJoint',
motor = 'drawMotorJoint',
mouse = 'drawMouseJoint',
prismatic = 'drawPrismaticJoint',
pulley = 'drawPulleyJoint',
revolute = 'drawRevoluteJoint',
rope = 'drawRopeJoint',
weld = 'drawWeldJoint',
wheel = 'drawWheelJoint',
}
local function drawDistanceJoint (self, joint)
local x1, y1, x2, y2 = joint:getAnchors()
lg.setColor(self.distanceJointColor)
lg.line(x1, y1, x2, y2)
lg.points(x1, y1, x2, y2)
end
local function drawFrictionJoint (self, joint)
local x1, y1, x2, y2 = joint:getAnchors()
lg.setColor(self.frictionJointColor)
lg.line(x1, y1, x2, y2)
lg.points(x1, y1, x2, y2)
end
local function drawGearJoint (self, joint)
-- not yet implemented
end
local function drawMotorJoint (self, joint)
local x1, y1, x2, y2 = joint:getAnchors()
lg.setColor(self.motorJointColor)
lg.line(x1, y1, x2, y2)
lg.points(x1, y1, x2, y2)
end
local function drawMouseJoint (self, joint)
local x1, y1, x2, y2 = joint:getAnchors()
lg.setColor(self.mouseJointColor)
lg.line(x1, y1, x2, y2)
lg.points(x1, y1, x2, y2)
end
local function drawPrismaticJoint (self, joint)
local x1, y1, x2, y2 = joint:getAnchors()
lg.setColor(self.prismaticJointColor)
lg.line(x1, y1, x2, y2)
lg.points(x1, y1, x2, y2)
end
local function drawPulleyJoint (self, joint)
local x1, y1, x2, y2 = joint:getAnchors()
local gx1, gy1, gx2, gy2 = joint:getGroundAnchors( )
lg.setColor(self.pulleyJointColor)
lg.line(x1, y1, gx1, gy1)
lg.line(gx1, gy1, gx2, gy2)
lg.line(gx2, gy2, x2, y2)
lg.points(x1, y1, x2, y2)
end
local function drawRevoluteJoint (self, joint)
local x1, y1, x2, y2 = joint:getAnchors()
lg.setColor(self.revoluteJointColor)
lg.line(x1, y1, x2, y2)
lg.points(x1, y1, x2, y2)
end
local function drawRopeJoint (self, joint)
local x1, y1, x2, y2 = joint:getAnchors()
lg.setColor(self.ropeJointColor)
lg.line(x1, y1, x2, y2)
lg.points(x1, y1, x2, y2)
end
local function drawWeldJoint (self, joint)
local x1, y1, x2, y2 = joint:getAnchors()
lg.setColor(self.weldJointColor)
lg.line(x1, y1, x2, y2)
lg.points(x1, y1, x2, y2)
end
local function drawWheelJoint (self, joint)
local x1, y1, x2, y2 = joint:getAnchors()
lg.setColor(self.wheelJointColor)
lg.line(x1, y1, x2, y2)
lg.points(x1, y1, x2, y2)
end
-- base types
local function drawBodyJoints (self, body)
local joints = body:getJointList()
for i = 1, #joints do
self:presentJoint(joints[i])
end
end
local function drawBodyContacts (self, body)
local contacts = body:getContactList()
lg.setColor(self.contactIndicatorColor)
for i = 1, #contacts do
self:drawContact(contacts[i])
end
end
local function drawBodyAngle (self, body)
lg.setColor(self.angleIndicatorColor)
local angle = body:getAngle()
local size = self.angleIndicatorSize / self.scale
local vx = math.cos(angle) * size
local vy = math.sin(angle) * size
local sharp = self.angleIndicatorSharpness
local x, y = body:getWorldCenter()
lg.polygon('fill',
x - vy / sharp, y + vx / sharp,
x + vx, y + vy,
x + vy / sharp, y - vx / sharp)
end
local function drawContact (self, contact)
if self.drawnObjects[contact] then return end
self.drawnObjects[contact] = true
local x1, y1, x2, y2 = contact:getPositions()
local size = self.contactIndicatorSize / self.scale
local segments = self.contactIndicatorSegments
if x1 then
lg.circle('fill', x1, y1, size, segments)
end
if x2 then
lg.circle('fill', x2, y2, size, segments)
end
end
local function drawFixture (self, fixture)
local body = fixture:getBody()
local bodyType = body:getType()
if bodyType == 'static' then
lg.setColor(self.staticFixtureColor)
elseif bodyType == 'kinematic' then
lg.setColor(self.kinematicFixtureColor)
elseif body:isAwake() then
lg.setColor(self.dynamicFixtureColor)
else
lg.setColor(self.sleepingFixtureColor)
end
self:drawShape(fixture:getShape(), fixture)
end
local function drawShape (self, shape, fixture)
local f = self[shapeMethodByType[shape:getType()]]
if f then
f(self, shape, fixture)
end
end
local function drawJoint (self, joint)
local f = self[jointMethodByType[joint:getType()]]
if f then
f(self, joint)
end
end
local function presentBody (self, body)
-- presenter
local present = self:getPresenter(body)
if present then
local finished = present(body)
if finished then return finished end
end
self:drawBodyJoints(body)
self:drawBodyContacts(body)
self:drawBodyAngle(body)
end
local function presentFixture (self, fixture)
local present = self:getPresenter(fixture)
if present then
local finished = present(fixture)
if finished then return finished end
end
self:drawFixture(fixture)
end
local function presentJoint (self, joint)
if self.drawnObjects[joint] then return end
self.drawnObjects[joint] = true
local present = self:getPresenter(joint)
if present then
local finished = present(joint)
if finished then return finished end
end
self:drawJoint(joint)
end
local function screenToWorld (self, px, py)
local x, y = self.left, self.top
local w, h, s = self.width * 0.5, self.height * 0.5, self.scale
local cx, cy = self.cameraX, self.cameraY
local sin, cos = math.sin(-self.angle), math.cos(-self.angle)
px, py = px - (x + w), py - (y + h)
px, py = cos * px - sin * py, sin * px + cos * py
px, py = px + cx * s, py + cy * s
px, py = px / s, py / s
return px, py
end
local function worldToScreen (self, px, py)
local x, y = self.left, self.top
local w, h, s = self.width * 0.5, self.height * 0.5, self.scale
local cx, cy = self.cameraX, self.cameraY
local sin, cos = math.sin(-self.angle), math.cos(-self.angle)
px, py = px * s, py * s
px, py = px - cx * s, py - cy * s
px, py = cos * px + sin * py, sin * -px + cos * py
px, py = px + (x + w), py + (y + h)
return px, py
end
-- Returns point components of the aabb
-- enclosing the circle circumscribing the input aabb.
local function expandAABB (x1, y1, x2, y2)
local w, h = x2 - x1, y2 - y1
local cx, cy = x1 + w * 0.5, y1 + h * 0.5
local r = 0.5 * math.sqrt(w * w + h * h)
return cx - r, cy - r, cx + r, cy + r
end
local function drawWorld (self, world)
local x, y = self.left, self.top
local w, h, s = self.width * 0.5, self.height * 0.5, self.scale
local ws, hs = w / s, h / s
local cx, cy = self.cameraX, self.cameraY
lg.push('all')
lg.setLineWidth(self.jointLineWidth / self.scale)
lg.setPointSize(self.jointPointSize)
-- translate center of viewport to origin; rotate to angle
lg.translate(x + w, y + h)
lg.rotate(self.angle)
-- translate camera position to center of viewport; scale
lg.translate(-cx * s, -cy * s)
lg.scale(s, s)
-- draw any fixtures intersected by viewport
local relatedBodies = {}
local function draw (fixture)
local body = fixture:getBody()
if not relatedBodies[body] then
local i = #relatedBodies + 1
relatedBodies[i] = body
relatedBodies[body] = i
end
self:presentFixture(fixture)
return true
end
local x1, y1, x2, y2 = expandAABB(cx - ws, cy - hs, cx + ws, cy + hs)
world:queryBoundingBox(x1, y1, x2, y2, draw)
-- draw bodies connected to the fixtures
-- (joints, contacts, angle indicators)
for i = 1, #relatedBodies do
self:presentBody(relatedBodies[i])
end
-- paranoia
relatedBodies = nil
draw = nil
lg.pop()
end
-- api
local function draw (self, thing)
self.drawnObjects = {}
if thing.getBodyList then
return self:drawWorld(thing)
end
if thing.getFixtureList then
return self:presentBody(thing)
end
end
local function setAngle (self, angle)
self.angle = angle
return self
end
local function getAngle (self)
return self.angle
end
local function setCamera (self, x, y)
self.cameraX, self.cameraY = x or 0, y or 0
return self
end
local function getCamera (self)
return self.cameraX, self.cameraY
end
local function setViewport (self, left, top, width, height)
self.left, self.top, self.width, self.height = left, top, width, height
return self
end
local function getViewport (self)
return self.left, self.top, self.width, self.height
end
local function setScale (self, scale)
self.scale = scale
return self
end
local function getScale (self)
return self.scale
end
local function setPresenter (self, object, presenter)
local data = object:getUserData()
if data == nil then
data = {}
object:setUserData(data)
end
data.sketchyPresenter = presenter
return self
end
local function getPresenter (self, object)
local data = object:getUserData()
if type(data) == 'table' then
return data.sketchyPresenter
end
end
local function snapScreenPoint (self, x, y, factor)
x, y = self:screenToWorld(x, y)
x, y = self:snapWorldPoint(x, y, factor)
return self:worldToScreen(x, y)
end
local function snapWorldPoint (self, x, y, factor)
factor = factor or 1
x = math.floor(x / factor + 0.5) * factor
y = math.floor(y / factor + 0.5) * factor
return x, y
end
return function ()
return {
-- internal
drawnObjects = {},
-- misc. colors and sizes
angleIndicatorColor = { 255, 255, 255, 64 },
angleIndicatorSize = 12,
angleIndicatorSharpness = 3,
contactIndicatorColor = { 255, 0, 0 },
contactIndicatorSize = 4,
contactIndicatorSegments = 8,
jointLineWidth = 2,
jointPointSize = 4,
-- fixture colors
staticFixtureColor = { 255, 0, 0, 128 },
kinematicFixtureColor = { 128, 0, 128, 128 },
dynamicFixtureColor = { 0, 0, 255, 128 },
sleepingFixtureColor = { 0, 0, 128, 128 },
-- joint colors
distanceJointColor = { 0, 255, 0, 128 },
frictionJointColor = { 255, 0, 128, 128 },
gearJointColor = { 1, 1, 1, 128 },
motorJointColor = { 0, 160, 160, 128 },
mouseJointColor = { 0, 255, 255, 128 },
prismaticJointColor = { 128, 192, 160, 128 },
pulleyJointColor = { 0, 128, 255, 128 },
revoluteJointColor = { 128, 255, 0, 128 },
ropeJointColor = { 128, 128, 0, 128 },
weldJointColor = { 255, 128, 0, 128 },
wheelJointColor = { 128, 160, 128, 128 },
-- initial values
left = 0,
top = 0,
width = 800,
height = 600,
scale = 1,
angle = 0,
cameraX = 0,
cameraY = 0,
-- base types
drawBodyAngle = drawBodyAngle,
drawBodyContacts = drawBodyContacts,
drawBodyJoints = drawBodyJoints,
drawContact = drawContact,
drawFixture = drawFixture,
drawJoint = drawJoint,
drawShape = drawShape,
drawWorld = drawWorld,
-- shape types
drawChainShape = drawChainShape,
drawCircleShape = drawCircleShape,
drawEdgeShape = drawEdgeShape,
drawPolygonShape = drawPolygonShape,
-- joint types
drawDistanceJoint = drawDistanceJoint,
drawFrictionJoint = drawFrictionJoint,
drawGearJoint = drawGearJoint,
drawMotorJoint = drawMotorJoint,
drawMouseJoint = drawMouseJoint,
drawPrismaticJoint = drawPrismaticJoint,
drawPulleyJoint = drawPulleyJoint,
drawRevoluteJoint = drawRevoluteJoint,
drawRopeJoint = drawRopeJoint,
drawWeldJoint = drawWeldJoint,
drawWheelJoint = drawWheelJoint,
-- presenter hooks
presentBody = presentBody,
presentFixture = presentFixture,
presentJoint = presentJoint,
-- api
draw = draw,
setAngle = setAngle,
getAngle = getAngle,
setCamera = setCamera,
getCamera = getCamera,
setPresenter = setPresenter,
getPresenter = getPresenter,
setScale = setScale,
getScale = getScale,
setViewport = setViewport,
getViewport = getViewport,
screenToWorld = screenToWorld,
worldToScreen = worldToScreen,
snapScreenPoint = snapScreenPoint,
snapWorldPoint = snapWorldPoint,
}
end