Skip to content

Commit 61224aa

Browse files
committed
Jump and crouch
1 parent 0ef7d39 commit 61224aa

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,11 @@ commit bb0c927871fbf0d17798a698c4c453b262d15436
4949

5050
# Step 5: Doors
5151

52-
commit f5980c0319457d9fc188d3ac40d5f0643f586973
52+
commit f5980c0319457d9fc188d3ac40d5f0643f586973
53+
54+
# Step 6: Look up and down
55+
56+
commit 0ef7d398abb09a70e6952e61d83cc6d2b70f29b7
57+
58+
# Step 7: Jump and crouch
59+

raycaster_sprites.cpp

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ double spriteDistance[numSprites];
124124

125125
double lookVert = 0;
126126

127+
double eyePos = 0;
128+
127129
// lookVert should have values between -LOOK_UP_MAX and LOOK_UP_MAX
128130
#define LOOK_UP_MAX 128
129131

@@ -338,7 +340,7 @@ int main(int /*argc*/, char */*argv*/[])
338340

339341
// Horizontal distance from the camera to the floor for the current row.
340342
// 0.5 is the z position exactly in the middle between floor and ceiling.
341-
float rowDistance = posZ / p;
343+
float rowDistance = (posZ + eyePos) / p;
342344

343345
// calculate the real world step vector we have to add for each x (parallel to camera plane)
344346
// adding step by step avoids multiplications with a weight in the inner loop
@@ -396,7 +398,7 @@ int main(int /*argc*/, char */*argv*/[])
396398

397399
// Horizontal distance from the camera to the floor for the current row.
398400
// 0.5 is the z position exactly in the middle between floor and ceiling.
399-
float rowDistance = posZ / p;
401+
float rowDistance = (posZ - eyePos) / p;
400402

401403
// calculate the real world step vector we have to add for each x (parallel to camera plane)
402404
// adding step by step avoids multiplications with a weight in the inner loop
@@ -538,8 +540,8 @@ int main(int /*argc*/, char */*argv*/[])
538540
int lineHeight = (int)(h / perpWallDist);
539541

540542
//calculate lowest and highest pixel to fill in current stripe
541-
int drawStart = -lineHeight / 2 + h / 2 + lookVert;
542-
int drawEnd = lineHeight / 2 + h / 2 + lookVert;
543+
int drawStart = -lineHeight / 2 + h / 2 + lookVert + eyePos/perpWallDist;
544+
int drawEnd = lineHeight / 2 + h / 2 + lookVert + eyePos/perpWallDist;
543545

544546
//calculate value of wallX
545547
double wallX; //where exactly the wall was hit
@@ -644,8 +646,8 @@ int main(int /*argc*/, char */*argv*/[])
644646
//calculate height of the sprite on screen
645647
int spriteHeight = abs(int(h / (transformY))) / vDiv; //using "transformY" instead of the real distance prevents fisheye
646648
//calculate lowest and highest pixel to fill in current stripe
647-
int drawStartY = -spriteHeight / 2 + h / 2 + vMoveScreen + lookVert;
648-
int drawEndY = spriteHeight / 2 + h / 2 + vMoveScreen + lookVert;
649+
int drawStartY = -spriteHeight / 2 + h / 2 + vMoveScreen + lookVert + eyePos/transformY;
650+
int drawEndY = spriteHeight / 2 + h / 2 + vMoveScreen + lookVert + eyePos/transformY;
649651

650652
//calculate width of the sprite
651653
int spriteWidth = abs(int (h / (transformY))) / uDiv; // same as height of sprite, given that it's square
@@ -804,6 +806,30 @@ int main(int /*argc*/, char */*argv*/[])
804806
lookVert = 0;
805807
}
806808
}
809+
if(keyDown(SDLK_x)) {
810+
if(eyePos == 0)
811+
eyePos = 128;
812+
} else if(keyDown(SDLK_c)) {
813+
if(eyePos <= 0 && eyePos > -128) {
814+
eyePos -= frameTime * 400;
815+
if(eyePos < -128) {
816+
eyePos = -128;
817+
}
818+
}
819+
} else {
820+
if(eyePos < 0) {
821+
eyePos += frameTime * 400;
822+
if(eyePos > 0) {
823+
eyePos = 0;
824+
}
825+
}
826+
}
827+
if(eyePos > 0) {
828+
eyePos -= frameTime * 400;
829+
if(eyePos < 0) {
830+
eyePos = 0;
831+
}
832+
}
807833
if(keyDown(SDLK_ESCAPE))
808834
{
809835
break;

0 commit comments

Comments
 (0)