This project has moved and is read-only. For the latest updates, please go here.

Move camera in demo, how?

Oct 14, 2008 at 12:35 PM
Hi all$0$0$0$0How can i pan or move the camera view on the world in the demoes, how do i implament that? i can't seem to get it to work at all. I just want to move up/down and sideways$0
Oct 14, 2008 at 1:06 PM
I'm not entirely sure what you're wanting to do, but a decent way to "cheat" into a moving camera is using Vector2 offsets. For example: create a cameraPosition Vector2, and set it to your main object at first. (So, cameraPosition = mainBody.Position in Initialize). Then, you can use it in your Draw() calls like so: spriteBatch.Draw(sprite, Vector2.Subtract(object.Position, cameraPosition), rest of your draw call); Then, you can use whatever method you want to move the camera (trailing along a body, moving with the mouse or thumbstick...), and it'll move stuff along the screen. Not perfect, but quite effective.
Oct 14, 2008 at 10:53 PM
Hi Arextion.

This is not really a Farseer directed question. You might want to take a look at the XNA forums here:

I'm going to answer anyway.

Creating a camera is really easy. All you have to do is keep a position where you camera is, and then translate (move) all other objects on the screen, relative to the camera position.

There is a good and simple article here:
It does also have a very simple implementation of a 2d camera.

You should make sure that your underground fields are drawn with the position of the physics body. One thing that you will have to remember is, that games are only a screen filled with drawn pixels. There is nothing else but that screen. When you see Mario move in the Mario 2D scroller game, the whole world is being moved, not the "camera".

I've written some code to make it clearer:

Vector2 _cameraPosition = new Vector2(100,100);

foreach (UndergroundField field in UndergroundFields)
 field.Position + _cameraPosition;

foreach (UndergroundField field in UndergroundFields)

All the fields would now have moved 100 X and 100 Y. This would make it seem like the "camera" moved.
Oct 20, 2008 at 10:10 AM
" When you see Mario move in the Mario 2D scroller game, the whole world is being moved, not the "camera"."$0$0well theres your problem (as Jamie in mythbusters would say it) i though is should move the view ON the world, not move THE world :D$0$0$0$0Thanks for answering anyway, thumbs up!$0
Oct 20, 2008 at 9:58 PM
There is an even easier way to move/rotate your scene. When you call spriteBatch.Begin() use all the parameters including the last which is a Matrix. Here is an example -

Matrix transform = Matrix.CreateTranslation(new Vector3(200, 200, 0)) * Matrix.CreateRotationZ(angle);

spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.None, transform);

This matrix gives you the ability to change the model-view matrix of the SpriteBatch shader. You do not have to have the rotation if you don't need it. Remember if you want your camera to move a certain direction, say down and to the left, you need to translate up and right. If anyone really needs an example I'd be gald to whip one up. Also remember that the angle is in radians so if you don't use radians use Math.Helper to change degrees to radians.