# Easy way to create shadow map for 2D game?

I’m new to Monogame and looking for easy method for create shadow map for my top-down rpg game. Lighting was implemented via per-pixel lighting.

Light source class looks like this:

``````public class Light
{
public Vector2 Position { get; set; }

public float Radius { get; set; }

public Color Color { get; set; }

public float Intensity { get; set; }
}
``````

I know that a light-view-projection matrix is needed, I tried to get it, but I’m not sure if it works correctly:

``````var lightDirection = Vector3.Normalize(lightPosition - position);

var viewMatrix = Matrix.CreateLookAt(
lightPosition,
lightPosition + lightDirection,
Vector3.One
);
var projectionMatrix = Matrix.CreateOrthographic(
0.1f,
0.1f,
-1,
1
);

var lightViewProjectionMatrix = viewMatrix * projectionMatrix;
``````

let’s update your code to generate the light’s view and projection matrices correctly:

``````// Assuming position is the position of the object casting shadows
Vector3 lightDirection = Vector3.Normalize(light.Position - position);

// Calculate the light's view matrix
Matrix viewMatrix = Matrix.CreateLookAt(
light.Position,
light.Position + lightDirection,
Vector3.Up // Assuming up direction is up
);

// Adjust the orthographic projection based on your scene's requirements
float orthoWidth = ...; // Define the width of the orthographic projection
float orthoHeight = ...; // Define the height of the orthographic projection
float orthoNearPlane = ...; // Define the near plane of the orthographic projection
float orthoFarPlane = ...; // Define the far plane of the orthographic projection

// Create the light's orthographic projection matrix
Matrix projectionMatrix = Matrix.CreateOrthographic(
orthoWidth,
orthoHeight,
orthoNearPlane,
orthoFarPlane
);

// Combine the view and projection matrices to get the light's view-projection matrix
Matrix lightViewProjectionMatrix = viewMatrix * projectionMatrix;
``````

Be sure to adjust the orthographic projection parameters (`orthoWidth`, `orthoHeight`, `orthoNearPlane`, `orthoFarPlane`) according to the dimensions and requirements of your scene.

// Ensure correct camera position
var cameraPosition = new Vector3(camera.Position.X, camera.Position.Y, /* Adjust Z position as needed */);

// Calculate light direction
var lightDirection = Vector3.Normalize(light.Position - cameraPosition);

// Create view matrix
var viewMatrix = Matrix.CreateLookAt(
cameraPosition,
cameraPosition + lightDirection,
Vector3.Up // Adjust up vector as needed
);

// Adjust near and far planes based on your game world
float nearPlane = /* Adjust near plane value based on your game world /;
float farPlane = /
Adjust far plane value based on your game world */;

// Create orthographic projection matrix
var projectionMatrix = Matrix.CreateOrthographicOffCenter(
/* Adjust left, right, bottom, and top values based on your game world */,
nearPlane,
farPlane
);

// Create light-view-projection matrix
var lightViewProjectionMatrix = viewMatrix * projectionMatrix;

Make sure to adjust the commented sections based on the specifics of your game world and camera setup. Additionally, consider testing your shadow mapping implementation with simple scenes to ensure it behaves as expected before integrating it into your top-down RPG game.

Pretty clever, great! when you have a break from work, do you play CoDMW3, for example? a great shooter with various unique mechanics. and if you have difficulties with something or just with a call of duty modern warfare 3 boosting, this service will help you very quickly with what you need)