Sign in to follow this  
progek

Raycasting sprites, need to position them correctly..

Recommended Posts

progek    122
Hi all, I wrote a decent raycaster and am trying to add static sprites. Here is a screenshot.. I need help finding the x position (on screen) to start drawing a sprite. I can figure out the y position and scaling. During my raycasting process my sprites (static for now) are marked 'visible' if a ray hits one. Now, after my walls are drawn, I want to process sprites.. Notes before code.. At the moment, I'm not sorting sprites and I'm not drawing them in slivers while checking distances between them and the zbuffer with wall slice distances.. Once I get one sprite to draw right, I will code that up and optimize it.
// _FOV = 64
// _screenWidth = 320, _screenHeight = 240
// _gridSize = 64
void drawSprites( ) {
	
	SDL_Rect spriteRect;
	
	SDL_Surface *scaledSprite = SDL_CreateRGBSurface(SDL_HWSURFACE, 1, int(_gridSize), 16, 0, 0, 0, 0);
	
	float px = playerX; 
	float py = playerY;
	float sx; //sprite x, y
	float sy;
	float xInc;
	float yInc;
	float yTmp;
	float theta = playerAngle; //current facing angle
	float thetaTemp;
	float playerToSpriteDistance;
		
	//loop for each sprite structure
	for(int i=0;i< _numSprites;i++) { 
		if(sprites[i].visible) { //only process sprites that should be visible
			
			sprites[i].visible = false; //turn this off for post game frames to turn back on
			
			sx = sprites[i].x; //to save some typing
			sy = sprites[i].y;
			
			xInc = sx - px;
			yInc = sy - py;
			thetaTemp = atan2(yInc, xInc); //find angle between player and sprite
			thetaTemp *= 180.0f / M_PI;  //convert to degrees 
			if(thetaTemp < 0) thetaTemp += 360;  //make sure its in proper range
			
			// Wrap things around if needed
			yTmp = theta + (_FOV/2) - thetaTemp;  // Theta + FOV/2 = angle of ray that generates leftmost column of the screen ?
			if (thetaTemp > 270 && theta < 90) yTmp = theta + (_FOV/2) - thetaTemp + 360;
			if (theta > 270 && thetaTemp < 90) yTmp = theta + (_FOV/2) - thetaTemp - 360;
			
			// Compute the screen x coordinate
			spriteRect.x = int((yTmp * _screenWidth / _FOV) - _gridSize/2);
			spriteRect.y = _screenHeight/2;
			
			playerToSpriteDistance = sqrt( (px-sx) * (px-sx) + (py-sy) * (py-sy) );
			
			scaledSprite = zoomSurface(spriteImages[sprites[i].id], 64/playerToSpriteDistance*4, 64/playerToSpriteDistance*4, 1);
				
			SDL_BlitSurface(scaledSprite, NULL, screen, &spriteRect);
		}
	}
	
	if(scaledSprite) SDL_FreeSurface(scaledSprite);
}




The problem is, when I turn the sprite moves left or right (until its logically out of the field of view). They move horizontally on the screen when my player facing angle increases/decreases, however walking forward and back seems ok. I would appreciate any help on this matter! Thanks for reading.

Share this post


Link to post
Share on other sites

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