Jump to content
  • Advertisement
Sign in to follow this  

Scrolling and Collisions

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

hey guys, I've been trying to think this through and can't seem to come up with a solution to my problem described down below. Problem: When the player's bounding box(the smallest box) hits one of the 4 sides of the camera box,(the next biggest box) the background will scroll in the corresponding direction and the player's x or y values won't change, depending on if the player is moving horizontally or vertically. Since I have to stop the character from moving off screen, Im using the camera box to stop his movement on screen and just keep playing the animation while scrolling the background. But the problem is when the screen is scrolling his x or y values dont change so the collisions with other objects dont happen where they are supposed to be, because of the limitation im putting on the players movement. If anyone can help me out on this dilemma, it would be greatly appreciated, or if anyone has done something like this in a different way and wouldnt mind explaining how to do it would be great as well. Thanks.

Share this post

Link to post
Share on other sites
Don't use the camera to 'stop' a player's movement, just use it to figure out where to draw the player.

Player is at x/y, player moves left.
Player moves left again, hits camera boundrary, camera moves left. (But it doesn't stop the player moving)
Camera cannot move left if it hits the sides of the 'world/level/map'.
Player cannot move left if it hits the sides of the 'world/level/map'.

The camera has no affect on the player's location, and so the player's collision detection isn't messed up. The camera only moves when the player touches it.

Draw the player as such:
ScreenDrawingX = PlayerX - CameraX
ScreenDrawingY = PlayerY - CameraY

Other than using the camera to draw the player(and all other objects) onscreen, the camera has no purpose. It shouldn't stop the player from moving, or alter the player's position at all.

Share this post

Link to post
Share on other sites
What I dont understand is how to keep the player from moving off screen if I do it like that. The thick black border in the image is the actual screen and I wanted to scroll the background before the player reaches the edge of the screen.

Share this post

Link to post
Share on other sites
I'm not entirely sure I understand what you mean, but something like below is what I think you are wanting.

//Pseudo code:

//variables: PlayerX/Y is the location in the map/world/background, not the location on the screen.

//variables: CameraX/Y is the location of the screen/camera/window in the map/world/background

//constants: The width and height of the player sprite or collision box

//constants: 'map' could also be called 'world' or 'background'
MAP_WIDTH = 1000

//constants: 'screen' could also be called 'camera' or 'window', whatever you prefer.

//psuedo function: Draws the player to the 'screen' (not the background) at locationX and locationY.
drawPlayer(locationX, locationY)

//Somewhere in the update loop.

//If the PlayerX is touching or past the far side of the camera/screen, shift it over the map/world/background more.
If (PlayerX + PLAYER_WIDTH) > (CameraX + SCREEN_WIDTH)
CameraX = ((PlayerX+SCREEN_WIDTH) + 2) - PLAYER_WIDTH

//If the PlayerY is touching or past the bottom of the camera/screen, shift it down the map/world/background more.

//If the PlayerX is touching or past the near(left) side of the camera/screen, shift it back over the map/world/background more.
If PlayerX < CameraX
CameraX = (PlayerX - 2)

//If the PlayerX is touching or past the top of the camera/screen, shift it up the map/world/background more.
If PlayerY < CameraY
CameraY = (PlayerY - 2)

//Check to make sure the player isn't going beyond a map boundary.
If PlayerX < 0
PlayerX = 0
If PlayerY < 0
PlayerY = 0

//Check to make sure the camera/screen/window isn't going past a map boundary.
If CameraX < 0
CameraX = 0
If CameraY < 0
CameraY = 0

//Somewhere in the draw loop.

drawPlayer(PlayerX - CameraX, PlayerY - CameraY)

All you need to do is move PlayerX and PlayerY, and the camera will move with it, but only when the player is touching the side. You can handle your collision detection of the player like you normally do, becuase the camera doesn't interfere with it. [smile]

Personally, I'd prefer the camera be centered on the player, except when near the edge of the map, but that's just me. It sounds like you are wanting it only to move when the player is near, though, so that's what I tried to do.

Share this post

Link to post
Share on other sites
Thanks Servant, I got it to work the way I wanted it to. I now understand why the player will not run off screen when subtracting the camera position from the players position. Thanks for putting my thinking in the right direction.

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!