2
\$\begingroup\$

I'm working with a game in C# XNA and I want to draw a texture and a position in the middle of the screen. The thing is that I want the viewport to move around and not the player. So for example: If I push right I want the viewport to move right and keep the player in the middle of the screen.

Since GraphicsDevice.Viewport.X and Y are not variables, I have no idea how to do this.

\$\endgroup\$
1
  • \$\begingroup\$ You want the view port to move but not the avatar. If you pushed right and the view port moved right, the avatar would have to move if you wanted them to stay in the middle of the screen. \$\endgroup\$
    – House
    Commented Apr 17, 2012 at 21:36

4 Answers 4

3
\$\begingroup\$

the best way would be to integrate a camera class there are many tutorials for that

i assume you will be using it for a 2d game...so :

http://adambruenderman.wordpress.com/2011/04/05/create-a-2d-camera-in-xna-gs-4-0/

http://www.david-amador.com/2009/10/xna-camera-2d-with-zoom-and-rotation

\$\endgroup\$
1
  • \$\begingroup\$ I think i have heard that you could use something called matrix in Draw(), does someone know ho to use it? \$\endgroup\$ Commented Apr 18, 2012 at 4:07
2
\$\begingroup\$

Now I know how to do it :)

In SpriteBatch.Begin() you type:

spriteBatch.Begin(SpriteSortMode.Texture,
    null,null,null,null,null,
    Matrix.CreateTranslation(X,Y,0));

Since it is a 2D game I'm working on the Z axis is 0.

X and Y are where you want your camera to be, so for my example I can type:

Player_position.X - drawOffSet
Player_position.Y - drawOffSet

But thanks for the help anyway

\$\endgroup\$
3
  • 3
    \$\begingroup\$ So moving the viewport wasn't your problem to begin with, you just wanted to move the view within the screen? If you'd just asked that, you'd have gotten an answer the same day since that's basic stuff! Instead you were asking to move the Viewport object, which isn't nearly as basic. The lesson here is: when you have no idea how to do something, don't assume it must be done in this particular way and then ask how to do that particular thing - just ask about the original fundamental thing you want to do. \$\endgroup\$ Commented Apr 20, 2012 at 23:57
  • \$\begingroup\$ ok sorry, i just assumed that the viewport was the camera. I have just worked with Xna for like a half year. But i will keep in mind what you said in the future :) \$\endgroup\$ Commented Apr 24, 2012 at 16:53
  • \$\begingroup\$ For monogame users: you can use spriteBatch.Begin(transformMatrix: Matrix.CreateTranslation(x, y, 0)); \$\endgroup\$ Commented May 6, 2018 at 12:49
1
\$\begingroup\$

if you want to move your viewport you only have to do:

GraphicsDevice.Viewport = new Viewport(X,Y,WIDTH, HEIGHT);

but I'm not sure that it is what you need... usually is used to work with a camera, so the viewport is fixed and you change the camera... like if you were watching tv...

the tv, (your viewport), is fixed..., but the camera is what you moves to show other parts of the film's world.

\$\endgroup\$
-2
\$\begingroup\$

I haven't worked with xna in a while now, but aren't you supposed to update the camera coordinates and update the viewport with the camera?

\$\endgroup\$
1
  • 1
    \$\begingroup\$ -1, this is not a useful answer. \$\endgroup\$
    – House
    Commented Apr 17, 2012 at 21:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .