So this is going to be linked to and for future reference.
You will need to replace the texture2d i used with you’re own.
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace HowToCameraScrollOnaGameObject
{
/// <summary>
/// This is the main type for your game.
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D texture;
public static Vector3 camera2DScrollPosition = new Vector3(0, 0, 0);
public static float camera2dRotationZ = 0f;
public static float cameraScale = 1f;
Matrix cameraTransform = Matrix.Identity;
Rectangle objectPositionBlue = new Rectangle(0, 0, 300, 300);
Rectangle objectPositionGreen = new Rectangle(100, 100, 300, 300);
Rectangle objectPositionRed = new Rectangle(200, 200, 300, 300);
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.GraphicsProfile = GraphicsProfile.HiDef;
Window.AllowUserResizing = true;
this.IsMouseVisible = true;
}
protected override void Initialize()
{
graphics.PreferredBackBufferWidth = 800;
graphics.PreferredBackBufferHeight = 800;
graphics.ApplyChanges();
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
texture = Content.Load<Texture2D>("blue-square-button-editted-md");
}
protected override void UnloadContent()
{
}
public void SetCameraToCenterOnGameObjectsCenter(Rectangle gameObjectRectangle)
{
var c = gameObjectRectangle.Center;
camera2DScrollPosition.X = -c.X + GraphicsDevice.Viewport.Width / 2;
camera2DScrollPosition.Y = -c.Y + GraphicsDevice.Viewport.Height / 2;
CreateCameraTransform();
}
public void CreateCameraTransform()
{
// There is two pretty easy ways to do it.
// Depending on how you want your camera to behave when you are moving under world rotation.
// 1)
//cameraTransform =
// Matrix.CreateTranslation(camera2DScrollPosition) *
// Matrix.CreateScale(cameraScale) *
// Matrix.CreateRotationZ(camera2dRotationZ)
// ;
// Or using the 3d create world function.
// 2)
cameraTransform = Matrix.CreateWorld(camera2DScrollPosition, Vector3.Forward, Matrix.CreateRotationZ(camera2dRotationZ).Up) * Matrix.CreateScale(cameraScale);
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// Some code to move a sprite around with the arrow keys.
if (Keyboard.GetState().IsKeyDown(Keys.Left))
objectPositionRed.X = objectPositionRed.X - 1;
if (Keyboard.GetState().IsKeyDown(Keys.Right))
objectPositionRed.X = objectPositionRed.X + 1;
if (Keyboard.GetState().IsKeyDown(Keys.Up))
objectPositionRed.Y = objectPositionRed.Y - 1;
if (Keyboard.GetState().IsKeyDown(Keys.Down))
objectPositionRed.Y = objectPositionRed.Y + 1;
// Set the sprite to be what the camera follows.
SetCameraToCenterOnGameObjectsCenter(objectPositionRed);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteSortMode.Immediate,null, null, null, null, null, cameraTransform);
Rectangle source = new Rectangle(0, 0, texture.Width, texture.Height);
spriteBatch.Draw(texture, objectPositionBlue, source, Color.Blue);
spriteBatch.Draw(texture, objectPositionGreen, source, Color.ForestGreen);
spriteBatch.Draw(texture, objectPositionRed, source, Color.Red);
spriteBatch.End();
base.Draw(gameTime);
}
}
}