mirror of
https://github.com/AlexMacocian/MonoGame.Extended.git
synced 2026-07-15 15:09:29 +00:00
camera pretty much complete
This commit is contained in:
@@ -46,10 +46,13 @@ namespace Game1
|
||||
{
|
||||
}
|
||||
|
||||
private int _previousScrollWheelValue = 0;
|
||||
|
||||
protected override void Update(GameTime gameTime)
|
||||
{
|
||||
var deltaTime = (float) gameTime.ElapsedGameTime.TotalSeconds;
|
||||
var keyboardState = Keyboard.GetState();
|
||||
var mouseState = Mouse.GetState();
|
||||
var gamePadState = GamePad.GetState(PlayerIndex.One);
|
||||
|
||||
if (gamePadState.Buttons.Back == ButtonState.Pressed || keyboardState.IsKeyDown(Keys.Escape))
|
||||
@@ -58,23 +61,41 @@ namespace Game1
|
||||
var up = new Vector2(0, -250);
|
||||
var right = new Vector2(250, 0);
|
||||
|
||||
// rotation
|
||||
if (keyboardState.IsKeyDown(Keys.Q))
|
||||
_camera.Rotation -= deltaTime;
|
||||
|
||||
if (keyboardState.IsKeyDown(Keys.W))
|
||||
_camera.Rotation += deltaTime;
|
||||
|
||||
// movement
|
||||
var direction = Vector2.Zero;
|
||||
|
||||
if (keyboardState.IsKeyDown(Keys.Up))
|
||||
_camera.Position += up * deltaTime;
|
||||
direction += up * deltaTime;
|
||||
|
||||
if (keyboardState.IsKeyDown(Keys.Down))
|
||||
_camera.Position += -up * deltaTime;
|
||||
direction += -up * deltaTime;
|
||||
|
||||
if (keyboardState.IsKeyDown(Keys.Left))
|
||||
_camera.Position += -right * deltaTime;
|
||||
direction += -right * deltaTime;
|
||||
|
||||
if (keyboardState.IsKeyDown(Keys.Right))
|
||||
_camera.Position += right * deltaTime;
|
||||
direction += right * deltaTime;
|
||||
|
||||
_camera.Move(direction);
|
||||
|
||||
// zoom
|
||||
var scrollWheelDelta = mouseState.ScrollWheelValue - _previousScrollWheelValue;
|
||||
|
||||
if (scrollWheelDelta != 0)
|
||||
_camera.Zoom += scrollWheelDelta * 0.0001f;
|
||||
|
||||
_previousScrollWheelValue = mouseState.ScrollWheelValue;
|
||||
|
||||
// look at
|
||||
if (mouseState.LeftButton == ButtonState.Pressed)
|
||||
_camera.LookAt(_camera.ToScreenSpace(new Vector2(mouseState.X, mouseState.Y)));
|
||||
|
||||
base.Update(gameTime);
|
||||
}
|
||||
@@ -87,12 +108,12 @@ namespace Game1
|
||||
_spriteBatch.Draw(_backgroundTextureSky, Vector2.Zero);
|
||||
_spriteBatch.Draw(_backgroundTextureClouds, Vector2.Zero);
|
||||
_spriteBatch.End();
|
||||
|
||||
for(var i = 0; i < 4; i++)
|
||||
|
||||
for (var i = 0; i < 4; i++)
|
||||
{
|
||||
var parallaxFactor = new Vector2(1.0f + 0.2f * i, 1.0f - 0.05f * i);
|
||||
var transformMatrix = _camera.GetViewMatrix(parallaxFactor);
|
||||
_spriteBatch.Begin(transformMatrix: transformMatrix);
|
||||
var parallaxFactor = new Vector2(0.5f + 0.25f * i, 1.0f - 0.05f * i);
|
||||
var viewMatrix = _camera.GetViewMatrix(parallaxFactor);
|
||||
_spriteBatch.Begin(transformMatrix: viewMatrix);
|
||||
_spriteBatch.Draw(_backgroundTexture[i], Vector2.Zero);
|
||||
_spriteBatch.End();
|
||||
}
|
||||
|
||||
@@ -3,19 +3,18 @@ using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
namespace MonoGame.Extended
|
||||
{
|
||||
public class Camera2D : IMovable, IRotatable
|
||||
public class Camera2D
|
||||
{
|
||||
public Camera2D(Viewport viewport)
|
||||
: this(viewport.Width, viewport.Height)
|
||||
{
|
||||
}
|
||||
private readonly Viewport _viewport;
|
||||
|
||||
public Camera2D(int viewportWidth, int viewportHeight)
|
||||
public Camera2D(Viewport viewport)
|
||||
{
|
||||
_viewport = viewport;
|
||||
|
||||
Rotation = 0;
|
||||
Zoom = 1;
|
||||
Origin = new Vector2(viewportWidth / 2f, viewportHeight / 2f);
|
||||
Position = Origin;
|
||||
Origin = new Vector2(viewport.Width / 2f, viewport.Height / 2f);
|
||||
Position = Vector2.Zero;
|
||||
}
|
||||
|
||||
public Vector2 Position { get; set; }
|
||||
@@ -23,45 +22,26 @@ namespace MonoGame.Extended
|
||||
public float Zoom { get; set; }
|
||||
public Vector2 Origin { get; set; }
|
||||
|
||||
public Vector2 ToWorldSpace(Vector2 position)
|
||||
public void Move(Vector2 direction)
|
||||
{
|
||||
return Vector2.Transform(position, Matrix.Invert(GetViewMatrix()));
|
||||
Position += Vector2.Transform(direction, Matrix.CreateRotationZ(-Rotation));
|
||||
}
|
||||
|
||||
public Vector2 ToWorldSpace(float x, float y)
|
||||
public void LookAt(Vector2 position)
|
||||
{
|
||||
return ToWorldSpace(new Vector2(x, y));
|
||||
Position = position - new Vector2(_viewport.Width / 2f, _viewport.Height / 2f);
|
||||
}
|
||||
|
||||
public Vector2 ToScreenSpace(Vector2 position)
|
||||
public Vector2 WorldToScreen(Vector2 worldPosition)
|
||||
{
|
||||
return Vector2.Transform(position, GetViewMatrix());
|
||||
return Vector2.Transform(worldPosition, GetViewMatrix());
|
||||
}
|
||||
|
||||
public Vector2 ToScreenSpace(float x, float y)
|
||||
public Vector2 ScreenToWorld(Vector2 screenPosition)
|
||||
{
|
||||
return ToScreenSpace(new Vector2(x, y));
|
||||
return Vector2.Transform(screenPosition, Matrix.Invert(GetViewMatrix()));
|
||||
}
|
||||
|
||||
//public Rectangle GetVisibileRectangle(int screenWidth, int screenHeight)
|
||||
//{
|
||||
// // NOT TESTED - Source: http://gamedev.stackexchange.com/questions/59301/xna-2d-camera-scrolling-why-use-matrix-transform
|
||||
// var viewMatrix = GetViewMatrix();
|
||||
// var inverseViewMatrix = Matrix.Invert(viewMatrix);
|
||||
// var topLeft = Vector2.Transform(Vector2.Zero, inverseViewMatrix);
|
||||
// var topRight = Vector2.Transform(new Vector2(screenWidth, 0), inverseViewMatrix);
|
||||
// var bottomLeft = Vector2.Transform(new Vector2(0, screenHeight), inverseViewMatrix);
|
||||
// var bottomRight = Vector2.Transform(new Vector2(screenWidth, screenHeight), inverseViewMatrix);
|
||||
// var min = new Vector2(
|
||||
// MathHelper.Min(topLeft.X, MathHelper.Min(topRight.X, MathHelper.Min(bottomLeft.X, bottomRight.X))),
|
||||
// MathHelper.Min(topLeft.Y, MathHelper.Min(topRight.Y, MathHelper.Min(bottomLeft.Y, bottomRight.Y))));
|
||||
// var max = new Vector2(
|
||||
// MathHelper.Max(topLeft.X, MathHelper.Max(topRight.X, MathHelper.Max(bottomLeft.X, bottomRight.X))),
|
||||
// MathHelper.Max(topLeft.Y, MathHelper.Max(topRight.Y, MathHelper.Max(bottomLeft.Y, bottomRight.Y))));
|
||||
|
||||
// return new Rectangle((int)min.X, (int)min.Y, (int)(max.X - min.X), (int)(max.Y - min.Y));
|
||||
//}
|
||||
|
||||
public Matrix GetViewMatrix(Vector2 parallaxFactor)
|
||||
{
|
||||
return
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace MonoGame.Extended
|
||||
{
|
||||
public interface IMovable
|
||||
{
|
||||
Vector2 Position { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
namespace MonoGame.Extended
|
||||
{
|
||||
public interface IRotatable
|
||||
{
|
||||
float Rotation { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -35,8 +35,6 @@
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Camera2D.cs" />
|
||||
<Compile Include="IMovable.cs" />
|
||||
<Compile Include="IRotatable.cs" />
|
||||
<Compile Include="SpriteBatchExtensions.cs" />
|
||||
<Compile Include="TextureRegion2D.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
|
||||
Reference in New Issue
Block a user