Jump to content
  • Advertisement
Sign in to follow this  

viewscreen not chasing player in 2d

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

I'm sorry for the poorly worded subject line, I was at a loss as to how to accurately describe the problems I'm having. I am trying to create a 2d engine in which the screen will follow the player around the map. Unfortunately, I am having some difficulties with this. In certain conditions, the screen will zoom off in one direction. And I haven't been able to determine exactly when this happens. My thinking is that it might happen when something goes from positive to negative, but I'm not positive on that. I've been working on figuring out this little bug for over a week now and am tearing my hair out. Can one of you gurus look at my code and see if you can easily see what it is I'm screwing up? Hopefully it's just something simple that will be easily rectified. This is the problem code (I finally figured out the tags for sourcecode):
[source lang = "cpp"]
void updatebackground()
	double distance = 0;
	int	pX, pY, vX, vY;

	//first we get the player's location on the playing field independent of the screen's location
	//then we get the screen's location on the playing field

	//get center of player x and y relative to playing field
	pX = (int)(background->w - (player->x + (player_images[0]->w /2)));
	pY = (int)(background->h - (player->y + (player_images[0]->h /2)));

	//get center of screen x and y relative to playing field
	vX = (int)(background->w - (view->x + (SCREEN_W / 2)));
	vY = (int)(background->h - (view->y + (SCREEN_H / 2))); 

	//determine what angle the center of the player is relative to the center of the screen
	//and how far away from each other they are
	view->moveAngle = itofix((int)((atan2(player->y, player->x)* 57.29578) - 90));
	distance = calcDistance(vX, vY, pX, pY);

	if ( distance > 50) 
		//here we move the screen to try and catch up to the player
		if (pX < vX)view->velx -= calcAngleMoveX(view->moveAngle) * calcVelocity(view->velx,distance ) ;
		if (pX > vX)view->velx += calcAngleMoveX(view->moveAngle) * calcVelocity(view->velx,distance ) ;
		if (pY < vY)view->vely -= calcAngleMoveY(view->moveAngle) * calcVelocity(view->vely,distance ) ;
		if (pY > vY)view->vely += calcAngleMoveY(view->moveAngle) * calcVelocity(view->vely,distance ) ;


	xoffset = view->x;
	yoffset = view->y;

	//make sure it doesn't scroll beyond map edge
	if (yoffset > background->h - SCREEN_H) yoffset = background->h - SCREEN_H;
	if (yoffset < 0) yoffset = 0;
	if (xoffset > background->w - SCREEN_W) xoffset = background->w - SCREEN_W;
	if (xoffset < 0) xoffset = 0;

	//draw it on the screen
	blit(background, buffer, xoffset, yoffset, 0, 0, SCREEN_W-1, SCREEN_H -1);
	//add a few lines of text for troubleshooting purposes
	textprintf_ex(buffer, font, 0, 380, WHITE, -1, "distance %d", (int)distance);
	textprintf_ex(buffer, font, 0, 370, WHITE, -1, "velx %d", (int)view->velx);
	textprintf_ex(buffer, font, 0, 360, WHITE, -1, "vely %d", (int)view->vely);

[Edited by - Goishin on May 21, 2007 1:46:49 PM]

Share this post

Link to post
Share on other sites
Your code is a little difficult to follow. You'll encourage more positive responses if you put in minimal annotation. In particular, what are pX, pY, vX, vY, background, view, distance, xoffset, yoffset? They all sound like likely Cartesian variables, but without knowing what entity each set of coordinates refers to, I get a headache.

Also, you may want to paste-copy the code into a plain-text editor before the 'Reply' form, to get rid of that nasty XML formatting.


Share this post

Link to post
Share on other sites
Oops, Admiral, you're right. I've spent so long staring at the same lines that it makes perfect sense to me, but you're right. I can't expect someone else to just magically be able to tell what I'm trying to do.

Ok, I edited the code above. Have a look now, and see if it's more readable.


- Goishin

Share this post

Link to post
Share on other sites
It looks too complicated [smile]
I do it like this.
1. Find x,y coordinates of player on map(in pixels)
2. subtract from it half screen width for x
and half screen height for y
3. now you know offset of map you need to draw.
4. check map not to go in minus or not to go beyond map width, height
5. Draw map on offset x,y coordinates
6. (eventually) you scroll map to reach needed coordinates

int playerx, playery;
playerx = m_Player.GetMapPositionX();
playery = m_Player.GetMapPositionY();

static int mapx,mapy;
mapx = playerx - (map_width/2);
mapy = playery - (map_height/2);

if(mapx < 0) mapx = 0;
if(mapy < 0) mapy = 0;
if(mapx > map_size.x) mapx = map_size.x;
if(mapy > map_size.y) mapy = map_size.y;

// 6 step
int current_map_x, current_map_y;

current_map_x = m_Map.GetX();
current_map_y = m_Map.GetY();
if(current_map_x > mapx) current_map_x--;
if(current_map_x < mapx) current_map_x++;

if(current_map_y > mapy) current_map_y--;
if(current_map_y < mapy) current_map_y++;


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!