-
Notifications
You must be signed in to change notification settings - Fork 43
/
raycaster_float.c
237 lines (216 loc) · 7.07 KB
/
raycaster_float.c
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
// floating-point implementation for testing/comparison
//
// This diagram shows the coordinate system used in the
// RayCasterFloat::Distance() function.
//
// ^ rayA/
// sin- | / sin+
// cos+ | / cos+
// tan- | / tan+
// | /
// |/
// ---------------------------->
// |
// sin- | sin+
// cos- | cos-
// tan+ | tan-
// |
#include "raycaster_float.h"
#include <math.h>
#include <stdlib.h>
#define P2P_DISTANCE(x1, y1, x2, y2) \
sqrt((float) (((x1) - (x2)) * ((x1) - (x2)) + \
((y1) - (y2)) * ((y1) - (y2))))
typedef struct {
float playerX;
float playerY;
float playerA;
} RayCasterFloat;
static void RayCasterFloatTrace(RayCaster *rayCaster,
uint16_t screenX,
uint8_t *screenY,
uint8_t *textureNo,
uint8_t *textureX,
uint16_t *textureY,
uint16_t *textureStep);
static void RayCasterFloatStart(RayCaster *rayCaster,
uint16_t playerX,
uint16_t playerY,
int16_t playerA);
static void RayCasterFloatDestruct(RayCaster *rayCaster);
RayCaster *RayCasterFloatConstruct(void)
{
RayCaster *rayCaster = RayCasterConstruct();
RayCasterFloat *rayCasterFloat = malloc(sizeof(RayCasterFloat));
if (!rayCasterFloat) {
rayCaster->Destruct(rayCaster);
return NULL;
}
rayCaster->derived = rayCasterFloat;
rayCaster->Start = RayCasterFloatStart;
rayCaster->Trace = RayCasterFloatTrace;
rayCaster->Destruct = RayCasterFloatDestruct;
return rayCaster;
}
static bool RayCasterFloatIsWall(float rayX, float rayY)
{
float mapX = 0;
float mapY = 0;
modff(rayX, &mapX);
modff(rayY, &mapY);
int tileX = (int) mapX;
int tileY = (int) mapY;
if (tileX < 0 || tileY < 0 || tileX >= MAP_X - 1 || tileY >= MAP_Y - 1) {
return true;
}
return g_map[(tileX >> 3) + (tileY << (MAP_XS - 3))] &
(1 << (8 - (tileX & 0x7)));
}
static float RayCasterFloatDistance(float playerX,
float playerY,
float rayA,
float *hitOffset,
int *hitDirection)
{
while (rayA < 0) {
rayA += 2.0f * M_PI;
}
while (rayA >= 2.0f * M_PI) {
rayA -= 2.0f * M_PI;
}
float tanA = tan(rayA);
float cotA = 1 / tanA;
float rayX, rayY, vx, vy;
float xOffset, yOffset, vertHitDis, horiHitDis;
int depth = 0;
const int maxDepth = 100; // maxDepth is the maximum number of
// attempt for a ray to find a wall.
// Check for vertical hit
depth = 0;
vertHitDis = 0;
if (sin(rayA) > 0.001) { // rayA pointing rightward
rayX = (int) playerX + 1;
rayY = (rayX - playerX) * cotA + playerY;
xOffset = 1;
yOffset = xOffset * cotA;
} else if (sin(rayA) < -0.001) { // rayA pointing leftward
rayX = (int) playerX - 0.001;
rayY = (rayX - playerX) * cotA + playerY;
xOffset = -1;
yOffset = xOffset * cotA;
} else { // rayA pointing up or down
rayX = playerX;
rayY = playerY;
xOffset = 0;
yOffset = 0;
depth = maxDepth;
}
while (depth < maxDepth) {
if (RayCasterFloatIsWall(rayX, rayY)) {
vertHitDis = P2P_DISTANCE(playerX, playerY, rayX, rayY);
break;
} else {
rayX += xOffset;
rayY += yOffset;
depth += 1;
}
}
vx = rayX;
vy = rayY;
// Check for horizontal hit
depth = 0;
horiHitDis = 0;
if (cos(rayA) > 0.001) { // rayA pointing upward
rayY = (int) playerY + 1;
rayX = (rayY - playerY) * tanA + playerX;
yOffset = 1;
xOffset = yOffset * tanA;
} else if (cos(rayA) < -0.001) { // rayA pointing downward
rayY = (int) playerY - 0.001;
rayX = (rayY - playerY) * tanA + playerX;
yOffset = -1;
xOffset = yOffset * tanA;
} else { // rayA pointing leftward or rightward
rayX = playerX;
rayY = playerY;
xOffset = 0;
yOffset = 0;
depth = maxDepth;
}
while (depth < maxDepth) {
if (RayCasterFloatIsWall(rayX, rayY)) {
horiHitDis = P2P_DISTANCE(playerX, playerY, rayX, rayY);
break;
} else {
rayX += xOffset;
rayY += yOffset;
depth += 1;
}
}
if (vertHitDis < horiHitDis) { // Vertical hit
rayX = vx;
rayY = vy;
*hitDirection = true;
*hitOffset = rayY;
} else { // Horizontal hit
*hitDirection = false;
*hitOffset = rayX;
}
return fmin(vertHitDis, horiHitDis);
}
static void RayCasterFloatTrace(RayCaster *rayCaster,
uint16_t screenX,
uint8_t *screenY,
uint8_t *textureNo,
uint8_t *textureX,
uint16_t *textureY,
uint16_t *textureStep)
{
float hitOffset;
int hitDirection;
float deltaAngle = atanf(((int16_t) screenX - SCREEN_WIDTH / 2.0f) /
(SCREEN_WIDTH / 2.0f) * M_PI / 4);
float lineDistance = RayCasterFloatDistance(
((RayCasterFloat *) (rayCaster->derived))->playerX,
((RayCasterFloat *) (rayCaster->derived))->playerY,
((RayCasterFloat *) (rayCaster->derived))->playerA + deltaAngle,
&hitOffset, &hitDirection);
float distance = lineDistance * cos(deltaAngle);
float dum;
*textureX = (uint8_t) (256.0f * modff(hitOffset, &dum));
*textureNo = hitDirection;
*textureY = 0;
*textureStep = 0;
if (distance > 0) {
float tmp = INV_FACTOR / distance;
*screenY = tmp;
float txs = (tmp * 2.0f);
if (txs != 0) {
*textureStep = (256 / txs) * 256;
if (txs > SCREEN_HEIGHT) {
float wallHeight = (txs - SCREEN_HEIGHT) / 2;
*textureY = wallHeight * (256 / txs) * 256;
*screenY = HORIZON_HEIGHT;
}
}
} else {
*screenY = 0;
}
}
static void RayCasterFloatStart(RayCaster *rayCaster,
uint16_t playerX,
uint16_t playerY,
int16_t playerA)
{
((RayCasterFloat *) (rayCaster->derived))->playerX =
(playerX / 1024.0f) * 4.0f;
((RayCasterFloat *) (rayCaster->derived))->playerY =
(playerY / 1024.0f) * 4.0f;
((RayCasterFloat *) (rayCaster->derived))->playerA =
(playerA / 1024.0f) * 2.0f * M_PI;
}
static void RayCasterFloatDestruct(RayCaster *rayCaster)
{
free(rayCaster->derived);
RayCasterDestruct(rayCaster);
}