Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>So I figured it out using a combination of the tutorials above and have created the class below. It tweens towards your target and follows it around. Try it out.</p> <pre><code>public interface IFocusable { Vector2 Position { get; } } public interface ICamera2D { /// &lt;summary&gt; /// Gets or sets the position of the camera /// &lt;/summary&gt; /// &lt;value&gt;The position.&lt;/value&gt; Vector2 Position { get; set; } /// &lt;summary&gt; /// Gets or sets the move speed of the camera. /// The camera will tween to its destination. /// &lt;/summary&gt; /// &lt;value&gt;The move speed.&lt;/value&gt; float MoveSpeed { get; set; } /// &lt;summary&gt; /// Gets or sets the rotation of the camera. /// &lt;/summary&gt; /// &lt;value&gt;The rotation.&lt;/value&gt; float Rotation { get; set; } /// &lt;summary&gt; /// Gets the origin of the viewport (accounts for Scale) /// &lt;/summary&gt; /// &lt;value&gt;The origin.&lt;/value&gt; Vector2 Origin { get; } /// &lt;summary&gt; /// Gets or sets the scale of the Camera /// &lt;/summary&gt; /// &lt;value&gt;The scale.&lt;/value&gt; float Scale { get; set; } /// &lt;summary&gt; /// Gets the screen center (does not account for Scale) /// &lt;/summary&gt; /// &lt;value&gt;The screen center.&lt;/value&gt; Vector2 ScreenCenter { get; } /// &lt;summary&gt; /// Gets the transform that can be applied to /// the SpriteBatch Class. /// &lt;/summary&gt; /// &lt;see cref="SpriteBatch"/&gt; /// &lt;value&gt;The transform.&lt;/value&gt; Matrix Transform { get; } /// &lt;summary&gt; /// Gets or sets the focus of the Camera. /// &lt;/summary&gt; /// &lt;seealso cref="IFocusable"/&gt; /// &lt;value&gt;The focus.&lt;/value&gt; IFocusable Focus { get; set; } /// &lt;summary&gt; /// Determines whether the target is in view given the specified position. /// This can be used to increase performance by not drawing objects /// directly in the viewport /// &lt;/summary&gt; /// &lt;param name="position"&gt;The position.&lt;/param&gt; /// &lt;param name="texture"&gt;The texture.&lt;/param&gt; /// &lt;returns&gt; /// &lt;c&gt;true&lt;/c&gt; if the target is in view at the specified position; otherwise, &lt;c&gt;false&lt;/c&gt;. /// &lt;/returns&gt; bool IsInView(Vector2 position, Texture2D texture); } public class Camera2D : GameComponent, ICamera2D { private Vector2 _position; protected float _viewportHeight; protected float _viewportWidth; public Camera2D(Game game) : base(game) {} #region Properties public Vector2 Position { get { return _position; } set { _position = value; } } public float Rotation { get; set; } public Vector2 Origin { get; set; } public float Scale { get; set; } public Vector2 ScreenCenter { get; protected set; } public Matrix Transform { get; set; } public IFocusable Focus { get; set; } public float MoveSpeed { get; set; } #endregion /// &lt;summary&gt; /// Called when the GameComponent needs to be initialized. /// &lt;/summary&gt; public override void Initialize() { _viewportWidth = Game.GraphicsDevice.Viewport.Width; _viewportHeight = Game.GraphicsDevice.Viewport.Height; ScreenCenter = new Vector2(_viewportWidth/2, _viewportHeight/2); Scale = 1; MoveSpeed = 1.25f; base.Initialize(); } public override void Update(GameTime gameTime) { // Create the Transform used by any // spritebatch process Transform = Matrix.Identity* Matrix.CreateTranslation(-Position.X, -Position.Y, 0)* Matrix.CreateRotationZ(Rotation)* Matrix.CreateTranslation(Origin.X, Origin.Y, 0)* Matrix.CreateScale(new Vector3(Scale, Scale, Scale)); Origin = ScreenCenter / Scale; // Move the Camera to the position that it needs to go var delta = (float) gameTime.ElapsedGameTime.TotalSeconds; _position.X += (Focus.Position.X - Position.X) * MoveSpeed * delta; _position.Y += (Focus.Position.Y - Position.Y) * MoveSpeed * delta; base.Update(gameTime); } /// &lt;summary&gt; /// Determines whether the target is in view given the specified position. /// This can be used to increase performance by not drawing objects /// directly in the viewport /// &lt;/summary&gt; /// &lt;param name="position"&gt;The position.&lt;/param&gt; /// &lt;param name="texture"&gt;The texture.&lt;/param&gt; /// &lt;returns&gt; /// &lt;c&gt;true&lt;/c&gt; if [is in view] [the specified position]; otherwise, &lt;c&gt;false&lt;/c&gt;. /// &lt;/returns&gt; public bool IsInView(Vector2 position, Texture2D texture) { // If the object is not within the horizontal bounds of the screen if ( (position.X + texture.Width) &lt; (Position.X - Origin.X) || (position.X) &gt; (Position.X + Origin.X) ) return false; // If the object is not within the vertical bounds of the screen if ((position.Y + texture.Height) &lt; (Position.Y - Origin.Y) || (position.Y) &gt; (Position.Y + Origin.Y)) return false; // In View return true; } } </code></pre> <p>And Here is how you would use it with <code>SpriteBatch</code>:</p> <pre><code>spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.FrontToBack, SaveStateMode.SaveState, Camera.Transform); spriteBatch.Draw(_heliTexture, _heliPosition, heliSourceRectangle, Color.White, 0.0f, new Vector2(0,0), 0.5f, SpriteEffects.FlipHorizontally, 0.0f); spriteBatch.End(); </code></pre> <p>Let Me know if this helps you out, and thanks to StackOverflow and the community. W00t!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload