Advertisement
jtjj222

Ray caster with a bug... help!

Feb 3rd, 2013
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.58 KB | None | 0 0
  1. package me.jtjj222.raycasting.Rendering;
  2.  
  3. import java.awt.image.BufferedImage;
  4. import java.awt.image.DataBufferInt;
  5. import me.jtjj222.raycasting.World;
  6. import me.jtjj222.raycasting.entities.Entity;
  7.  
  8. public class Renderer {
  9.  
  10.     int[] pixelData;
  11.     private BufferedImage backBuffer;
  12.     int width,height;
  13.    
  14.     public static final int MAX_WALL_CHECKS = 10, DIST_TO_PROJECTION_PLANE = 3;
  15.    
  16.     int[] zBuffer = new int[width]; //store the length of walls
  17.    
  18.     public Renderer(int width, int height) {
  19.         this.width = width;
  20.         this.height = height;
  21.         backBuffer = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
  22.         pixelData = ((DataBufferInt) backBuffer.getRaster().getDataBuffer()).getData();
  23.     }
  24.    
  25.     public final BufferedImage render(World world) {
  26.        
  27.         drawFloor(world);
  28.         drawWalls(world);
  29.                
  30.         return backBuffer;
  31.     }
  32.    
  33.     private void drawWalls(World world) {
  34.        
  35.         double angleBetweenCollumns = (double)world.player.FOV/(double)width;
  36.         double playerX = world.player.posX, playerZ = world.player.posZ;
  37.         int tileX = (int) playerX, tileZ = (int) playerZ;
  38.        
  39.         for (int collumn = -width/2; collumn < width/2; collumn++) { //0 is at the center of the screen
  40.            
  41.             double worldCollumnAngle = Entity.getAngleInRange(collumn * angleBetweenCollumns + world.player.direction);
  42.            
  43.             double hRayPosX=0, hRayPosZ=0, nextHorizontalIntersectionXDist=0, nextHorizontalIntersectionZDist=0;
  44.             double vRayPosX=0, vRayPosZ=0, nextVerticalIntersectionXDist=0, nextVerticalIntersectionZDist=0;
  45.             double firstHorizontalIntersectionZdist = 0, firsthorizontalIntersectionXdist = 0, firstVerticalIntersectionXdist = 0, firstVerticalIntersectionZdist = 0; //changes if the ray is up or down
  46.             double triangleAngle = 0;
  47.            
  48.             if (worldCollumnAngle < 360 && worldCollumnAngle >= 0) {               
  49.  
  50.                 if (worldCollumnAngle < 90)
  51.                     triangleAngle = worldCollumnAngle;
  52.                 else if (worldCollumnAngle < 180)
  53.                     triangleAngle = 180 - worldCollumnAngle;
  54.                 else if (worldCollumnAngle < 270)
  55.                     triangleAngle = 270 - worldCollumnAngle;
  56.                 else if (worldCollumnAngle < 360)
  57.                     triangleAngle = 360 - worldCollumnAngle;
  58.                 else System.out.println("render error");
  59.                
  60.                 //facing left
  61.                 if ((worldCollumnAngle < 90 && worldCollumnAngle > 0) || (worldCollumnAngle < 360 && worldCollumnAngle > 270)) {
  62.                     vRayPosX = tileX + 1;
  63.                     nextVerticalIntersectionXDist = 1;
  64.                     firstVerticalIntersectionXdist = vRayPosX - playerX;
  65.                 }
  66.                 //facing right
  67.                 else {
  68.                     vRayPosX = tileX;
  69.                     nextVerticalIntersectionXDist = -1;
  70.                     firstVerticalIntersectionXdist = playerX - vRayPosX;
  71.                 }
  72.                
  73.                 //facing up
  74.                 if ((worldCollumnAngle < 180 && worldCollumnAngle > 0)) {
  75.                     hRayPosZ = tileZ + 1;
  76.                     nextHorizontalIntersectionZDist = 1;
  77.                     firstHorizontalIntersectionZdist = hRayPosZ - playerZ;
  78.                 }
  79.                 //facing down
  80.                 else {
  81.                     hRayPosZ = tileZ;
  82.                     nextHorizontalIntersectionZDist = -1;
  83.                     firstHorizontalIntersectionZdist = playerZ - hRayPosZ;
  84.                 }
  85.                                
  86.                 firsthorizontalIntersectionXdist = firstHorizontalIntersectionZdist /(Math.tan(Math.toDegrees(triangleAngle)));            
  87.                 nextHorizontalIntersectionXDist = nextHorizontalIntersectionZDist /(Math.tan(Math.toDegrees(triangleAngle)));              
  88.  
  89.                 firstVerticalIntersectionZdist = firstVerticalIntersectionXdist * Math.tan(Math.toDegrees(triangleAngle));             
  90.                 nextVerticalIntersectionZDist = nextVerticalIntersectionXDist * Math.tan(Math.toDegrees(triangleAngle));
  91.                
  92.                 //facing left
  93.                 if ((worldCollumnAngle < 90 && worldCollumnAngle > 0) || (worldCollumnAngle < 360 && worldCollumnAngle > 270)) {
  94.                     vRayPosZ = firstVerticalIntersectionZdist + playerZ;
  95.                 }
  96.                 else {
  97.                     vRayPosZ = playerZ - firstVerticalIntersectionZdist;
  98.                 }
  99.                
  100.                 //facing up
  101.                 if ((worldCollumnAngle < 180 && worldCollumnAngle > 0)) {
  102.                     hRayPosX = firsthorizontalIntersectionXdist + playerX;
  103.                 }
  104.                 else {
  105.                     hRayPosX = playerX - firsthorizontalIntersectionXdist;
  106.                 }
  107.                
  108.                 //now we know the initial intersection that the ray makes, and the distance to the next intersection
  109.             }
  110.             else System.out.println("Render error");
  111.            
  112.             double hIntDistance = 999999, vIntDistance = 999999; //make sure if one of these isn't defined, it will not be used
  113.            
  114.             //go along the ray to find horizontal intersections
  115.             if (worldCollumnAngle != 0 && worldCollumnAngle != 180) { //those cases have no horizontal intersections
  116.                 int i = 0;
  117.                 while (i < MAX_WALL_CHECKS) {
  118.                     if (world.isWall(hRayPosX, hRayPosZ)) {
  119.                         //found horizontal intersection
  120.                         hIntDistance = distance(hRayPosX,hRayPosZ,playerX,playerZ);
  121.                         break;
  122.                     }
  123.                     hRayPosX += nextHorizontalIntersectionXDist;
  124.                     hRayPosZ += nextHorizontalIntersectionZDist;
  125.                     if (hRayPosX < 0 || hRayPosZ > world.width) { //out of the world (should never happen)
  126.                         vIntDistance = 999999999; //make sure that this distance is never used (always larger)
  127.                         break;
  128.                     }
  129.                     i++;
  130.                 }
  131.             }
  132.            
  133.             //find vertical intersections
  134.             if (worldCollumnAngle != 90 && worldCollumnAngle != 270) {
  135.                 int i = 0;
  136.                 while (i < MAX_WALL_CHECKS) {
  137.                     if (world.isWall(vRayPosX, vRayPosZ)) {
  138.                         vIntDistance = distance(vRayPosX,vRayPosZ,playerX,playerZ);
  139.                         break;
  140.                     }
  141.                     vRayPosX += nextVerticalIntersectionXDist;
  142.                     vRayPosZ += nextVerticalIntersectionZDist;
  143.                     if (vRayPosX < 0 || vRayPosZ > world.height) {
  144.                         vIntDistance = 999999999; //make sure that this distance is never used (allways larger)
  145.                         break;
  146.                     }
  147.                     i++;
  148.                 }
  149.             }
  150.            
  151.             double wallDist = 0;
  152.            
  153.             if (hIntDistance > vIntDistance) wallDist = vIntDistance;
  154.             else wallDist = hIntDistance;
  155.            
  156.             double trueWallDist = wallDist ;//* Math.cos(triangleAngle);
  157.  
  158.             int wallheight = 200;
  159.             int projectedWallHeight = (int) (wallheight*DIST_TO_PROJECTION_PLANE/trueWallDist);
  160.            
  161.             int startX = collumn+width/2;
  162.             int startZ = height/2 - projectedWallHeight/2;
  163.             int endX = startX + 1;
  164.             int endZ = height/2 + projectedWallHeight/2;
  165.            
  166.             drawRect(startX,startZ,endX,endZ,0xFFFFFF);
  167.         }
  168.        
  169.     }
  170.    
  171.     private void drawRect(int x0, int y0, int x1, int y1, int col) {       
  172.         for (int y = y0; y < y1; y++) {
  173.             for (int x = x0; x < x1; x++) {
  174.                 int offset = x + y * width;
  175.                 if (offset < pixelData.length && offset > 0) pixelData[offset] = col;
  176.             }
  177.         }
  178.     }
  179.  
  180.     private double distance (double ax, double az, double bx, double bz) {
  181.         return Math.sqrt((ax-bx)*(ax-bx) + (az-bz)*(az-bz) );
  182.     }
  183.    
  184.     private void drawFloor(World world) {
  185.         drawCeiling(world);
  186.     }
  187.    
  188.     private void drawCeiling(World world) {
  189.         drawRect(0,0,width,height,0x555555);
  190.     }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement