Repository navigation
Expand file tree
/
Copy pathraycasting.cpp
More file actions
214 lines (144 loc) · 5.51 KB
/
Copy pathraycasting.cpp
File metadata and controls
214 lines (144 loc) · 5.51 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
#include "global.h"
#include "raycasting.h"
#include "raylib.h"
#include <cmath>
#include <algorithm>
const int nRays = 90;
float density = 1;
dataRays ray[nRays] = {};
int cellSize = 32;
const int W = 20;
const int H = 20;
Vector2 pos = {screenWidth/2, screenHeight/2};
float spd = 300.0f;
int hsp = 0, vsp = 0;
float angle = 0.0f;
float mouseSensivity = 0.2f;
void UpdateMovement(){
hsp = IsKeyDown(KEY_D) - IsKeyDown(KEY_A);
vsp = IsKeyDown(KEY_W) - IsKeyDown(KEY_S);
float rad = angle * DEG2RAD;
float forwardX = cosf(rad);
float forwardY = sinf(rad);
float rightX = -sinf(rad);
float rightY = cosf(rad);
float len = sqrtf(hsp*hsp + vsp*vsp);
if(len > 0){
pos.x += (forwardX * vsp + rightX * hsp) / len * spd * GetFrameTime();
pos.y += (forwardY * vsp + rightY * hsp) / len * spd * GetFrameTime();
}
Vector2 mouseDelta = GetMouseDelta();
angle += mouseDelta.x * mouseSensivity;
if(angle >= 360) angle -= 360;
if(angle < 0.0f) angle += 360;
}
int obstacels[H][W] ={
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1},
{1,0,0,1,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1},
{1,0,0,1,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1},
{1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1},
{1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,1,1,0,0,1},
{1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1},
{1,1,1,1,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,1},
{1,0,0,0,0,0,1,1,1,1,0,0,0,0,1,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};
void SetRays(){
float rayDensity = 0.0f;
for(int i = 0; i < nRays; i++){
ray[i].angle = rayDensity;
rayDensity += density;
}
}
Vector2 mousePos = GetMousePosition();
bool moveMouse = false;
void RotateRays(){
mousePos = GetMousePosition();
for(int i = 0; i < nRays; i++){
ray[i].angleRot = ray[i].angle + angle-(nRays*density)/2;
}
}
void DrawRays(){
for(int i = 0; i < nRays; i++){
float rad = ray[i].angleRot * DEG2RAD;
Vector2 end = {
pos.x + cosf(rad) * ray[i].dist,
pos.y + sinf(rad) * ray[i].dist
};
DrawLineEx(pos, end, 4,RED);
}
}
void DrawMap(){
for(int i = 0; i < W; i++){
for(int j = 0; j < H; j++){
if(obstacels[j][i] == 1){
DrawRectangleV({ (float)(i*cellSize + (screenWidth/2 - (W*cellSize)/2)), (float)(j*cellSize + (screenHeight/2 - (H*cellSize)/2)) }, { (float)cellSize, (float)cellSize }, BLUE);
}
}
}
}
void StepRays(){
float stepDist = 1;
float maxDistance = 2000.0f;
for(int k = 0; k < nRays; k++){
Vector2 end = {};
float rad = ray[k].angleRot * DEG2RAD;
ray[k].dist = 0.0f;
bool hit = false;
while(!hit && ray[k].dist < maxDistance){
ray[k].dist += stepDist;
end = {
pos.x + cosf(rad) * ray[k].dist,
pos.y + sinf(rad) * ray[k].dist
};
for(int i = 0; i < W; i++){
for(int j = 0; j < H; j++){
if(obstacels[j][i] == 0) continue;
if(CheckCollisionPointRec(end, { (float)(i*cellSize + (screenWidth/2 - (W*cellSize)/2)), (float)(j*cellSize + (screenHeight/2 - (H*cellSize)/2)), (float)cellSize, (float)cellSize })){
hit = true;
}
}
}
}
}
}
int Hcol = 400;
void raycasting(){
float Wcol = (float)screenWidth/nRays;
for(int k = 0; k < nRays; k++){
float relativeAngle = (ray[k].angleRot - angle) * DEG2RAD;
float correctDist = (ray[k].dist * cosf(relativeAngle))/2;
float opacity = 255 - correctDist;
opacity = std::clamp(opacity, 0.0f, 255.0f);
float HeightCol = cellSize/correctDist;
HeightCol *= Hcol;
HeightCol = std::clamp(HeightCol, 5.0f, (float)Hcol);
DrawRectangle(k*Wcol, screenHeight/2 - HeightCol/2, Wcol+1, HeightCol, BLACK);
DrawRectangle(k*Wcol, screenHeight/2 - HeightCol/2, Wcol+1, HeightCol, (Color){0,121,241, opacity});
}
}
Texture2D gun;
void LoadTextures(){
gun = LoadTexture("textures/gun.png");
}
void DrawGun(){
DrawTextureEx(gun, {screenWidth- gun.width, screenHeight/2 - gun.height/2}, 0.0f, 1.0f, WHITE);
}
void DrawCrossair(){
DrawCircle(screenWidth/2, screenHeight/2, 2, (Color){255,255,255, 120});
}
void Floor(){
DrawRectangleV({0, screenHeight/2}, {screenWidth, screenHeight/2}, (Color){0,131,241, 255});
DrawRectangleV({0, 0}, {screenWidth, screenHeight/2}, (Color){0,100, 190, 255});
}