My 2D engine is resolution independent and has a camera based on the article here: http://www.david-gouveia.com/portfolio/2d-camera-with-parallax-scrolling-in-xna/
I've implemented parallax scrolling into my camera class the same way the article above mentions. It works great, but it's screwed up my culling code and I'm struggling to figure out the math.
Each of my background images has a Rectangle I use to check if it's currently on screen. If it doesn't collide with my Camera rectangle, I don't draw it. The problem is that if the image is drawing as a parallax layer, the rectangle isn't being calculated where it really appears on the screen.
The transform matrix in my Camera class looks like this:
public static Matrix GetTransformMatrix(Vector2 parallax)
{
return Matrix.CreateTranslation(new Vector3(-position * parallax, 0)) * Matrix.CreateRotationZ(rotation) *
Matrix.CreateScale(new Vector3(zoom, zoom, 1)) * Matrix.CreateTranslation(new Vector3(Resolution.VirtualWidth
* 0.5f, Resolution.VirtualHeight * 0.5f, 0));
}
For each parallax layer, I call SpriteBatch.Begin() and pass it the above transform matrix with the correct parallax offset passed in depending on what layer we're drawing (foreground, background etc.)
I've successfully made a ScreenToWorld function that works for getting the position of where the mouse has been clicked. Note that I need to calculate both my resolution matrix and camera matrix for it to work.
public static Vector2 ScreenToWorld(Vector2 input, Vector2 parallax)
{
input.X -= Resolution.VirtualViewportX;
input.Y -= Resolution.VirtualViewportY;
Vector2 resPosition = Vector2.Transform(input, Matrix.Invert(Resolution.getTransformationMatrix()));
Vector2 finalPosition = Vector2.Transform(resPosition, Matrix.Invert(Camera.GetTransformMatrix(parallax)));
return finalPosition;
}
So I figured to calculate the correct Rectangle positions of my parallax layers I would need a WorldToScreen function... I tried this, but it isn't working:
public static Vector2 WorldToScreen(Vector2 input, Vector2 parallax) //I pass the same parallax value that is used in the Camera matrix function.
{
input.X -= Resolution.VirtualViewportX;
input.Y -= Resolution.VirtualViewportY;
Vector2 resPosition = Vector2.Transform(input, Resolution.getTransformationMatrix());
Vector2 finalPosition = Vector2.Transform(resPosition, Camera.GetTransformMatrix(parallax));
return finalPosition;
}
I'm guessing I'm on the right track but my math is wrong? I'm passing the above function the non-parallaxed Rectangle position with the hopes of making it update to where the parallax image is really being drawn. Thanks in advance if someone can help!