-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrays.go
275 lines (237 loc) · 8.02 KB
/
rays.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
268
269
270
271
272
273
274
275
package main
import (
"math"
"github.com/veandco/go-sdl2/sdl"
)
// Rays - many rays
type Rays [NumRays]*Ray
func NewRays() *Rays {
r := new(Rays)
for i := range r {
r[i] = NewRay()
}
return r
}
// Ray - struct
type Ray struct {
angle, wallHitX, wallHitY, distance float64
wasHitVertical bool
isRayFacingUp, isRayFacingDown,
isRayFacingLeft, isRayFacingRight bool
wallHitContent int // store the actual content of the wall once we find a hit
}
// NewRay - constructor
func NewRay() *Ray {
return &Ray{
angle: 0.0,
wallHitX: -1,
wallHitY: -1,
distance: -1,
wasHitVertical: false,
isRayFacingDown: false,
isRayFacingUp: false,
isRayFacingRight: false,
isRayFacingLeft: false,
wallHitContent: -1, // set to -1 for debugging in case
}
}
func (r *Ray) Render(renderer *sdl.Renderer, x, y float64) {
renderer.SetDrawColor(255, 0, 0, 30)
renderer.DrawLine(
int32(MinimapScaleFactor*x),
int32(MinimapScaleFactor*y),
int32(MinimapScaleFactor*r.wallHitX),
int32(MinimapScaleFactor*r.wallHitY),
)
}
func (r *Ray) Cast(angle float64) *Ray {
var xIntercept, yIntercept, xStep, yStep float64
r.angle = normalizeAngle(angle)
// we have to figure out which way the ray is facing when trying to calculate the intersects.
// Math.PI = 180 Degrees
r.isRayFacingDown = false
if r.angle > 0 && r.angle < PI {
r.isRayFacingDown = true
}
r.isRayFacingUp = !r.isRayFacingDown
// 0.5 * Math.PI = 90 Degrees
// 1.5 * Math.PI = 270 Degrees
r.isRayFacingRight = false
if r.angle < 0.5*PI || r.angle > 1.5*PI {
r.isRayFacingRight = true
}
r.isRayFacingLeft = !r.isRayFacingRight
/*
* ================================
* HORIZONTAL ray-grid intersection
* ================================
*
*/
foundHorizontalWallHit := false
horzWallHitX := 0.0
horzWallHitY := 0.0
horzWallContent := 0
/* Find the y-coordinate of the closest horizontal grid intersection
* =================================================================
*
* Ray facing up. (standard - base assumption)
* -----*-- => Ay (yIntercept) when ray is facing up. That's what we always calculate
* | | No need to add TILE_SIZE
* | |
* | |
* | |
* | |
* --------
*
* Ray facing down
* --------
* | |
* | |
* | |
* | |
* | |
* -----*-- => Ay (yIntercept) when ray is facing down.
* We take yIntercept = Math.floor(player.y / TILE_SIZE) * TILE_SIZE;
* And we add TILE_SIZE
*
* So the yIntercept value above relates to if the ray is actually pointing up.
* We actually calculate that initially using:
* yIntercept = Math.floor(player.y / TILE_SIZE) * TILE_SIZE
*
* Think of the box (tile) - yIntercept calculated initially with the above formula will
* give the Ay coordinate sitting at the 'top' of the tile i.e. when the ray is facing up.
*
* If however it's actually facing down then we add TILE_SIZE to it
* yIntercept += this.isRayFacingDown ? TILE_SIZE : 0;
*
*/
yIntercept = math.Floor(G.Player.y/TileSize) * TileSize // this always gets the 'top' Ay coordinate i.e. ray facing up
if r.isRayFacingDown { // else += 0
yIntercept += TileSize
}
// Find the x-coordinate of the closest horizontal grid interception
// xIntercept = G.Player.x + ((G.Player.y - yIntercept) / Math.tan(this.angle));
xIntercept = G.Player.x + ((yIntercept - G.Player.y) / math.Tan(r.angle))
// Calculate the increment xstep and ystep
yStep = TileSize
if r.isRayFacingUp { // depending on where the ray is facing we invert or not
yStep *= -1
}
xStep = TileSize / math.Tan(r.angle)
if r.isRayFacingLeft && xStep > 0 { // if the xstep is positive but the ray is facing left we invert
xStep *= -1
}
if r.isRayFacingRight && xStep < 0 { // if the xstep is negative but the ray is facing right we invert
xStep *= -1
}
nextHorzTouchX := xIntercept
nextHorzTouchY := yIntercept
// increment xstep and ystep until we find a wall
for nextHorzTouchX >= 0 &&
nextHorzTouchX <= WindowWidth &&
nextHorzTouchY >= 0 &&
nextHorzTouchY <= WindowHeight {
testTouchX := nextHorzTouchX
testTouchY := nextHorzTouchY
// Since right now the Y position is sitting right on the border of the tile to check
// if we are actually in the wall i.e. not skip over it we can force it by
// just pushing it by 1 (i.e. subtract 1 if its facing up) to get it in
// only used for checking we don't want to change the value of nextHorzTouchY as otherwise we will be skipping a value each time we loop
if r.isRayFacingUp { // force one pixel up
testTouchY = nextHorzTouchY - 1
}
// Found a wall hit
if G.GameMap.HasWallAt(testTouchX, testTouchY) {
horzWallHitX = nextHorzTouchX
horzWallHitY = nextHorzTouchY
horzWallContent = G.GameMap.Level.At(int(math.Floor(testTouchY/TileSize)), int(math.Floor(testTouchX/TileSize)))
foundHorizontalWallHit = true
break
} else {
nextHorzTouchX += xStep
nextHorzTouchY += yStep
}
}
/*
*
* ================================
* VERTICAL ray-grid intersection
* ================================
*
*/
foundVerticalWallHit := false
vertWallHitX := 0.0
vertWallHitY := 0.0
vertWallContent := 0
// Find the x-coordinate of the closest vertical grid interception
xIntercept = math.Floor(G.Player.x/TileSize) * TileSize
if r.isRayFacingRight { // add 32 (tile_size) if facing right
xIntercept += TileSize
}
// Find the y-coordinate of the closest vertical grid interception
// yIntercept = G.Player.y + ((G.Player.x - xIntercept) * Math.tan(this.angle));
yIntercept = G.Player.y + ((xIntercept - G.Player.x) * math.Tan(r.angle))
// Calculate the increment xstep and ystep
xStep = TileSize
if r.isRayFacingLeft { // depending on where the ray is facing we invert or not
xStep *= -1
}
yStep = TileSize * math.Tan(r.angle)
if r.isRayFacingUp && yStep > 0 {
yStep *= -1
}
if r.isRayFacingDown && yStep < 0 {
yStep *= -1
}
nextVertTouchX := xIntercept
nextVertTouchY := yIntercept
// increment xstep and ystep until we find a wall
for nextVertTouchX >= 0 &&
nextVertTouchX <= WindowWidth &&
nextVertTouchY >= 0 &&
nextVertTouchY <= WindowHeight {
testTouchX := nextVertTouchX
testTouchY := nextVertTouchY
// since right now the X position is sitting right on the border of the tile
// to check if we are actually in the wall i.e. not skip over it
// we can force it by just pushing it by 1 (i.e. subtract 1 if its facing left) to get it in the next tile/wall/pixel
// only used for checking we don't want to change the value of nextVertTouchX as otherwise we will be skipping a value each time we loop
if r.isRayFacingLeft { // force one pixel left
testTouchX = nextVertTouchX - 1
}
if G.GameMap.HasWallAt(testTouchX, nextVertTouchY) {
vertWallHitX = nextVertTouchX
vertWallHitY = nextVertTouchY
vertWallContent = G.GameMap.Level.At(int(math.Floor(testTouchY/TileSize)), int(math.Floor(testTouchX/TileSize)))
foundVerticalWallHit = true
break
} else {
nextVertTouchX += xStep
nextVertTouchY += yStep
}
}
// Calculate both horizontal and vertical distances and choose the smallest value
horzHitDistance := math.MaxFloat64 // if we didn't get a hit then we basically just set it to a really large value
if foundHorizontalWallHit {
horzHitDistance = distanceBetweenPoints(G.Player.x, G.Player.y, horzWallHitX, horzWallHitY)
}
vertHitDistance := math.MaxFloat64 // if we didn't get a hit then we basically just set it to a really large value
if foundVerticalWallHit {
vertHitDistance = distanceBetweenPoints(G.Player.x, G.Player.y, vertWallHitX, vertWallHitY)
}
// Compare the two and store the smallest one
if horzHitDistance < vertHitDistance {
r.wallHitX = horzWallHitX
r.wallHitY = horzWallHitY
r.distance = horzHitDistance
r.wallHitContent = horzWallContent
r.wasHitVertical = false
} else {
r.wallHitX = vertWallHitX
r.wallHitY = vertWallHitY
r.distance = vertHitDistance
r.wallHitContent = vertWallContent
r.wasHitVertical = true
}
return r
}