-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaster.cpp
More file actions
218 lines (185 loc) · 7.71 KB
/
caster.cpp
File metadata and controls
218 lines (185 loc) · 7.71 KB
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
#include "caster.h"
#include "constants.h"
#include <cmath>
void handleEvents(sf::RenderWindow& window) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
}
double calcFrameTime(sf::Clock& clock, double& oldTime) {
sf::Time elapsed = clock.getElapsedTime();
double now = elapsed.asMilliseconds();
double frameTime = (now - oldTime) / 1000.0;
oldTime = now;
return frameTime;
}
void handleMouseMovement(sf::RenderWindow& window,
sf::Vector2<double>& playerDir,
sf::Vector2<double>& playerPlane,
double mouseSensitivity,sf::Vector2<int> delta){
double msens = mouseSensitivity;
double rotationAmount = -delta.x * msens;
double oldDirX = playerDir.x;
playerDir.x = playerDir.x * cos(rotationAmount) - playerDir.y * sin(rotationAmount);
playerDir.y = oldDirX * sin(rotationAmount) + playerDir.y * cos(rotationAmount);
double oldPlaneX = playerPlane.x;
playerPlane.x = playerPlane.x * cos(rotationAmount) - playerPlane.y * sin(rotationAmount);
playerPlane.y = oldPlaneX * sin(rotationAmount) + playerPlane.y * cos(rotationAmount);
}
void movement(const int worldMap[24][24],
sf::Vector2<double>& playerPos,
sf::Vector2<double>& playerDir,
sf::Vector2<double>& playerPlane,
double moveSpeed,
double rotSpeed) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
if (worldMap[int(playerPos.x + playerDir.x * moveSpeed)][int(playerPos.y)] == 0)
playerPos.x += playerDir.x * moveSpeed;
if (worldMap[int(playerPos.x)][int(playerPos.y + playerDir.y * moveSpeed)] == 0)
playerPos.y += playerDir.y * moveSpeed;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
if (worldMap[int(playerPos.x - playerDir.x * moveSpeed)][int(playerPos.y)] == 0)
playerPos.x -= playerDir.x * moveSpeed;
if (worldMap[int(playerPos.x)][int(playerPos.y - playerDir.y * moveSpeed)] == 0)
playerPos.y -= playerDir.y * moveSpeed;
}
//if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
// double oldDirX = playerDir.x;
// playerDir.x = playerDir.x * cos(-rotSpeed) - playerDir.y * sin(-rotSpeed);
// playerDir.y = oldDirX * sin(-rotSpeed) + playerDir.y * cos(-rotSpeed);
//
// double oldPlaneX = playerPlane.x;
// playerPlane.x = playerPlane.x * cos(-rotSpeed) - playerPlane.y * sin(-rotSpeed);
// playerPlane.y = oldPlaneX * sin(-rotSpeed) + playerPlane.y * cos(-rotSpeed);
//}
//
//if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
// double oldDirX = playerDir.x;
// playerDir.x = playerDir.x * cos(rotSpeed) - playerDir.y * sin(rotSpeed);
// playerDir.y = oldDirX * sin(rotSpeed) + playerDir.y * cos(rotSpeed);
//
// double oldPlaneX = playerPlane.x;
// playerPlane.x = playerPlane.x * cos(rotSpeed) - playerPlane.y * sin(rotSpeed);
// playerPlane.y = oldPlaneX * sin(rotSpeed) + playerPlane.y * cos(rotSpeed);
//}
}
////void castFloornCeiling(sf::RenderWindow& window,
// const int worldMap[24][24],
// sf::Vector2<double>& playerPos,
// sf::Vector2<double>& playerDir,
// sf::Vector2<double>& playerPlane) {
//
//
// sf::VertexArray floorCeiling(sf::Points);
//
// for(int y = 0; y<screenHeight; y++){
//
// sf::Vector2<float> rayDir0(playerDir.x - playerPlane.x , playerDir.y - playerPlane.y);
// sf::Vector2<float> rayDir1(playerDir.x + playerPlane.x , playerDir.y + playerPlane.y);
//
// int p = y-screenHeight /2;
// if (p == 0) continue;
//
// float zPos = 0.5 * screenHeight;
// float rowDistance = zPos / p;
//
// sf::Vector2<double> floorStep(rowDistance * (rayDir1.x - rayDir0.x) / screenWidth,rowDistance * (rayDir1.y - rayDir0.y) / screenWidth);
// sf::Vector2<double> floorCords(playerPos.x+ rowDistance * rayDir0.x ,playerPos.y+rowDistance*rayDir0.y);
//
// for(int x = 0; x<screenWidth; x++){
// //sf::Vector2<int> cellCord((int)(floorCords.x),(int)(floorCords.y))
//
//
// floorCeiling.append(sf::Vertex(sf::Vector2f(x, y), sf::Color::Red));
//
// // Draw ceiling pixel (symmetrical)
// floorCeiling.append(sf::Vertex(sf::Vector2f(x, screenHeight - y - 1),sf::Color::Red));
//
// // Advance floor world position
// floorCords.x += floorStep.x;
// floorCords.y += floorStep.y;
//
// }
//
// }
// window.draw(floorCeiling);
//
// }
void castRays(sf::RenderWindow& window,
const int worldMap[24][24],
sf::Vector2<double>& playerPos,
sf::Vector2<double>& playerDir,
sf::Vector2<double>& playerPlane) {
for (int x = 0; x < screenWidth; ++x) {
double cameraX = 2.0 * x / double(screenWidth) - 1.0;
sf::Vector2<double> rayDir(
playerDir.x + playerPlane.x * cameraX,
playerDir.y + playerPlane.y * cameraX
);
sf::Vector2<int> mapPos(int(playerPos.x), int(playerPos.y));
sf::Vector2<double> sideDist;
double bignum = 1e30;
sf::Vector2<double> deltaDist(
(rayDir.x == 0.0) ? bignum : std::abs(1.0 / rayDir.x),
(rayDir.y == 0.0) ? bignum : std::abs(1.0 / rayDir.y)
);
double perpWallDist = 0.0;
sf::Vector2<int> step(0, 0);
if(rayDir.x < 0) {
step.x = -1;
sideDist.x =(playerPos.x - mapPos.x) * deltaDist.x;
} else {
step.x = 1;
sideDist.x =(mapPos.x + 1.0 - playerPos.x) * deltaDist.x;
}
if(rayDir.y < 0) {
step.y = -1;
sideDist.y =(playerPos.y - mapPos.y) * deltaDist.y;
} else {
step.y = 1;
sideDist.y =(mapPos.y + 1.0 - playerPos.y) * deltaDist.y;
}
int hit = 0;
int side = 0;
while (hit == 0) {
if (sideDist.x < sideDist.y) {
sideDist.x += deltaDist.x;
mapPos.x += step.x;
side = 0;
} else {
sideDist.y += deltaDist.y;
mapPos.y += step.y;
side = 1;
}
if (mapPos.x < 0 || mapPos.x >= mapWidth || mapPos.y < 0 || mapPos.y >= mapHeight) {
hit =1;
break;
}
if (worldMap[mapPos.x][mapPos.y] > 0) hit = 1;
}
if (side == 0) perpWallDist = sideDist.x - deltaDist.x;
else perpWallDist = sideDist.y - deltaDist.y;
int lineHeight = (perpWallDist > 0.0) ? int(screenHeight / perpWallDist) : screenHeight;
int drawStart = -lineHeight / 2 + screenHeight / 2;
if (drawStart < 0) drawStart = 0;
int drawEnd = lineHeight / 2 + screenHeight / 2;
if(drawEnd >= screenHeight) drawEnd = screenHeight - 1;
sf::Color col = sf::Color::Yellow;
switch (worldMap[mapPos.x][mapPos.y]) {
case 1: col = sf::Color::Red; break;
case 2: col = sf::Color::Green; break;
case 3: col = sf::Color::Blue; break;
case 4: col = sf::Color::White; break;
case 5: col = sf::Color::Black; break;
default: break;
}
if (side == 1) col = sf::Color(col.r / 2, col.g / 2, col.b/2);
sf::RectangleShape column(sf::Vector2(1.f, float(drawEnd - drawStart +1)));
column.setPosition(float(x), float(drawStart));
column.setFillColor(col);
window.draw(column);
}
}