-
Notifications
You must be signed in to change notification settings - Fork 0
/
ray.cpp
179 lines (141 loc) · 5.73 KB
/
ray.cpp
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
#include "ray.h"
#include "map.h"
#include "player.h"
#include "window.h"
#include <iostream>
#define LOG(X) std::cout << X << std::endl;
namespace rc {
enum Color {
BLACK,
BLUE,
RED
};
};
void rc::Ray::checkVerticalIntersects(const Map &map) {
long intersectX, intersectY;
bool isRayCastingRight = cos(_angleInRadians) >= 0;
bool isRayCastingDown = sin(_angleInRadians) >= 0;
float stepx, stepy;
_verticalIntersectionPoints.clear();
// Coordinates on map grid
float gridX = floor(_originX / MapConstants::TILE_SIZE);
// Ray is being cast to the right, the y-intersection point should be at the nearest-right position of the viewer
if (isRayCastingRight) {
intersectX = (gridX + 1) * MapConstants::TILE_SIZE + 1;
} else {
// Ray is being cast to the left, the y-intersection point should be at the nearest-left position of the viewer
intersectX = gridX * MapConstants::TILE_SIZE - 1;
}
intersectY = _originY + tan(_angleInRadians) * (intersectX - _originX);
stepx = MapConstants::TILE_SIZE;
stepx *= isRayCastingRight ? 1 : -1;
stepy = stepx * tan(_angleInRadians);
stepy *= (!isRayCastingDown && stepy > 0) ? -1 : 1;
stepy *= (isRayCastingDown && stepy < 0) ? -1 : 1;
while (intersectX > 0 && intersectX < Window::WINDOW_WIDTH
&& intersectY > 0 && intersectY < Window::WINDOW_HEIGHT) {
if (map.isWallAt(intersectX, intersectY)) {
_distance = sqrt(abs(intersectX - _originX) * abs(intersectX - _originX) +
abs(intersectY - _originY) * abs(intersectY - _originY));
_collisionPoints.push_back(CollisionPoint{intersectX, intersectY});
break;
} else {
_verticalIntersectionPoints.push_back(IntersectionPoint{intersectX, intersectY});
}
intersectX += stepx;
intersectY += stepy;
}
}
void rc::Ray::checkHorizontalIntersects(const Map &map) {
int intersectX, intersectY;
bool isRayCastingRight = cos(_angleInRadians) >= 0;
bool isRayCastingDown = sin(_angleInRadians) >= 0;
float stepx, stepy;
_horizontalIntersectionPoints.clear();
// Coordinates on map grid
float gridY = floor(_originY / MapConstants::TILE_SIZE);
if (isRayCastingDown) {
intersectY = (gridY + 1) * MapConstants::TILE_SIZE + 1;
} else {
intersectY = gridY * MapConstants::TILE_SIZE - 1;
}
intersectX = _originX + (intersectY - _originY) / tan(_angleInRadians);
stepy = MapConstants::TILE_SIZE;
stepy *= isRayCastingDown ? 1 : -1;
stepx = stepy / tan(_angleInRadians);
stepx *= (!isRayCastingRight && stepx > 0) ? -1 : 1;
stepx *= (isRayCastingRight && stepx < 0) ? -1 : 1;
while (intersectX > 0 && intersectX < Window::WINDOW_WIDTH
&& intersectY > 0 && intersectY < Window::WINDOW_HEIGHT) {
if (map.isWallAt(intersectX, intersectY)) {
_distance = sqrt(abs(intersectX - _originX) * abs(intersectX - _originX) +
abs(intersectY - _originY) * abs(intersectY - _originY));
_collisionPoints.push_back(CollisionPoint{intersectX, intersectY});
break;
} else {
_horizontalIntersectionPoints.push_back(IntersectionPoint{intersectX, intersectY});
}
intersectX += stepx;
intersectY += stepy;
}
}
static int distanceFromPoint(int x, int y, int pX, int pY) {
return sqrt(abs(x - pX) * abs(x - pX) +
abs(y - pY) * abs(y - pY));
}
bool rc::Ray::cast(const Map &map) {
checkHorizontalIntersects(map);
checkVerticalIntersects(map);
CollisionPoint closerPoint = _collisionPoints.front();
for (auto &point : _collisionPoints) {
int closestDistance = distanceFromPoint(closerPoint.x, closerPoint.y, _originX, _originY);
if (distanceFromPoint(point.x, point.y, _originX, _originY) < closestDistance) {
closerPoint = point;
}
}
// Minimum distance from walls should be at least the player's size.
_distance = std::max(static_cast<int>(distanceFromPoint(closerPoint.x, closerPoint.y, _originX, _originY)),
static_cast<int>(Player::_radius));
_collisionPoints.clear();
_collisionPoints.push_back(closerPoint);
return false;
}
static void
drawPoints(SDL_Renderer *renderer, const std::vector<rc::Point> &points, rc::Color color) {
for (auto &intersection : points) {
// Collision points are drawn red
switch (color) {
case rc::BLACK:
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
break;
case rc::BLUE:
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
break;
case rc::RED:
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
break;
}
SDL_Rect rect{intersection.x, intersection.y, 5, 5};
SDL_RenderFillRect(renderer, &rect);
}
}
void rc::Ray::render(SDL_Renderer *renderer, unsigned originX, unsigned originY) const {
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
SDL_RenderDrawLine(renderer,
originX,
originY,
originX + cos(_angleInRadians) * _distance,
originY + sin(_angleInRadians) * _distance);
if (_drawHorizIntersectionPoints) {
drawPoints(renderer, _horizontalIntersectionPoints, Color::BLUE);
}
if (_drawVertIntersectionPoints) {
drawPoints(renderer, _verticalIntersectionPoints, Color::BLACK);
}
if (_drawCollisionPoints) {
drawPoints(renderer, _collisionPoints, Color::RED);
}
}
void rc::Ray::setDistance(int newDistance) {
_distance = newDistance;
}