Jump to content
  • Advertisement
Sign in to follow this  

Raycasting sprites, need to position them correctly..

This topic is 4088 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

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.visible) { //only process sprites that should be visible
			sprites.visible = false; //turn this off for post game frames to turn back on
			sx = sprites.x; //to save some typing
			sy = sprites.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.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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!