/
collider.nim
374 lines (300 loc) · 9.2 KB
/
collider.nim
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
import
sdl,
common, mask
type
# Base class (virtual)
PCollider* = ref TCollider
TCollider* = object of TOBject
# Point
PPointCollider* = ref TPointCollider
TPointCollider* = object of TCollider
x, y: int16
# Box
PBoxCollider* = ref TBoxCollider
TBoxCollider* = object of TCollider
left, right, top, bottom: int16
# Circle
PCircleCollider* = ref TCircleCollider
TCircleCollider* = object of TCollider
centerX, centerY: int16
radius: UInt16
# Mask
PMaskCollider* = ref TMaskCollider
TMaskCollider* = object of TCollider
x, y: int16
mask*: PMask
# PointCollider
proc init*(obj: PPointCollider, x, y: int16) =
obj.x = x
obj.y = y
proc newPointCollider*(pos: TPoint): PPointCollider =
new(result)
init(result, pos.x, pos.y)
proc newPointCollider*(x, y: int): PPointCollider =
new(result)
init(result, int16(x), int16(y))
# BoxCollider
proc init*(obj: PBoxCollider, x, y: int16, w, h: UInt16) =
obj.left = x
obj.right = x + w
obj.top = y
obj.bottom = y + h
proc newBoxCollider*(area: TRect): PBoxCollider =
new(result)
init(result, area.x, area.y, area.w, area.h)
proc newBoxCollider*(x, y, w, h: int): PBoxCollider =
new(result)
init(result, int16(x), int16(y), UInt16(w), UInt16(h))
proc newBoxCollider*(pos: TPoint, w, h: int): PBoxCollider =
new(result)
init(result, pos.x, pos.y, UInt16(w), UInt16(h))
# CircleCollider
proc init*(obj: PCircleCollider, x, y: int16, r: UInt16) =
obj.centerX = x
obj.centerY = y
obj.radius = r
proc newCircleCollider*(area: TCircle): PCircleCollider =
new(result)
init(result, area.x, area.y, area.r)
proc newCircleCollider*(x, y, r: int): PCircleCollider =
new(result)
init(result, int16(x), int16(y), UInt16(r))
proc newCircleCollider*(pos: TPoint, r: int): PCircleCollider =
new(result)
init(result, pos.x, pos.y, UInt16(r))
# MaskCollider
proc init*(obj: PMaskCollider, mask: PMask, x, y: int16) =
obj.mask = mask
obj.x = x
obj.y = y
proc newMaskCollider*(mask: PMask, x, y: int): PMaskCollider =
new(result)
init(result, mask, int16(x), int16(y))
proc newMaskCollider*(mask: PMask, pos: TPoint): PMaskCollider =
new(result)
init(result, mask, pos.x, pos.y)
# methods
# virtual methods
method x*(obj: PCollider): int16 {.inline.} = nil
method `x=`*(obj: PCollider, value: int16) {.inline.} = nil
method y*(obj: PCollider): int16 {.inline.} = nil
method `y=`*(obj: PCollider, value: int16) {.inline.} = nil
method collide*(a: PCollider, b: PCollider): bool = nil
# Point
method x*(obj: PPointCollider): int16 {.inline.} = return obj.x
method `x=`*(obj: PPointCollider, value: int16) {.inline.} =
obj.x = value
method y*(obj: PPointCollider): int16 {.inline.} = return obj.y
method `y=`*(obj: PPointCollider, value: int16) {.inline.} =
obj.y = value
# Box
method x*(obj: PBoxCollider): int16 {.inline.} = return obj.left
method `x=`*(obj: PBoxCollider, value: int16) {.inline.} =
let dx = value - obj.left
obj.left += dx
obj.right += dx
method y*(obj: PBoxCollider): int16 {.inline.} = return obj.top
method `y=`*(obj: PBoxCollider, value: int16) {.inline.} =
let dy = value - obj.top
obj.top += dy
obj.bottom += dy
# Circle
method x*(obj: PCircleCollider): int16 {.inline.} = return obj.centerX
method `x=`*(obj: PCircleCollider, value: int16) {.inline.} =
obj.centerX = value + obj.radius
method y*(obj: PCircleCollider): int16 {.inline.} = return obj.centerY
method `y=`*(obj: PCircleCollider, value: int16) {.inline.} =
obj.centerY = value + obj.radius
# Mask
method x*(obj: PMaskCollider): int16 {.inline.} = return obj.x
method `x=`*(obj: PMaskCollider, value: int16) {.inline.} =
obj.x = value
method y*(obj: PMaskCollider): int16 {.inline.} = return obj.y
method `y=`*(obj: PMaskCollider, value: int16) {.inline.} =
obj.y = value
# COLLIDE
# Point - Point
method collide*(a: PPointCollider, b: PPointCollider): bool =
if (a.x == b.x) and (a.y == b.y):
return true
return false
# Box - Box
method collide*(a: PBoxCollider, b: PBoxCollider): bool =
if a.bottom <= b.top: return false
if a.top >= b.bottom: return false
if a.right <= b.left: return false
if a.left >= b.right: return false
return true
# Circle - Circle
method collide*(a: PCircleCollider, b: PCircleCollider): bool =
if distance((a.centerX, a.centerY), (b.centerX, b.centerY)).toInt < (a.radius + b.radius):
return true
return false
# Mask - Mask
method collide*(a: PMaskCollider, b: PMaskCollider): bool =
# Check bounding boxes
var boxA, boxB: TRect
boxA = a.mask.getRect()
boxA.x = boxA.x + a.x
boxA.y = boxA.y + a.y
boxB = b.mask.getRect()
boxB.x = boxB.x + b.x
boxB.y = boxB.y + b.y
if not collide(newBoxCollider(boxA), newBoxCollider(boxB)):
return false
# Check intersection rect
var rectA, rectB: TRect
var width, height: int16
if a.x > b.x:
rectA.x = 0'i16
rectB.x = a.x - b.x
width = min(b.mask.w - rectB.x, a.mask.w)
else:
rectA.x = b.x - a.x
rectB.x = 0'i16
width = min(a.mask.w - rectA.x, b.mask.w)
if a.y > b.y:
rectA.y = 0'i16
rectB.y = a.y - b.y
height = min(b.mask.h - rectB.y, a.mask.h)
else:
rectA.y = b.y - a.y
rectB.y = 0'i16
height = min(a.mask.h - rectA.y, b.mask.h)
rectA.w = width
rectB.w = width
rectA.h = height
rectB.h = height
# Per-pixel scan
for y in 0..height-1:
for x in 0..width-1:
if a.mask.data[rectA.y + y][rectA.x + x] and
b.mask.data[rectB.y + y][rectB.x + x]:
return true
return false
# Point - Box
method collide*(a: PPointCollider, b: PBoxCollider): bool =
if (a.x < b.left) or (a.x >= b.right): return false
if (a.y < b.top) or (a.y >= b.bottom): return false
return true
# Box - Point
method collide*(a: PBoxCollider, b: PPointCollider): bool {.inline.} =
return collide(b, a)
# Point - Circle
method collide*(a: PPointCollider, b: PCircleCollider): bool =
if UInt16(distance((a.x, a.y), (b.centerX, b.centerY))) > b.radius: return false
return true
# Circle - Point
method collide*(a: PCircleCollider, b: PPointCollider): bool {.inline.} =
return collide(b, a)
# Point - Mask
method collide*(a: PPointCollider, b: PMaskCollider): bool =
var boxB: TRect
boxB = b.mask.getRect()
boxB.x = boxB.x + b.x
boxB.y = boxB.y + b.y
if not collide(a, newBoxCollider(boxB)):
return false
if b.mask.data[a.y - boxB.y][a.x - boxB.x]:
return true
return false
# Mask - Point
method collide*(a: PMaskCollider, b: PPointCollider): bool {.inline.} =
return collide(b, a)
# Circle - Box
method collide*(a: PCircleCollider, b: PBoxCollider): bool =
var cx, cy: int16 # closest point
# cx
if a.centerX < b.left: cx = b.left
elif a.centerX > b.right: cx = b.right
else: cx = a.centerX
# cy
if a.centerY < b.top: cy = b.top
elif a.centerY > b.bottom: cy = b.bottom
else: cy = a.centerY
# distance
if distance((a.centerX, a.centerY), (cx, cy)).toInt < a.radius:
return true
return false
# Box - Circle
method collide*(a: PBoxCollider, b: PCircleCollider): bool {.inline.} =
return collide(b, a)
# Circle - Mask
method collide*(a: PCircleCollider, b: PMaskCollider): bool =
# Check bounding boxes
var boxB: TRect
boxB = b.mask.getRect()
boxB.x = boxB.x + b.x
boxB.y = boxB.y + b.y
if not collide(a, newBoxCollider(boxB)):
return false
# Check intersection rect
var boxA, rectB: TRect
var width, height: int16
boxA.x = a.centerX - a.radius
boxA.y = a.centerY - a.radius
boxA.w = a.centerX + a.radius
boxA.h = a.centerY + a.radius
if boxA.x > b.x:
rectB.x = boxA.x - b.x
width = min(b.mask.w - rectB.x, boxA.w)
else:
rectB.x = 0'i16
width = min(boxA.x + boxA.w - b.x, b.mask.w)
if boxA.y > b.y:
rectB.y = boxA.y - b.y
height = min(b.mask.h - rectB.y, boxA.h)
else:
rectB.y = 0'i16
height = min(boxA.x + boxA.h - b.y, b.mask.h)
rectB.w = width
rectB.h = height
# Per-pixel scan
let offsetX = boxB.x + rectB.x - a.centerX
let offsetY = boxB.y + rectB.y - a.centerY
for y in 0..height-1:
for x in 0..width-1:
let dx = abs(offsetX + x)
let dy = abs(offsetY + y)
if dx*dx + dy*dy <= a.radius*a.radius and
b.mask.data[rectB.y + y][rectB.x + x]:
return true
return false
# Mask - Circle
method collide*(a: PMaskCollider, b: PCircleCollider): bool {.inline.} =
return collide(b, a)
# Box - Mask
method collide*(a: PBoxCollider, b: PMaskCollider): bool =
# Check bounding boxes
var boxB: TRect
boxB = b.mask.getRect()
boxB.x = boxB.x + b.x
boxB.y = boxB.y + b.y
if not collide(a, newBoxCollider(boxB)):
return false
# Check intersection rect
var rectB: TRect
var width, height: int16
if a.left > b.x:
rectB.x = a.left - b.x
width = min(b.mask.w - rectB.x, a.right - a.left)
else:
rectB.x = 0'i16
width = min(a.right - b.x, b.mask.w)
if a.top > b.y:
rectB.y = a.top - b.y
height = min(b.mask.h - rectB.y, a.bottom - a.top)
else:
rectB.y = 0'i16
height = min(a.bottom - b.y, b.mask.h)
rectB.w = width
rectB.h = height
# Per-pixel scan
for y in 0..height-1:
for x in 0..width-1:
if b.mask.data[rectB.y + y][rectB.x + x]:
return true
return false
# Mask - Box
method collide*(a: PMaskCollider, b: PBoxCollider): bool {.inline.} =
return collide(b, a)