Camera functions to convert between world and screen coordinates

This commit is contained in:
Simon Holmberg
2012-09-27 23:08:11 +02:00
parent a6117af112
commit 11aef39e29
2 changed files with 14 additions and 4 deletions
+10 -1
View File
@@ -28,6 +28,16 @@ namespace DepthsBelow
Speed = 300;
}
public Vector2 ScreenToWorld(Vector2 screenPos)
{
return Vector2.Transform(screenPos, Matrix.Invert(Transform));
}
public Vector2 WorldToScreen(Vector2 worldPos)
{
return Vector2.Transform(worldPos, Transform);
}
public void Update(GameTime gameTime)
{
float elapsed = gameTime.ElapsedGameTime.Milliseconds / 1000.0f;
@@ -48,7 +58,6 @@ namespace DepthsBelow
Matrix.CreateTranslation(new Vector3(-Position.X, -Position.Y, 0))
* Matrix.CreateRotationZ(Rotation)
* Matrix.CreateScale(Zoom)
//* Matrix.CreateTranslation(new Vector3(
;
}
}
+4 -3
View File
@@ -68,9 +68,10 @@ namespace DepthsBelow
}
// Grid highlighting
Point currentGrid = Grid.ScreenToGrid(new Vector2(ms.X + core.camera.Position.X, ms.Y + core.camera.Position.Y));
Vector2 currentScreen = Grid.GridToScreen(currentGrid);
gridRectangle = new Rectangle((int)currentScreen.X, (int)currentScreen.Y, Grid.TileSize, Grid.TileSize);
Vector2 clickWorldPos = core.camera.ScreenToWorld(new Vector2(ms.X, ms.Y));
Point clickGridPos = Grid.WorldToGrid(clickWorldPos);
Vector2 rectWorldPos = Grid.GridToWorld(clickGridPos);
gridRectangle = new Rectangle((int)rectWorldPos.X, (int)rectWorldPos.Y, Grid.TileSize, Grid.TileSize);
lastMouseState = ms;
}