-
Notifications
You must be signed in to change notification settings - Fork 0
/
Raycaster.pde
181 lines (145 loc) · 5.08 KB
/
Raycaster.pde
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
class Raycaster
{
DungeonMap dungeonMap;
float posX = 25;
float posY = 23;
float dirX = -1;
float dirY = 0;
float planeX = 0;
float planeY = 0.66;
float time = 0;
float oldTime = 0;
int currentFloor = 0;
int renderAlpha = 255;
void init()
{
println( "Raycaster Init " + currentFloor );
dungeonMap = new DungeonMap();
dungeonMap.init( currentFloor );
posX = dungeonMap.startX;
posY = dungeonMap.startY;
}
void update()
{
// implement this
}
void draw()
{
for(int x = 0; x < 800; x++)
{
float cameraX = 2 * x / (float) 800 - 1; //x-coordinate in camera space
float rayPosX = posX;
float rayPosY = posY;
float rayDirX = dirX + planeX * cameraX;
float rayDirY = dirY + planeY * cameraX;
int mapX = ( int )rayPosX;
int mapY = ( int )rayPosY;
float sideDistX;
float sideDistY;
float deltaDistX = sqrt( 1 + ( rayDirY * rayDirY ) / ( rayDirX * rayDirX ) );
float deltaDistY = sqrt( 1 + ( rayDirX * rayDirX ) / ( rayDirY * rayDirY ) );
float perpWallDist;
int stepX;
int stepY;
int hit = 0;
int side = 0;
if ( rayDirX < 0 )
{
stepX = -1;
sideDistX = (rayPosX - mapX) * deltaDistX;
}
else
{
stepX = 1;
sideDistX = (mapX + 1.0 - rayPosX) * deltaDistX;
}
if (rayDirY < 0)
{
stepY = -1;
sideDistY = (rayPosY - mapY) * deltaDistY;
}
else
{
stepY = 1;
sideDistY = (mapY + 1.0 - rayPosY) * deltaDistY;
}
while (hit == 0)
{
if (sideDistX < sideDistY)
{
sideDistX += deltaDistX;
mapX += stepX;
side = 0;
}
else
{
sideDistY += deltaDistY;
mapY += stepY;
side = 1;
}
if ( dungeonMap.tileMap[mapX][mapY].blocked ) hit = 1;
if ( dungeonMap.tileMap[mapX][mapY].blockSight ) hit = 1;
}
if (side == 0)
perpWallDist = abs((mapX - rayPosX + (1 - stepX) / 2) / rayDirX);
else
perpWallDist = abs((mapY - rayPosY + (1 - stepY) / 2) / rayDirY);
int lineHeight = abs(int(600 / perpWallDist));
int drawStart = -lineHeight / 2 + 600 / 2;
if(drawStart < 0)drawStart = 0;
int drawEnd = lineHeight / 2 + 600 / 2;
if(drawEnd >= 600)drawEnd = 600 - 1;
color wallColor = color( 0, 0, 255 );
if ( dungeonMap.tileMap[mapX][mapY].blocked )
//wallColor = color( 0, 0, 255 );
wallColor = color( 232, 146, 146 );
if ( dungeonMap.tileMap[mapX][mapY].exits )
wallColor = color( 0, 176, 246 );
if (side == 1) {wallColor = color( red(wallColor) / 2, green(wallColor) / 2, blue(wallColor) / 2 );}
stroke( wallColor, renderAlpha );
line( x, drawStart, x, drawEnd );
}
}
void moveForward()
{
if ( dungeonMap.tileMap[int(posX + dirX * 0.5)][int(posY)].exits )
{
changeFloor();
}
if ( !dungeonMap.tileMap[int(posX + dirX * 0.5)][int(posY)].blocked ) posX += dirX * 0.5;
if ( !dungeonMap.tileMap[int(posX)][int(posY + dirY * 0.5)].blocked ) posY += dirY * 0.5;
}
void moveBackwards()
{
if ( !dungeonMap.tileMap[int(posX - dirX * 0.5)][int(posY)].blocked ) posX -= dirX * 0.5;
if ( !dungeonMap.tileMap[int(posX)][int(posY - dirY * 0.5)].blocked ) posY -= dirY * 0.5;
}
void rotateLeft()
{
float oldDirX = dirX;
float rotSpeed = 0.1;
dirX = dirX * cos(rotSpeed) - dirY * sin(rotSpeed);
dirY = oldDirX * sin(rotSpeed) + dirY * cos(rotSpeed);
float oldPlaneX = planeX;
planeX = planeX * cos(rotSpeed) - planeY * sin(rotSpeed);
planeY = oldPlaneX * sin(rotSpeed) + planeY * cos(rotSpeed);
}
void rotateRight()
{
float oldDirX = dirX;
float rotSpeed = 0.1;
dirX = dirX * cos(-rotSpeed) - dirY * sin(-rotSpeed);
dirY = oldDirX * sin(-rotSpeed) + dirY * cos(-rotSpeed);
float oldPlaneX = planeX;
planeX = planeX * cos(-rotSpeed) - planeY * sin(-rotSpeed);
planeY = oldPlaneX * sin(-rotSpeed) + planeY * cos(-rotSpeed);
}
void changeFloor()
{
//println( "Changing floor" );
currentFloor++;
if ( currentFloor == 8 )
return;
init();
}
}