Raycasting sprites

Started by
-1 comments, last by progek 16 years, 11 months ago
Hello all, I wrote a pretty decent Wolfenstein 3D style raycaster. Here is a screenshot-> screenshot I am getting to a point now where I would like to add some simple static sprites. (Later move them with maybe A* pathfinding). I cannot figure out how to correctly display them and the few raycasting tutorials around mostly discuss rendering walls, floors, and ceilings, which I am pretty much past now. I did find one tutorial with sprite information (Lode's), but the way it was programmed was rather specific on how the engine was coded, which was a little different than mine and I didn't understand most of what he/she was doing (with sprites). At the moment, I created a sprite map which rays check during the ray casting process. If a sprite is encountered, it is marked as (visible) so I can later draw them. After drawing the walls, my code will then process the sprites. Now, I am keeping track of the wall distance for every vertical wall slice during the raycasting process so I can check this to make sure a sprite is not blitted over a wall. I understand I need to sort the sprites to draw back to front, that is also not a problem. I am stuck on how to translate the sprite onto the screen, and scale it. I can figure out whether or not the player should be seeing a sprite, but how do I use the sprites map position (say float x = 128.0f, float y = 128.0f for example) and translate that to the screen/scaled? I'm sure I need to use the players current viewing angle and position, but don't have an idea on how to go about it other than maybe getting the distance from player to current sprite by taking the sqrt on the deltas and doing something else? Any help would be greatly appreciated! In case this helps, I'm using a field of view of 64* degrees, resolution of 320x240, grid size/sprite image size of 64x64 units (all grids of equal length), and a distance from player to projection plane of (64 * 255). I'm casting rays using Permadi's guide, and everything works great! I just need those simple sprites. EDIT:: Right now, I am just drawing a sprite in the corner of the screen just to let me know that a ray is turning on its flag and it should be visible. It works, so the rays do find the sprites, now if I can just translate its position to the screen..

void drawSprites( ) {
	const float _px = playerX; 
	const float _py = playerY;
	float playerToSpriteDistance;
	float sx; //sprite x, y
	float sy;
	
	//loop for each sprite structure
	for(int i=0;i< _numSprites;i++) {
		
		if(sprites.visible) { //process sprites which can only be seen, turned on during raycasting
			sprites.visible = false; //reset flag for next frame
			sx = sprites.x; //to save some typing and make code cleaner
			sy = sprites.y;
			
			//get the distance from player to sprite
                        //im sure I need this but not sure how to apply it
			playerToSpriteDistance = sqrt( ( _px-sx) * ( _px-sx) + ( _py-sy) * ( _py-sy) );
			
			//to test for now, later I will scale with zoomSurface() and draw the sprite where it should be
			SDL_BlitSurface(spriteImages[sprites.id], NULL, screen, NULL);
		}
		
	}
}







EDIT:: I moved this over to the game programming area [Edited by - progek on May 9, 2007 4:21:32 PM]

This topic is closed to new replies.

Advertisement