Advertisement
Guest User

Untitled

a guest
Dec 29th, 2015
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 17.59 KB | None | 0 0
  1. #include <cmath>
  2. #include <string>
  3. #include <vector>
  4. #include <iostream>
  5. #include "palrender.h"
  6.  
  7. #define screenWidth 320
  8. #define screenHeight 200
  9. #define texWidth 64
  10. #define texHeight 64
  11. #define mapWidth 24
  12. #define mapHeight 24
  13.  
  14. unsigned char texture[11][4096];
  15.  
  16. struct wallType
  17. {
  18.     int texture[4];
  19.     bool solid[4];
  20. };
  21.  
  22.   double posX = 22.0, posY = 11.5; //x and y start position
  23.   double dirX = -1.0, dirY = 0.0; //initial direction vector
  24.   double planeX = 0.0, planeY = 0.66; //the 2d raycaster version of camera plane
  25.  
  26.   double time = 0; //time of current frame
  27.   double oldTime = 0; //time of previous frame
  28.   double moveSpeed = (1.0/60.0) * 3.0; //the constant value is in squares/second
  29.   double rotSpeed =  (1.0/60.0) * 2.0; //the constant value is in radians/second
  30.  
  31. int worldMap[mapWidth][mapHeight] =
  32. {
  33.   {8,8,8,8,8,8,8,8,8,8,8,4,4,6,4,4,6,4,6,4,4,4,6,4},
  34.   {8,0,0,0,0,0,0,0,0,0,8,4,0,0,0,0,0,0,0,0,0,0,0,4},
  35.   {8,0,3,3,0,0,0,0,0,8,8,4,0,0,0,0,0,0,0,0,0,0,0,6},
  36.   {8,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6},
  37.   {8,0,3,3,0,0,0,0,0,8,8,4,0,0,0,0,0,0,0,0,0,0,0,4},
  38.   {8,0,0,0,0,0,0,0,0,0,8,4,0,0,0,0,0,6,6,6,0,6,4,6},
  39.   {8,8,8,8,0,8,8,8,8,8,8,4,4,4,4,4,4,6,0,0,0,0,0,6},
  40.   {7,7,7,7,0,7,7,7,7,0,8,0,8,0,8,0,8,4,0,4,0,6,0,6},
  41.   {7,7,0,0,0,0,0,0,7,8,0,8,0,8,0,8,8,6,0,0,0,0,0,6},
  42.   {7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,6,0,0,0,0,0,4},
  43.   {7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,6,0,6,0,6,0,6},
  44.   {7,7,0,0,0,0,0,0,7,8,0,8,0,8,0,8,8,6,4,6,0,6,6,6},
  45.   {7,7,7,7,0,7,7,7,7,8,8,4,0,6,8,4,8,3,3,3,0,3,3,3},
  46.   {2,2,2,2,0,2,2,2,2,4,6,4,0,0,6,0,6,3,0,0,0,0,0,3},
  47.   {2,2,0,0,0,0,0,2,2,4,0,0,0,0,0,0,4,3,0,0,0,0,0,3},
  48.   {2,0,0,0,0,0,0,0,2,4,0,0,0,0,0,0,4,3,0,0,0,0,0,3},
  49.   {1,0,0,0,0,0,0,0,1,4,4,4,4,4,6,0,6,3,3,0,0,0,3,3},
  50.   {2,0,0,0,0,0,0,0,2,2,2,1,2,2,2,6,6,0,0,5,0,5,0,5},
  51.   {2,2,0,0,0,0,0,2,2,2,0,0,0,2,2,0,5,0,5,0,0,0,5,5},
  52.   {2,0,0,0,0,0,0,0,2,0,0,0,0,0,2,5,0,5,0,5,0,5,0,5},
  53.   {1,0,0,10,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5},
  54.   {2,0,0,0,0,0,0,0,2,0,0,0,0,0,2,5,0,5,0,5,0,5,0,5},
  55.   {2,2,0,0,0,0,0,2,2,2,0,0,0,2,2,0,5,0,5,0,0,0,5,5},
  56.   {2,2,2,2,1,2,2,2,2,2,2,1,2,2,2,5,5,5,5,5,5,5,5,5}
  57. };
  58.  
  59. int lightMap[mapWidth][mapHeight] =
  60. {
  61.   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  62.   {0,1,1,2,3,4,5,4,3,2,0,0,6,7,7,7,7,7,7,7,7,7,7,0},
  63.   {0,1,0,3,4,5,6,5,4,0,0,0,6,7,7,7,7,7,7,7,7,7,7,0},
  64.   {0,2,3,4,5,6,7,6,5,4,3,2,6,7,7,7,7,7,7,7,7,7,7,0},
  65.   {0,1,0,0,4,5,6,5,4,0,0,0,6,7,7,7,7,7,7,7,7,7,7,0},
  66.   {0,1,1,2,3,4,5,4,1,1,0,0,5,6,6,6,6,0,0,0,6,0,0,0},
  67.   {0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,3,4,5,4,3,0},
  68.   {0,0,0,0,5,0,0,0,0,1,0,1,0,1,0,1,0,0,1,0,4,0,2,0},
  69.   {0,0,4,5,6,5,2,3,0,0,1,0,6,0,4,0,0,0,1,2,3,2,1,0},
  70.   {0,4,5,6,7,6,5,4,3,4,5,6,7,6,5,3,0,0,1,1,2,1,1,0},
  71.   {0,4,5,6,7,6,5,4,3,4,5,6,7,6,5,3,0,0,1,0,3,0,1,0},
  72.   {0,0,4,5,6,5,4,3,0,0,1,0,6,0,4,0,0,0,0,0,4,0,0,0},
  73.   {0,0,0,0,5,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,5,0,0,0},
  74.   {0,0,0,0,4,0,0,0,0,0,0,0,4,3,0,1,0,0,4,5,6,5,4,0},
  75.   {0,0,1,2,3,2,1,0,0,0,1,2,3,2,1,1,0,0,5,6,7,6,5,0},
  76.   {0,1,2,3,4,3,2,1,0,0,1,1,2,1,1,1,0,0,4,5,6,5,4,0},
  77.   {0,2,3,4,5,4,3,2,0,0,0,0,0,0,0,1,0,0,0,4,5,4,0,0},
  78.   {0,3,4,5,6,5,4,3,0,0,0,0,0,0,0,0,0,1,1,0,4,0,1,0},
  79.   {0,0,4,6,7,6,5,0,0,0,4,5,4,0,0,1,0,1,0,2,3,2,0,0},
  80.   {0,3,4,5,6,5,4,3,0,4,5,6,5,4,0,0,1,0,1,0,2,0,1,0},
  81.   {0,2,3,4,5,4,3,3,4,5,6,7,6,5,4,3,2,1,1,1,1,1,1,0},
  82.   {0,1,2,3,4,3,2,2,0,4,5,6,5,4,0,0,1,0,1,0,1,0,1,0},
  83.   {0,0,1,2,3,2,1,0,0,0,4,5,4,0,0,1,0,1,0,1,1,1,0,0},
  84.   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
  85. };
  86.  
  87. struct Sprite
  88. {
  89.   double x;
  90.   double y;
  91.   int texture;
  92. };
  93.  
  94. #define numSprites 19
  95.  
  96. Sprite sprite[numSprites] =
  97. {
  98.   {20.5, 11.5, 10}, //green light in front of playerstart
  99.   //green lights in every room
  100.   {18.5,4.5, 10},
  101.   {10.0,4.5, 10},
  102.   {10.0,12.5,10},
  103.   {3.5, 6.5, 10},
  104.   {3.5, 20.5,10},
  105.   {3.5, 14.5,10},
  106.   {14.5,20.5,10},
  107.  
  108.   //row of pillars in front of wall: fisheye test
  109.   {18.5, 10.5, 9},
  110.   {18.5, 11.5, 9},
  111.   {18.5, 12.5, 9},
  112.  
  113.   //some barrels around the map
  114.   {21.5, 1.5, 8},
  115.   {15.5, 1.5, 8},
  116.   {16.0, 1.8, 8},
  117.   {16.2, 1.2, 8},
  118.   {3.5,  2.5, 8},
  119.   {9.5, 15.5, 8},
  120.   {10.0, 15.1,8},
  121.   {10.5, 15.8,8},
  122. };
  123.  
  124. //1D Zbuffer
  125. double ZBuffer[screenWidth][screenHeight];
  126.  
  127. //arrays used to sort the sprites
  128. int spriteOrder[numSprites];
  129. double spriteDistance[numSprites];
  130.  
  131. //function used to sort the sprites
  132. void combSort(int* order, double* dist, int amount);
  133.  
  134. void MakeTextures (int slot)
  135. {
  136.     BITMAP * texspr = engine->GetSpriteGraphic (slot);
  137.     unsigned char **texbuffer = engine->GetRawBitmapSurface (texspr);
  138.   for(int x = 0; x < texWidth; x++)
  139.     for(int y = 0; y < texHeight; y++)
  140.         {
  141.  
  142.             for (int num=0;num<11;num++)
  143.             {
  144.                 texture[num][texWidth * y + x] = texbuffer [y][x+texWidth*num];  //flat red texture with black cross
  145.             }
  146.     }
  147. }
  148.  
  149. void Raycast_Render (int slot)
  150. {
  151.     long w=0,h=0;
  152.     BITMAP *screen = engine->GetSpriteGraphic (slot);
  153.     engine->GetBitmapDimensions (screen,&w,&h,NULL);
  154.     unsigned char** buffer = engine->GetRawBitmapSurface (screen);
  155.     for (int x = 0;x<w;x++)
  156.     {
  157.         for (int y=0;y<h;y++)
  158.         {
  159.             ZBuffer[x][y] = 0;
  160.         }
  161.     }
  162.   //start the main loop
  163.     for(int x = 0; x < w; x++)
  164.     {
  165.       //calculate ray position and direction
  166.       double cameraX = 2 * x / double(w) - 1; //x-coordinate in camera space    
  167.       double rayPosX = posX;
  168.       double rayPosY = posY;
  169.       double rayDirX = dirX + planeX * cameraX;
  170.       double rayDirY = dirY + planeY * cameraX;
  171.      
  172.       //which box of the map we're in  
  173.       int mapX = int(rayPosX);
  174.       int mapY = int(rayPosY);
  175.        
  176.       //length of ray from current position to next x or y-side
  177.       double sideDistX;
  178.       double sideDistY;
  179.        
  180.       //length of ray from one x or y-side to next x or y-side
  181.       double deltaDistX = sqrt(1 + (rayDirY * rayDirY) / (rayDirX * rayDirX));
  182.       double deltaDistY = sqrt(1 + (rayDirX * rayDirX) / (rayDirY * rayDirY));
  183.       double perpWallDist;
  184.        
  185.       //what direction to step in x or y-direction (either +1 or -1)
  186.       int stepX;
  187.       int stepY;
  188.       int prevmapX=0;
  189.       int prevmapY=0;
  190.       int hit = 0; //was there a wall hit?
  191.       int side; //was a NS or a EW wall hit?
  192.      
  193.       //calculate step and initial sideDist
  194.       if (rayDirX < 0)
  195.       {
  196.         stepX = -1;
  197.         sideDistX = (rayPosX - mapX) * deltaDistX;
  198.       }
  199.       else
  200.       {
  201.         stepX = 1;
  202.         sideDistX = (mapX + 1.0 - rayPosX) * deltaDistX;
  203.       }
  204.       if (rayDirY < 0)
  205.       {
  206.         stepY = -1;
  207.         sideDistY = (rayPosY - mapY) * deltaDistY;
  208.       }
  209.       else
  210.       {
  211.         stepY = 1;
  212.         sideDistY = (mapY + 1.0 - rayPosY) * deltaDistY;
  213.       }
  214.       //perform DDA
  215.       bool deeper = true;
  216.       bool opposite = false;
  217.       double wallX; //where exactly the wall was hit
  218.       int drawStart;
  219.       int drawEnd;
  220.       while (hit == 0 && deeper == true)
  221.       {
  222.         if (opposite)
  223.         {
  224.             rayDirX = rayDirX * (-1);
  225.             rayDirY = rayDirY * (-1);
  226.             stepX = stepX * (-1);
  227.             stepY = stepY * (-1);
  228.         }
  229.         else if (sideDistX < sideDistY) //jump to next map square, OR in x-direction, OR in y-direction
  230.         {
  231.           sideDistX += deltaDistX;
  232.           mapX += stepX;
  233.           side = 0;
  234.         }
  235.         else
  236.         {
  237.           sideDistY += deltaDistY;
  238.           mapY += stepY;
  239.           side = 1;
  240.         }
  241.         //Check if ray has hit a wall      
  242.         if (worldMap[mapX][mapY] > 0)
  243.         {
  244.             hit = 1; //Set this to true so that by default, it's impossible to hang the engine.
  245.             deeper = false; //Set this to false so that we don't go deeper than we need to.
  246.  
  247.             //Calculate distance of perpendicular ray (oblique distance will give fisheye effect!)    
  248.             if (side == 0) perpWallDist = fabs((mapX - rayPosX + (1 - stepX) / 2) / rayDirX);
  249.             else       perpWallDist = fabs((mapY - rayPosY + (1 - stepY) / 2) / rayDirY);
  250.      
  251.              //Calculate height of line to draw on screen      
  252.              int lineHeight = abs(int(h / perpWallDist));
  253.              
  254.              //calculate lowest and highest pixel to fill in current stripe
  255.              drawStart = -lineHeight / 2 + h /2;
  256.              if(drawStart < 0) drawStart = 0;
  257.              drawEnd = lineHeight / 2 + h / 2;
  258.              if(drawEnd >= h) drawEnd = h - 1;
  259.              //texturing calculations
  260.              int texNum = worldMap[mapX][mapY] - 1; //1 subtracted from it so that texture 0 can be used!
  261.              
  262.              //calculate value of wallX
  263.              if (side == 1) wallX = rayPosX + ((mapY - rayPosY + (1 - stepY) / 2) / rayDirY) * rayDirX;
  264.              else       wallX = rayPosY + ((mapX - rayPosX + (1 - stepX) / 2) / rayDirX) * rayDirY;
  265.              wallX -= floor((wallX));
  266.              
  267.              //x coordinate on the texture
  268.              int wall_light=0;
  269.              int texX = int(wallX * double(texWidth));
  270.              if(side == 0 && rayDirX > 0) texX = texWidth - texX - 1;
  271.              if(side == 1 && rayDirY < 0) texX = texWidth - texX - 1;
  272.              if (rayDirX > 0 && side == 0) wall_light = 255 / (8-lightMap [(int)mapX-1][(int)mapY]);
  273.              if (rayDirX < 0 && side == 0) wall_light = 255 / (8-lightMap [(int)mapX+1][(int)mapY]);
  274.              if (rayDirY > 0 && side == 1) wall_light = 255 / (8-lightMap [(int)mapX][(int)mapY-1]);
  275.              if (rayDirY < 0 && side == 1) wall_light = 255 / (8-lightMap [(int)mapX][(int)mapY+1]);
  276.              for(int y = drawStart; y < drawEnd; y++)
  277.                 {
  278.                     if (ZBuffer[x][y] > perpWallDist || ZBuffer[x][y] == 0) //We can draw.
  279.                     {
  280.                     int d = y * 256 - h * 128 + lineHeight * 128; //256 and 128 factors to avoid floats
  281.                     int texY = ((d * texHeight) / lineHeight) / 256;
  282.                     int color = texture[texNum][texWidth * texY + texX];
  283.                     if (color > 0)
  284.                         {
  285.                             color = MixColorAlpha (color,GetColor565(0,0,10),wall_light);
  286.                             buffer[y][x] = color;
  287.                             //SET THE ZBUFFER FOR THE SPRITE CASTING
  288.                             ZBuffer[x][y] = perpWallDist; //perpendicular distance is used
  289.                         }
  290.                     else
  291.                         {
  292.                             //We found transparency, we have to draw deeper.
  293.                             deeper = true;
  294.                             hit = 0;
  295.                         }
  296.                     }
  297.                 }
  298.              if (opposite)
  299.              {
  300.                  opposite = false;
  301.                  rayDirX = rayDirX * (-1);
  302.                  rayDirY = rayDirY * (-1);
  303.                  stepX = stepX * (-1);
  304.                  stepY = stepY * (-1);
  305.              }
  306.              else if (!opposite && deeper && (prevmapX != mapX && prevmapY != mapY))
  307.              {
  308.                  opposite = true;
  309.                  prevmapX = mapX;
  310.                  prevmapY = mapY;
  311.              }
  312.             //End of wall drawing functions.
  313.             }
  314.         //End of loop.
  315.         }
  316.      
  317.       //FLOOR CASTING
  318.       double floorXWall, floorYWall; //x, y position of the floor texel at the bottom of the wall
  319.       //4 different wall directions possible
  320.       if(side == 0 && rayDirX > 0)
  321.       {
  322.         floorXWall = mapX;
  323.         floorYWall = mapY + wallX;
  324.       }
  325.       else if(side == 0 && rayDirX < 0)
  326.       {
  327.         floorXWall = mapX + 1.0;
  328.         floorYWall = mapY + wallX;
  329.       }
  330.       else if(side == 1 && rayDirY > 0)
  331.       {
  332.         floorXWall = mapX + wallX;
  333.         floorYWall = mapY;
  334.       }
  335.       else
  336.       {
  337.         floorXWall = mapX + wallX;
  338.         floorYWall = mapY + 1.0;
  339.       }
  340.      
  341.       double distWall, distPlayer, currentDist;
  342.  
  343.       distWall = perpWallDist;
  344.       distPlayer = 0.0;
  345.       if (drawEnd < 0) drawEnd = h; //becomes < 0 when the integer overflows
  346.       //draw the floor from drawEnd to the bottom of the screen
  347.       for(int y = drawEnd; y < h; y++)
  348.       {
  349.         currentDist = h / (2.0 * y - h); //you could make a small lookup table for this instead
  350.         if (currentDist < ZBuffer[x][y] || ZBuffer[x][y] == 0)
  351.         {
  352.             double weight = (currentDist - distPlayer) / (distWall - distPlayer);
  353.          
  354.             double currentFloorX = weight * floorXWall + (1.0 - weight) * posX;
  355.             double currentFloorY = weight * floorYWall + (1.0 - weight) * posY;
  356.        
  357.             int floorTexX, floorTexY;
  358.             int lighting = 255 / (8-lightMap [(int)currentFloorX][(int)currentFloorY]);
  359.             floorTexX = int(currentFloorX * texWidth) % texWidth;
  360.             floorTexY = int(currentFloorY * texHeight) % texHeight;
  361.        
  362.             //floor
  363.             buffer[y][x] = MixColorAlpha(texture[3][texWidth * floorTexY + floorTexX],GetColor565(0,0,10),lighting);
  364.             //ceiling (symmetrical!)
  365.             buffer[h-y][x] = MixColorAlpha(texture[6][texWidth * floorTexY + floorTexX],GetColor565(0,0,10),lighting);
  366.             //SET THE ZBUFFER FOR THE SPRITE CASTING
  367.             ZBuffer[x][y] = currentDist; //perpendicular distance is used
  368.             ZBuffer[x][h-y] = currentDist; //perpendicular distance is used
  369.         }
  370.       }
  371.     }
  372.    
  373.    
  374.  
  375.     //SPRITE CASTING
  376.     //sort sprites from far to close
  377.  
  378.     for(int i = 0; i < numSprites; i++)
  379.     {
  380.       spriteOrder[i] = i;
  381.       spriteDistance[i] = ((posX - sprite[i].x) * (posX - sprite[i].x) + (posY - sprite[i].y) * (posY - sprite[i].y)); //sqrt not taken, unneeded
  382.     }
  383.     combSort(spriteOrder, spriteDistance, numSprites);
  384.      
  385.     //after sorting the sprites, do the projection and draw them
  386.     for(int i = 0; i < numSprites; i++)
  387.     {
  388.       //translate sprite position to relative to camera
  389.       double spriteX = sprite[spriteOrder[i]].x - posX;
  390.       double spriteY = sprite[spriteOrder[i]].y - posY;
  391.          
  392.       //transform sprite with the inverse camera matrix
  393.       // [ planeX   dirX ] -1                                       [ dirY      -dirX ]
  394.       // [               ]       =  1/(planeX*dirY-dirX*planeY) *   [                 ]
  395.       // [ planeY   dirY ]                                          [ -planeY  planeX ]
  396.      
  397.       double invDet = 1.0 / (planeX * dirY - dirX * planeY); //required for correct matrix multiplication
  398.      
  399.       double transformX = invDet * (dirY * spriteX - dirX * spriteY);
  400.       double transformY = invDet * (-planeY * spriteX + planeX * spriteY); //this is actually the depth inside the screen, that what Z is in 3D      
  401.            
  402.       int spriteScreenX = int((w / 2) * (1 + transformX / transformY));
  403.      
  404.       //parameters for scaling and moving the sprites
  405.       #define uDiv 1
  406.       #define vDiv 1
  407.       #define vMove 0.0
  408.       int vMoveScreen = int(vMove / transformY);
  409.      
  410.       //calculate height of the sprite on screen
  411.       int spriteHeight = abs(int(h / (transformY))) / vDiv; //using "transformY" instead of the real distance prevents fisheye
  412.       //calculate lowest and highest pixel to fill in current stripe
  413.       int drawStartY = -spriteHeight / 2 + h / 2 + vMoveScreen;
  414.       if(drawStartY < 0) drawStartY = 0;
  415.       int drawEndY = spriteHeight / 2 + h / 2 + vMoveScreen;
  416.       if(drawEndY >= h) drawEndY = h - 1;
  417.      
  418.       //calculate width of the sprite
  419.       int spriteWidth = abs( int (h / (transformY))) / uDiv;
  420.       int drawStartX = -spriteWidth / 2 + spriteScreenX;
  421.       if(drawStartX < 0) drawStartX = 0;
  422.       int drawEndX = spriteWidth / 2 + spriteScreenX;
  423.       if(drawEndX >= w) drawEndX = w - 1;
  424.       int spr_light = 255 / (8-lightMap [(int)sprite[spriteOrder[i]].x][(int)sprite[spriteOrder[i]].y]);
  425.       //loop through every vertical stripe of the sprite on screen
  426.       for(int stripe = drawStartX; stripe < drawEndX; stripe++)
  427.       {
  428.         int texX = int(256 * (stripe - (-spriteWidth / 2 + spriteScreenX)) * texWidth / spriteWidth) / 256;
  429.         //the conditions in the if are:
  430.         //1) it's in front of camera plane so you don't see things behind you
  431.         //2) it's on the screen (left)
  432.         //3) it's on the screen (right)
  433.         //4) ZBuffer, with perpendicular distance
  434.         if(transformY > 0 && stripe > 0 && stripe < w)
  435.         for(int y = drawStartY; y < drawEndY; y++) //for every pixel of the current stripe
  436.         {
  437.           if (transformY < ZBuffer[stripe][y])
  438.           {
  439.             int d = (y-vMoveScreen) * 256 - h * 128 + spriteHeight * 128; //256 and 128 factors to avoid floats
  440.             int texY = ((d * texHeight) / spriteHeight) / 256;
  441.             unsigned char color = texture[sprite[spriteOrder[i]].texture][texWidth * texY + texX]; //get current color from the texture
  442.             if (color !=0)
  443.             {
  444.                   color = MixColorAlpha (color,GetColor565(0,0,10),spr_light);
  445.                   buffer[y][stripe] = color; //paint pixel if it isn't black, black is the invisible color
  446.           }
  447.           }
  448.         }
  449.       }
  450.      
  451.     }
  452.     engine->ReleaseBitmapSurface (screen);
  453.     engine->NotifySpriteUpdated (slot);
  454. }
  455.  
  456.  
  457. void MoveForward ()
  458. {
  459.     if(worldMap[int(posX + dirX * moveSpeed)][int(posY)] == false) posX += dirX * moveSpeed;
  460.     if(worldMap[int(posX)][int(posY + dirY * moveSpeed)] == false) posY += dirY * moveSpeed;
  461. }
  462.  
  463. void MoveBackward ()
  464. {
  465.       if(worldMap[int(posX - dirX * moveSpeed)][int(posY)] == false) posX -= dirX * moveSpeed;
  466.       if(worldMap[int(posX)][int(posY - dirY * moveSpeed)] == false) posY -= dirY * moveSpeed;
  467. }
  468.  
  469. void RotateLeft ()
  470. {
  471.       double oldDirX = dirX;
  472.       dirX = dirX * cos(rotSpeed) - dirY * sin(rotSpeed);
  473.       dirY = oldDirX * sin(rotSpeed) + dirY * cos(rotSpeed);
  474.       double oldPlaneX = planeX;
  475.       planeX = planeX * cos(rotSpeed) - planeY * sin(rotSpeed);
  476.       planeY = oldPlaneX * sin(rotSpeed) + planeY * cos(rotSpeed);
  477. }
  478.  
  479. void RotateRight ()
  480. {
  481.       double oldDirX = dirX;
  482.       dirX = dirX * cos(-rotSpeed) - dirY * sin(-rotSpeed);
  483.       dirY = oldDirX * sin(-rotSpeed) + dirY * cos(-rotSpeed);
  484.       double oldPlaneX = planeX;
  485.       planeX = planeX * cos(-rotSpeed) - planeY * sin(-rotSpeed);
  486.       planeY = oldPlaneX * sin(-rotSpeed) + planeY * cos(-rotSpeed);
  487. }
  488.  
  489. //sort algorithm
  490. void combSort(int* order, double* dist, int amount)
  491. {
  492.   int gap = amount;
  493.   bool swapped = false;
  494.   while(gap > 1 || swapped)
  495.   {
  496.     //shrink factor 1.3
  497.     gap = (gap * 10) / 13;
  498.     if(gap == 9 || gap == 10) gap = 11;
  499.     if (gap < 1) gap = 1;
  500.     swapped = false;
  501.     for (int i = 0; i < amount - gap; i++)
  502.     {
  503.       int j = i + gap;
  504.       if (dist[i] < dist[j])
  505.       {
  506.         std::swap(dist[i], dist[j]);
  507.         std::swap(order[i], order[j]);
  508.         swapped = true;
  509.       }
  510.     }
  511.   }
  512. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement