Jump to content
  • Advertisement
Sign in to follow this  
homer_3

Camera following jumping player?

This topic is 2635 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 working on a 2D XNA game and I want my "camera" to follow my player up when he jumps, so the ground drops down a bit and more sky is visible. In my Draw method I'm doing


spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState, spriteBatchMatrix);


where spriteBatchMatrix is set to Matrix.Identity. It seems like one solution I can do is, in my Update method do something like


Update(GameTime gametime)
{
if(jumping)
{
spriteBatchMatrix = Matrix.CreateTranslation(new Vector3(0, height++, 0));
}else
{
spriteBatchMatrix = Matrix.Identity;
height = 0;
}
}


This kind of gives the effect I want, but when drawing from the top of the screen (0,0), it's not really the top of the screen anymore. Plus, creating a new matrix every update seems wasteful. Can anyone suggest another way to handle this?

Share this post


Link to post
Share on other sites
Advertisement
Oops. I replied for a 3D world.

For 2D you just want to move the camera up when the player jumps.
The target position of the camera should be relative to the position of the player, plus a little offset either up or down depending how fast the player is moving up or down.

Once you derive the target position where you want your camera to be, use that value in a weighted calculation that can move your camera towards that position from its current position smoothly over a few frames. If you are using a fixed time-step, this is trivial.


L. Spiro

Share this post


Link to post
Share on other sites
Plus, creating a new matrix every update seems wasteful. Can anyone suggest another way to handle this?

Every update means once per frame, which is negligible from a performance point of view. Your matrix will be used to draw hundreds of thousands of pixels.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!