Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: [SOLVED] Raycasting troubles

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [SOLVED] Raycasting troubles

    Greetings.

    I am writing a raycasting engine right now, based off the Permadi tutorial, and I seem to have encountered two problems concerning wall drawing.

    The first is that when I turn 180°, I get the exact same thing, regardless of how far away the wall behind me might be (essentially, if you look at the minimap top right (which does display the level correctly), you'd get the same images if I were to look into the top-left corner).

    The second is that the walls don't render properly, as can be seen on the images below. Only difference between these two images is that I move forward a little (it gets a lot worse if I move more).

    Would anybody know offhand what is happening ? Being new to this forum, I can't post enough code to provide an understanding of the way I'm trying to do this.

    raycasting.jpg


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Raycasting troubles

    Can you explain how your problem is related to java programming?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Raycasting troubles

    I'm writing all this in Java, and I'm not sure all my issues are purely of an algorithmic nature.

  4. #4
    Junior Member
    Join Date
    Aug 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Raycasting troubles

    Here's a .jar so that people can better visualise my issue (I've cleared up the 180° problem, but I still have trouble with the wal drawing).

    TestExport.rar

    One can move with the arrow keys. The level is 10*10, with blocks at all edges, and one block near the middle.

  5. #5
    Junior Member
    Join Date
    Aug 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Raycasting troubles

    All right, I've cleared up some more of the issues, but there still seems to be some problems with corners and such. a couple of walls come out of nowhere, and when one gets closer to the far walls, it gets a little strange. All these issues seem to be happening when the player angle is somewhere between 330 and 30 degrees (when the player looks straight right).

    TestExport.zip

  6. #6
    Junior Member
    Join Date
    Aug 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Raycasting troubles

    Ah, I can post some code. Essentially, the issues are with the raycast method.
    (source code is unoptimised, and hopelly not too illegible).

    This method is supposed to return the distance of a wall from player position at a certain angle.

    public static double raycast(double x, double y, double angle, Level level){
     
    // These will be intersection coordinates. Initialised at player coordinates. 
    		int xtemp = (int)Math.floor(x);
    		int ytemp = (int)Math.floor(y);
     
    // These will be the distance one gets
    		double distancefoundhor = 0;
    		double distancefoundver = 0;
    		double angle2 = Math.PI * angle /180;
    		boolean iswall = false;
     
    		//avoid division by zero
    		if(angle2 %(Math.PI/2) == 0) return 0;
     
    // Horizontal intersections
    // Get first intersection coordinates, depends if ray is facing up or down
     
    		if(angle2>Math.PI)
    {ytemp = ((ytemp/128)*128 -1);}else{ytemp = ((ytemp/128)*128 +128);}
    		xtemp -= ((y - ytemp)/(Math.tan(angle2)));
     
    // if it's out of boundaries, return huge distance
    		if(xtemp < 0 || ytemp < 0 || xtemp >= level.WIDTH*128  || ytemp >= level.HEIGHT*128){ iswall = true; distancefoundhor = 4000;}
     
    // if the block corresponding to the intersection is a wall, return distance
     
    		if(!iswall&&level.getBlockType(xtemp, ytemp)==-1){iswall = true;
    			distancefoundhor = ((y-ytemp)/Math.sin(angle2));
    		}
     
    // otherwise get the next intersection	
    		while(!iswall){
    //not the same formula
    			if(angle2 >Math.PI){ytemp-=128; xtemp -= Math.floor(128/Math.tan(angle2));}else{ytemp += 128; xtemp += Math.floor(128/(Math.tan(angle2)));}
     
    //check boundaries
    			if(xtemp < 0 || ytemp < 0 || xtemp >= level.WIDTH*128 || ytemp >= level.HEIGHT*128){ iswall = true; distancefoundhor = 4000;}
     
    // Check wall block again
    			if(!iswall && level.getBlockType(xtemp, ytemp)==-1){iswall = true;
    				distancefoundhor = ((y-ytemp)/Math.sin(angle2));
    			}
    		}			
     
    // Vertical intersections
    // Reinitialise
    		xtemp = (int)x; ytemp = (int)y;iswall = false;
     
    // Get first intersection, depends if ray is facing left or right
     
    		if(angle2>Math.PI/2 && angle2<3*Math.PI/2){	xtemp = ((xtemp/128) *128 -1);}else{xtemp = ((xtemp/128)*128+128);}
    		ytemp -= ((x - xtemp)*(Math.tan(angle2)));
     
    // Check boundaries		
    		if(xtemp < 0 || ytemp < 0 || xtemp >= level.WIDTH*128 || ytemp >= level.HEIGHT*128){ iswall = true; distancefoundver = 4000;}
     
    // Check if is wall block. Distance is calculated using cos as it avoids division by near-zero values
    		if(!iswall&&level.getBlockType(xtemp, ytemp)== -1){iswall = true;
    			distancefoundver =  ((x-xtemp)/Math.cos(angle2));
    		}	
     
    // Get next intersection(s)
    		while(!iswall){
    			if(angle2 >Math.PI/2&&angle2<3*Math.PI/2){xtemp-=128;ytemp -= Math.floor(128*(Math.tan(angle2)));}else{xtemp += 128;	ytemp += Math.floor(128*(Math.tan(angle2)));}
     
    // Same as before
    			if(xtemp < 0 || ytemp < 0 || xtemp >= level.WIDTH*128 || ytemp >= level.HEIGHT*128){ iswall = true; distancefoundver = 4000;}
    			if(!iswall&&level.getBlockType(xtemp, ytemp)==-1){iswall = true;
    				distancefoundver =  ((x-xtemp)/Math.cos(angle2));
    			}
    		}	
     
    // Return lower value of distance
    		if(Math.abs(distancefoundhor) < Math.abs(distancefoundver)){
     
    			return Math.abs(distancefoundhor);
    		}else{
    			return Math.abs(distancefoundver);}
     
    	}
    }


    This is in my render method (I know it's badly written) :

    for(double i = 0; i <640; i++){  
     
    //spans from player angle -30 to player angle + 30 with pixel increment  
                    double j = game.player.ROTATION + i*3/32 -30;            
                    this.drawWall((int)i,Raycasting.raycast(game.player.X_COORD,game.player.Y_COORD,j , game.level), j, 0x10cc);  
            }

    This draws the walls :

        public void drawWall(int x,double distance, double angle, int colour ){  
                //draws the corresponding wall section using distance       
                int wallheight = 0;  
                if(distance!= 0){  
                 wallheight = (int) ((128*554/(distance*Math.cos((angle- Screen.display.game.player.ROTATION)*Math.PI/180))));      
         // cos is to avoid fisheye effect.  
                }  
     
        // Avoid drawing out of screen  
                if(wallheight > Display.HEIGHT){  
                    wallheight = Display.HEIGHT;  
                }              
                drawVerticalLine(x, Raycasting.CENTER_Y - wallheight/2, wallheight, colour);  
        }

    Using :

     public void drawVerticalLine(int x, int y, int length, int colour){  
                //This method draws a vertical line from (x, y-length/2) to (x,y+length/2) 
        		for(int i = 0; i < length;i++){
                    pixels[x+Display.WIDTH*(y+i)] = colour;}  
            }

    Levels are represented as an array, with width and depth specified. -1 for walls, 0 for ground.

    The raycast method uses this method :

    public int getBlockType(int x, int y){
     
    	return floordata[(x/128 ) + WIDTH*(y/128)];
     
    	}

    Here's my test level in the .jars (width = depth = 10), I think (changed it since, the -1 isn't there anymore) :

    {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
    -1,0,0,0,0,0,0,0,0,-1,
    -1,0,0,0,0,0,0,0,0,-1,
    -1,0,0,0,0,0,0,0,0,-1,
    -1,0,0,0,0,-1,0,0,0,-1,
    -1,0,0,0,0,0,0,0,0,-1,
    -1,0,0,0,0,0,0,0,0,-1,
    -1,0,0,0,0,0,0,0,0,-1,
    -1,0,0,0,0,0,0,0,0,-1,
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1}

    and here's the Permadi tutorial :
    Ray Casting Programming Tutorial For Game Development

    I don't use the same conventions concerning angles (mine are clockwise), and wall size (128, with a display screen of 640*400).

    I hope that's enough information. Essentially, there's only the issues mentioned above left : weird behaviour when angle is between 330 and 30 degrees, and it seems wall corners aren't quite right.

    I believe some of these could be attributed to rounding errors, although I'm not sure where. I've been debugging this for quite a while, I would really like to *finally* get this properly working.

    Anyways, if you've actually read from start to end, thanks a lot !

  7. #7
    Junior Member
    Join Date
    Aug 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Raycasting troubles

    Solved those weird wall drawing issues. Only a few rounding issues with corners left, but that's a minor niggle for now. Sorry for the sort of monologue.

Similar Threads

  1. Raycasting Wall Texturing
    By SkyAphid in forum Java Theory & Questions
    Replies: 6
    Last Post: November 30th, 2011, 06:55 AM
  2. binarySearch troubles
    By pottsiex5 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 4th, 2011, 11:09 AM
  3. [SOLVED] ArrayList Troubles.
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 2nd, 2010, 10:19 AM
  4. JSP Troubles
    By sdkeslar in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 12th, 2010, 02:26 PM
  5. Array Troubles
    By Leeds_Champion in forum Collections and Generics
    Replies: 6
    Last Post: October 22nd, 2009, 11:05 AM