I am working with my camera, and am trying to improve it so that I can calculate if a world point is on screen, taking rotation into account. Currently, my camera rectangle is calculated as so:
public Rectangle Rectangle
{
get
{
int width = (int)((ViewportWidth / _zoom));
int height = (int)((ViewportHeight / _zoom));
return new Rectangle((int)(_pos.X - width / 2), (int)(_pos.Y - height / 2), width, height);
}
}
Then, to check if a point is on screen, I call one of the following functions:
public bool IsOnScreen(Point location)
{
return this.Rectangle.Contains(location);
}
public bool IsOnScreen(Rectangle bounds)
{
return this.Rectangle.Intersects(bounds);
}
However, none of these calculations take the rotation factor into account; only position and the zoom factor. I know that I can calculate the corners of the camera, in world space, using the following transformations:
var topLeftCorner = Vector2.Transform(new Vector2(0, 0), Matrix.Invert(ViewMatrix));
var topRightCorner = Vector2.Transform(new Vector2(ViewportWidth, 0), Matrix.Invert(ViewMatrix));
var bottomLeftCorner = Vector2.Transform(new Vector2(0, ViewportHeight), Matrix.Invert(ViewMatrix));
var bottomRightCorner = Vector2.Transform(new Vector2(ViewportWidth, ViewportHeight), Matrix.Invert(ViewMatrix));
However, the rectangle object that I am using is not able to take this into account, since it cannot store rotation. What is the calculation that I can use to check if a point is inside the virtual rectangle with these points, or if a rectangle intersects with the virtual rectangle?
Here's some graphics to better illustrate some of my code:
location
in world space (3d) or screen space? \$\endgroup\$PointToRotate.X = CameraRectangleCenter.X+cos(-CameraRectangle.Angle)*distance(CameraRectangleCenter.X, CameraRectangleCenter.Y, PointToRotate.X, PointToRotate.Y); PointToRotate.Y = CameraRectangleCenter.X+sin(-CameraRectangle.Angle)*distance(CameraRectangleCenter.X, CameraRectangleCenter.Y, PointToRotate.X, PointToRotate.Y);
But still, it's not good solution to use. \$\endgroup\$