# Set a 2d camera's position so that a building is under the players feet

My issue is this: i am making a scrolling game in XNA, and the camera updates based on the players velocity, but the player never actually moves, he is always in the center of the screen. When he hits the top of the building though i want him to always be on top and sink through the texture in a way like this:

what i am doing to make this happen is i am just setting his velocity to 0, so its not moving, but the more velocity he hits a building with the more he sinks through it.

I also tried setting the buildings position to the plays Bounding Box's bottom, and this achieved the look i wanted but this also resulted in the other buildings rising in the air, because the velocity was still moving (even if i set it to 0).

if it was not a scrolling game, this would be not a problem, because you just set the players position to the top of the building, but because the player never actually moves, i actually need to move the camera to the point where the building is under the players feet without the other buildings rising. (Take note this is note a real camera, it is just a class that moves the objects in the world based on the players velocity). All questions are welcome.

• Oh so you are making a flappy bird game also? :) In mine what I'm doing is I DO move the character in X space. Then I also move the camera to match the X of the player. But set the camera's look at to be the same x and y as the camera with a the same as the player. I can post an answer if you want me to. Commented Feb 22, 2014 at 6:00
• I don't get it. Why not just set the buildings' bounding boxes to something other than their sprite height? Is this a collision detection problem or a graphical effect problem?
– Anko
Commented Feb 22, 2014 at 11:10

The way I see it, you are just stopping the movement the instant a collision is detected, which apparently causes the problem you just mentioned.

Why don't you try also relocating the player's Y coordinate when a downwards collision is detected, so that it is just a few pixels in the ground? This way you still detect collision, but keep the player at a fixed height in relation to the ground he's colliding with.

Now, regarding the camera, just a suggestion. Since you're using XNA, there is a nifty way of making a camera without even having to create a Camera class, but without having to reposition every single object in the screen either. Just store a Vector2 with the Camera's position, and when you start your spritebatch, pass a Translation Matrix as parameter, containing the X and Y info from your cameraPosition Vector2.

    Vector2 cameraPosition = new Vector2(500, 1000);
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, Matrix.CreateTranslation(new Vector3(cameraPosition.X, cameraPosition.Y, 0)));


Using a Translation Matrix to work the camera helps in a lot of ways, as you can just keep your objects in their real positions, which should make your life easier overall. You should be careful not to keep drawing what's outside the camera for perfomance issues, though.

One way might be is to add a set of "offset" properties to your player class or allow the ability to offset what the "center position" is.

Move the camera until he is about to sink in, and then change his center position instead of the camera's position. You can then draw the character sunken in without moving the camera around him. Then we he needs to get out of the hole, you can adjust the offset until it is back to the "real center" and then start to move the camera again.

You can see an example here at the one minute mark or anytime a character gets near the edge of the map. http://www.youtube.com/watch?v=7mMJio2MO6w&feature=player_embedded