I’ve recently tried to implement the Penumbra library into my game so I could add lights. However, it’s using the same matrix that I use for my SpriteBatch, which always follows the player. There’s a problem with this since whenever I add a light, the light moves along with the camera instead of being stationary.
Like in this .gif:
Here’s the constructor of Game1:
public Game1() { graphics = new GraphicsDeviceManager(this); IsMouseVisible = true; graphics.PreferredBackBufferWidth = 1280; graphics.PreferredBackBufferHeight = 720; Window.Position = new Point((GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / 2) - (graphics.PreferredBackBufferWidth / 2), (GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / 2) - (graphics.PreferredBackBufferHeight / 2)); Content.RootDirectory = "Content"; // Lightning Engine penumbra = new PenumbraComponent(this); Components.Add(penumbra); penumbra.Lights.Add(new PointLight() { Position = new Vector2(720, 350) }); }
Here’s where I call the Draw method from Penumbra:
protected override void Draw(GameTime gameTime) { penumbra.BeginDraw(); controller.DrawObjects(ref spriteBatch);
base.Draw(gameTime); }
This is the other Draw method I use:
public void DrawObjects(ref SpriteBatch spriteBatch) { spriteBatch.GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(transformMatrix: Camera.TransformMatrix()); foreach (Component component in components) { component.Draw(ref spriteBatch); } spriteBatch.End(); }
And this is my Camera class:
class Camera
{
static Matrix transform;
public Camera() {
}
public void Follow(Sprite sprite) { Matrix position = Matrix.CreateTranslation( -sprite.Position().X - (sprite.Rectangle().Width / 2), -sprite.Position().Y - (sprite.Rectangle().Height / 2), 0); Matrix offset = Matrix.CreateTranslation( Game1.screenWidth / 2, Game1.screenHeight / 2, 0); transform = position * offset; }
static public Matrix TransformMatrix() { return transform; } }
I tried changing the Matrix like it says in the documentation:
But it didn’t work for me. Maybe I’m doing something wrong I don’t know. Here’s the code:
public Game1() { graphics = new GraphicsDeviceManager(this); IsMouseVisible = true; graphics.PreferredBackBufferWidth = 1280; graphics.PreferredBackBufferHeight = 720; Window.Position = new Point((GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / 2) - (graphics.PreferredBackBufferWidth / 2), (GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / 2) - (graphics.PreferredBackBufferHeight / 2)); Content.RootDirectory = "Content"; // Lightning Engine penumbra = new PenumbraComponent(this); Components.Add(penumbra); penumbra.SpriteBatchTransformEnabled = false; penumbra.Transform = Matrix.CreateTranslation(-720, -400, 0); penumbra.Lights.Add(new PointLight() { Position = new Vector2(0, 0) }); }
And the result:
If you could help me that would be really appreciated.