I am trying to make a “cascaded shadow” effect in my 2D sprite game, the way I currently handle my game map is that I draw my scenes objects in the following order:
Floor tiles → Shadows of “Overlay” tiles → Overlay tiles
The floor tiles is a simple draw call, we use a matrix that simply moves the world position of our camera to follow a game character like so:
Transform = Matrix.Identity *
Matrix.CreateTranslation(-Position.X, -Position.Y, 0) *
Matrix.CreateRotationZ(Rotation) *
Matrix.CreateTranslation(0, 0, 0) *
Matrix.CreateScale(new Vector3(Scale, Scale, Scale));
Drawing the floor:
spriteBatch.Begin(SpriteSortMode.BackToFront,
BlendState.AlphaBlend,
SamplerState.PointClamp,
DepthStencilState.None,
RasterizerState.CullCounterClockwise,
null,
Transform);
// Called for every 32x32 tile in the Floor layer
spriteBatch.Draw(tileSource, position, sourceRect, Color.White,
0f, new Vector2(0, 0), 1.0f, SpriteEffects.None, LayerDepth);
spriteBatch.End();
And then, just before we draw our Overlay tiles, I call another draw with an altered matrix that skews the perspective of the next draw like so:
Transform = Matrix.Identity *
Matrix.CreateRotationX(MathHelper.ToRadians(45)) *
Matrix.CreateRotationY(MathHelper.ToRadians(45)) *
Matrix.CreateScale(1, 1, 0) *
Matrix.CreateTranslation(-Position.X, -Position.Y, 0) *
Matrix.CreateRotationZ(Rotation) *
Matrix.CreateTranslation(0, 0, 0) *
Matrix.CreateScale(new Vector3(Scale, Scale, Scale));
spriteBatch.Begin(...same as before but with updated Transform)
// Called for every 32x32 tiles in the "Overlay" layer, as a Color.Black colour
spriteBatch.Draw(tileSource, position, sourceRect, Color.Black,
0f, new Vector2(0, 0), 1.0f, SpriteEffects.None, LayerDepth);
spriteBatch.End()
We then reset the matrix back to what it was before (None-Skewed) and draw our Overlays similar to the floor tiles and get the following:
So it’s obvious as to why this looks a bit jank at the moment, we’re drawing a set of tiles in given Vector2 positions with one matrix, and the exact same Vector2 positions with a different matrix! Of course they’re going to be positioned differently!
So the issue is basically I need to find a mathematical way to get offset my Vector2 positions for my “Shadow tiles” so they can appear in the same position where the Overlay tiles are. I’m not quite sure how to do that, I’m assuming I need to alter the Vector2s of the Shadow Tiles in some way? Would appreciate any suggestions or help!