-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtilecollider.go
267 lines (232 loc) · 7.63 KB
/
tilecollider.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
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
// Package tilecollide provides collision detection for rectangles and 2D tilemaps.
package tilecollider
import (
"math"
"golang.org/x/exp/constraints"
)
// Integer is a constraint that matches any integer type.
type Integer = constraints.Integer
// CollisionInfo stores information about a collision with a tile
type CollisionInfo[T Integer] struct {
TileID T // ID of the collided tile
TileCoords [2]int // X,Y coordinates of the tile in the tilemap
Normal [2]int // Normal vector of the collision (-1/0/1)
}
// Collider handles collision detection between rectangles and a 2D tilemap
type Collider[T Integer] struct {
Collisions []CollisionInfo[T] // List of collisions from last check
TileSize [2]int // Width and height of tiles
TileMap [][]T // 2D grid of tile IDs
NonSolidTileID T // Sets the ID of non-solid tiles. Defaults to 0.
StaticCheck bool // If true, always checks for static collisions. (no movement)
}
// NewCollider creates a new tile collider with the given tilemap and tile dimensions
func NewCollider[T Integer](tileMap [][]T, tileWidth, tileHeight int) *Collider[T] {
return &Collider[T]{
TileMap: tileMap,
TileSize: [2]int{tileWidth, tileHeight},
}
}
// CollisionCallback is called when collisions occur, receiving collision info and final movement
type CollisionCallback[T Integer] func([]CollisionInfo[T], float64, float64)
// Collide checks for collisions when moving a rectangle and returns the allowed movement
func (c *Collider[T]) Collide(rectX, rectY, rectW, rectH, moveX, moveY float64, onCollide CollisionCallback[T]) (float64, float64) {
c.Collisions = c.Collisions[:0]
if moveX == 0 && moveY == 0 {
if c.StaticCheck {
// Static collision test
playerTop := int(math.Floor(rectY / float64(c.TileSize[1])))
playerBottom := int(math.Ceil((rectY+rectH)/float64(c.TileSize[1]))) - 1
playerLeft := int(math.Floor(rectX / float64(c.TileSize[0])))
playerRight := int(math.Ceil((rectX+rectW)/float64(c.TileSize[0]))) - 1
minPenetration := math.MaxFloat64
var resolveX, resolveY float64
for y := playerTop; y <= playerBottom; y++ {
if y < 0 || y >= len(c.TileMap) {
continue
}
for x := playerLeft; x <= playerRight; x++ {
if x < 0 || x >= len(c.TileMap[0]) {
continue
}
if c.TileMap[y][x] != c.NonSolidTileID {
// Calculate overlap on each axis
tileLeft := float64(x * c.TileSize[0])
tileTop := float64(y * c.TileSize[1])
overlapX := min(rectX+rectW-tileLeft, tileLeft+float64(c.TileSize[0])-rectX)
overlapY := min(rectY+rectH-tileTop, tileTop+float64(c.TileSize[1])-rectY)
// Choose smallest penetration
if overlapX < overlapY && overlapX < minPenetration {
minPenetration = overlapX
if rectX+rectW/2 < tileLeft+float64(c.TileSize[0])/2 {
resolveX = -overlapX
resolveY = 0
} else {
resolveX = overlapX
resolveY = 0
}
} else if overlapY < minPenetration {
minPenetration = overlapY
if rectY+rectH/2 < tileTop+float64(c.TileSize[1])/2 {
resolveX = 0
resolveY = -overlapY
} else {
resolveX = 0
resolveY = overlapY
}
}
c.Collisions = append(c.Collisions, CollisionInfo[T]{
TileID: c.TileMap[y][x],
TileCoords: [2]int{x, y},
Normal: [2]int{int(math.Copysign(1, -resolveX)), int(math.Copysign(1, -resolveY))},
})
}
}
}
return resolveX, resolveY
} else {
return moveX, moveY
}
}
if math.Abs(moveX) > math.Abs(moveY) {
if moveX != 0 {
moveX = c.CollideX(rectX, rectY, rectW, rectH, moveX)
}
if moveY != 0 {
moveY = c.CollideY(rectX+moveX, rectY, rectW, rectH, moveY)
}
} else {
if moveY != 0 {
moveY = c.CollideY(rectX, rectY, rectW, rectH, moveY)
}
if moveX != 0 {
moveX = c.CollideX(rectX, rectY+moveY, rectW, rectH, moveX)
}
}
if onCollide != nil {
onCollide(c.Collisions, moveX, moveY)
}
return moveX, moveY
}
// CollideX checks for collisions along the X axis and returns the allowed X movement
func (c *Collider[T]) CollideX(rectX, rectY, rectW, rectH, moveX float64) float64 {
checkLimit := max(1, int(math.Ceil(math.Abs(moveX)/float64(c.TileSize[0])))+1)
playerTop := int(math.Floor(rectY / float64(c.TileSize[1])))
playerBottom := int(math.Ceil((rectY+rectH)/float64(c.TileSize[1]))) - 1
if moveX > 0 {
startX := int(math.Floor((rectX + rectW) / float64(c.TileSize[0])))
endX := startX + checkLimit
endX = min(endX, len(c.TileMap[0]))
for y := playerTop; y <= playerBottom; y++ {
if y < 0 || y >= len(c.TileMap) {
continue
}
for x := startX; x < endX; x++ {
if x < 0 || x >= len(c.TileMap[0]) {
continue
}
if c.TileMap[y][x] != c.NonSolidTileID {
tileLeft := float64(x * c.TileSize[0])
collision := tileLeft - (rectX + rectW)
if collision <= moveX {
moveX = collision
c.Collisions = append(c.Collisions, CollisionInfo[T]{
TileID: c.TileMap[y][x],
TileCoords: [2]int{x, y},
Normal: [2]int{-1, 0},
})
}
}
}
}
}
if moveX < 0 {
endX := int(math.Floor(rectX / float64(c.TileSize[0])))
startX := endX - checkLimit
startX = max(startX, 0)
for y := playerTop; y <= playerBottom; y++ {
if y < 0 || y >= len(c.TileMap) {
continue
}
for x := startX; x <= endX; x++ {
if x < 0 || x >= len(c.TileMap[0]) {
continue
}
if c.TileMap[y][x] != c.NonSolidTileID {
tileRight := float64((x + 1) * c.TileSize[0])
collision := tileRight - rectX
if collision >= moveX {
moveX = collision
c.Collisions = append(c.Collisions, CollisionInfo[T]{
TileID: c.TileMap[y][x],
TileCoords: [2]int{x, y},
Normal: [2]int{1, 0},
})
}
}
}
}
}
return moveX
}
// CollideY checks for collisions along the Y axis and returns the allowed Y movement
func (c *Collider[T]) CollideY(rectX, rectY, rectW, rectH, moveY float64) float64 {
checkLimit := max(1, int(math.Ceil(math.Abs(moveY)/float64(c.TileSize[1])))+1)
playerLeft := int(math.Floor(rectX / float64(c.TileSize[0])))
playerRight := int(math.Ceil((rectX+rectW)/float64(c.TileSize[0]))) - 1
if moveY > 0 {
startY := int(math.Floor((rectY + rectH) / float64(c.TileSize[1])))
endY := startY + checkLimit
endY = min(endY, len(c.TileMap))
for x := playerLeft; x <= playerRight; x++ {
if x < 0 || x >= len(c.TileMap[0]) {
continue
}
for y := startY; y < endY; y++ {
if y < 0 || y >= len(c.TileMap) {
continue
}
if c.TileMap[y][x] != c.NonSolidTileID {
tileTop := float64(y * c.TileSize[1])
collision := tileTop - (rectY + rectH)
if collision <= moveY {
moveY = collision
c.Collisions = append(c.Collisions, CollisionInfo[T]{
TileID: c.TileMap[y][x],
TileCoords: [2]int{x, y},
Normal: [2]int{0, -1},
})
}
}
}
}
}
if moveY < 0 {
endY := int(math.Floor(rectY / float64(c.TileSize[1])))
startY := endY - checkLimit
startY = max(startY, 0)
for x := playerLeft; x <= playerRight; x++ {
if x < 0 || x >= len(c.TileMap[0]) {
continue
}
for y := startY; y <= endY; y++ {
if y < 0 || y >= len(c.TileMap) {
continue
}
if c.TileMap[y][x] != c.NonSolidTileID {
tileBottom := float64((y + 1) * c.TileSize[1])
collision := tileBottom - rectY
if collision >= moveY {
moveY = collision
c.Collisions = append(c.Collisions, CollisionInfo[T]{
TileID: c.TileMap[y][x],
TileCoords: [2]int{x, y},
Normal: [2]int{0, 1},
})
}
}
}
}
}
return moveY
}