-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraycaster.cpp
More file actions
84 lines (76 loc) · 1.92 KB
/
Copy pathraycaster.cpp
File metadata and controls
84 lines (76 loc) · 1.92 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
#include <vector>
#include <stdio.h>
class RayCaster
{
public:
RayCaster() : pos{500.0f / 2.0f, 500.0f / 2.0f}
{
for (float a = 0; a < 360; a += 0.01)
{
rays.push_back(Ray(pos.x, pos.y, a * M_PI / 180.0f));
}
}
void set(float x, float y)
{
pos.x = x;
pos.y = y;
}
void move(float dx, float dy) {
pos.x += dx;
pos.y += dy;
}
void look(const std::vector<Line>& walls)
{
for (auto& ray : rays)
{
Point closest;
float record = INFINITY;
printf("%f",record);
for (const auto& wall : walls)
{
Point pt = cast(ray, wall);
printf("%f, %f", pt.x, pt.y);
//if (pt != null)
//{
float d = sqrt(pow(pos.x - pt.x, 2) + pow(pos.y - pt.y, 2));
if (d < record)
{
record = d;
closest = pt;
}
//}
}
if (record != INFINITY)
{
float d = sqrt(pow(pos.x - pt.x, 2) + pow(pos.y - pt.y, 2));
if (d < record)
{
record = d;
closest = pt;
}
}
}
if (record != INFINITY)
{
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex2f(pos.x, pos.y);
glVertex2f(closest.x, closest.y);
glEnd();
}
}
void show() const
{
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_POINTS);
glVertex2f(pos.x, pos.y);
glEnd();
for (const auto& ray : rays)
{
ray.show();
}
}
private:
std::vector<Ray> rays;
Point pos;
};