Sign in to follow this  

Raycasting Floors!

This topic is 4197 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

HEllo everybody I spent the last hour scouring the web for information on floor casting but did not find anything constructive. right now i can render perspectivly correct only a single texture. my map is stored as a simple integer array map.data= {{1,1,1,1}, {{1,0,0,1}, {{1,0,2,1}, {{1,0,0,1}, {{1,1,1,1}} right now it only renders texture 0 as the ground texture. i would like to extrapolate the ground tile intersected by the ray and then choose the correct texture to be shown. my aim is to reproduce an engine like Might and Magic IV, ie. to have water, grass, desert tiles and the like, not just a simple stone floor. hope you can help me Nukleon below is the floorcasting code CODE:
///floor casting    
	            double floorXWall, floorYWall; //x, y position of the floor texel at the bottom of the wall
	
	            //4 different wall directions possible
	            if(side == 0 && rayDirX > 0)
	            {
	                floorXWall = mapX;
	                floorYWall = mapY + wallX;
	            }
	            else if(side == 0 && rayDirX < 0)
	            {
	                floorXWall = mapX + 1.0;
	                floorYWall = mapY + wallX;
	            }        
	            else if(side == 1 && rayDirY > 0)
	            {
	                floorXWall = mapX + wallX;
	                floorYWall = mapY;
	            }        
	            else
	            {               
	                floorXWall = mapX + wallX;
	                floorYWall = mapY + 1.0;       
	            } 
	            
	            double distWall, distPlayer, currentDist;  
	
	            distWall = perpWallDist;
	            distPlayer = 0.0;  
		    
	            if (drawEnd < 0) drawEnd = resolution_y; //becomes < 0 when the integer overflows
	            //draw the floor from drawEnd to the bottom of the screen
	            for(int y = drawEnd +1; y < resolution_y; y++)
	            {
	                currentDist = resolution_y / (2.0 * y - resolution_y); //you could make a small lookup table for this instead
	
	                double weight = (currentDist - distPlayer) / (distWall - distPlayer);
	               
	                double currentFloorX = weight * floorXWall + (1.0 - weight) * Party.posX;
	                double currentFloorY = weight * floorYWall + (1.0 - weight) * Party.posY;
	                
	                int floorTexX, floorTexY;
	                floorTexX = (int)((currentFloorX * texWidth) % texWidth);
	                floorTexY = (int)((currentFloorY * texHeight) % texHeight);
	                
	                //here i would like to change the 1. parameter to the texture interesected by the ray
	                buffer[y*resolution_x+x]= textures[1,floorTexX,floorTexY];
	               
	                
	                if(CurrentMap.outdoor==false)
	                buffer[(resolution_y-y)*resolution_x+x]= textures[2,floorTexX,floorTexY];
	            }

Share this post


Link to post
Share on other sites
I know this code from somewhere :D

I think it should be fairly easy to choose a texture depending on where you hit the floor. You can get the integer x and y coordinate of the floor tile. You must have a 2D array of the floor texture indexes (not the wall texture array) and use x/y of the floor tile as index of that to get the texture. Then just get the pixel from that texture in the same way as in the code you posted.

I don't think it's hard to extend it like that and that it's not necessary to post code for it. The checkerboard floor example already goes into that direction by the way, now just add a 2D array of floor texture indexes to it.

Share this post


Link to post
Share on other sites
thanx Lode!

I'm sorry that i did not directly credit you for the code from your tutorial. it is very good by the way.

i will look into your suggestions, and especially into the checkerboard code.

happy coding

Nukleon

EDIT: oh man it was sooo easy! right after you hinted me at the use of a second array, all i had to do was to cast "currentFloorX" to int and use it as a coordinate in the floorcasting algorithm.
it now works like a charm.
thank you very much!!!

Share this post


Link to post
Share on other sites

This topic is 4197 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this