repl.it
@uint9000_t/

Simple Ray Casting

C++

Crappy ray casting algorithm. Works like Wolfenstein 3D in that is has only one ray per column.

fork
loading
Files
  • main.cpp
  • funcs.cpp
  • funcs.h
  • leveldat.cpp
  • leveldat.h
  • main
  • types.h
main.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
#include <iostream>
#include <cmath>
#include <string>

#include "types.h"
#include "funcs.h"
//#include "leveldat.h"

int main() {
  Player person1;
  person1.pos.x = 0; //Position of eye, x coord
  person1.pos.y = 0; //Position of eye, y coord
  person1.viewDepth = 16; //Distance of vision
  person1.viewAngle = -20; //Direction of view
  person1.viewRange = 90; //FOV
  Point viewPoint = getPointFromAngle(person1.viewAngle, person1);
  Segment sightline{person1.pos, viewPoint};
  Segment hitline{{4, -3}, {9, 7}};
  
  Point hitPoint = intersectPt(sightline, hitline);
  double dToWall = deltaPts(person1.pos, hitPoint);
  
  std::cout << "Position of eye: ( " << person1.pos.x << " , " << person1.pos.y << " )\n";
  std::cout << "Point of view: ( " << viewPoint.x << " , " << viewPoint.y << " )\n";
  std::cout << "Point of wall: ( " << hitPoint.x << " , " << hitPoint.y << " )\n";
  std::cout << "1 = is on wall and in range: " << (onSegment(hitPoint, sightline) && onSegment(hitPoint, hitline)) << "\n";
  std::cout << "Distance between eye and wall: " << dToWall << "\n\n";
  
  ScreenArray dists = viewDepthOverRange(person1, hitline);
  //ScreenArray is because I was too lazy to mess with returning pointers to arrays

  ScreenArray degOfVision = degreesVisionFromDepth(dists, 1);
  char pixRow;
  std::cout << "=============================================================\n";
  for (int i=0; i<SCREENHEIGHT; i++) {
    std::cout << '|';
    getGraphicalString(i, degOfVision, person1, &pixRow);
    for (int j = 0; j < SCREENWIDTH; j++) {
      std::cout << *(&pixRow + j);
    }
    std::cout << "|\n";
  }
  std::cout << "=====================================================\n\n";

  return 0;
}

?