I’m currently optimizing drawing my tiled map, so instead of calling SpriteBatch.Draw for all of the tiles, I’m going to use a vertex and index buffer. As a test I just tried to draw one tile, but the camera view matrix screws it up (drawing without it works fine). When I use the camera view matrix with SpriteBatch.Draw it works fine.
TiledMap.TileSize is 16
This is my Draw function
`protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(backgroundColor);
var basicEffect = new BasicEffect(GraphicsDevice);
basicEffect.TextureEnabled = true;
basicEffect.VertexColorEnabled = false;
basicEffect.World = Matrix.Identity;
int tx = 176;
int ty = 496;
var rect = new Rectangle(0, 0, TiledMap.TileSize, TiledMap.TileSize);
basicEffect.View = Scene.Camera.TransformMatrix;
basicEffect.Projection = Matrix.CreateOrthographicOffCenter(0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0, 0, -1);
basicEffect.Texture = Content.Load<Texture2D>("Textures/Tiles/collisionmask");
var vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionTexture), 4, BufferUsage.WriteOnly);
var indexBuffer = new IndexBuffer(GraphicsDevice, typeof(short), 6, BufferUsage.WriteOnly);
var vertices = new VertexPositionTexture[4]
{
new VertexPositionTexture(new Vector3(tx, ty + TiledMap.TileSize, 0), new Vector2(rect.Left, rect.Bottom)),
new VertexPositionTexture(new Vector3(tx, ty, 0), new Vector2(rect.Left, rect.Top)),
new VertexPositionTexture(new Vector3(tx + TiledMap.TileSize, ty + TiledMap.TileSize, 0), new Vector2(rect.Right, rect.Bottom)),
new VertexPositionTexture(new Vector3(tx + TiledMap.TileSize, ty, 0), new Vector2(rect.Right, rect.Top))
};
var indices = new short[6]
{
0, 1, 2,
2, 1, 3
};
vertexBuffer.SetData(vertices);
indexBuffer.SetData(indices);
GraphicsDevice.SetVertexBuffer(vertexBuffer);
GraphicsDevice.Indices = indexBuffer;
foreach (var pass in basicEffect.CurrentTechnique.Passes)
{
pass.Apply();
GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 2);
}
vertexBuffer.Dispose();
indexBuffer.Dispose();
base.Draw(gameTime);
}`
And this is how the camera matrix is calculated (PixelsPerUnit = 1)
`protected virtual void UpdateMatrices()
{
if (!matricesDirty)
return;
// Translate position
transformMatrix = Matrix.CreateTranslation(-transform.Position.X, -transform.Position.Y, 0) *
Matrix.CreateScale(PixelsPerUnit, PixelsPerUnit, 1);
if (zoom != 1)
transformMatrix *= Matrix.CreateScale(zoom, zoom, 1); // Scale by zoom
transformMatrix *= Matrix.CreateScale(Screen.scale.X, Screen.scale.Y, 1);
transformMatrix *= Matrix.CreateTranslation(view.Width * 0.5f, view.Height * 0.5f, 0);
// Calculate our inverse as well
inverseTransformMatrix = Matrix.Invert(transformMatrix);
matricesDirty = false;
}`