Home Game Development xna – Keeping 2D digicam inside recreation bounds

xna – Keeping 2D digicam inside recreation bounds

0
xna – Keeping 2D digicam inside recreation bounds

[ad_1]

I feel your digicam class wants some tuning up. If you comply with together with this hyperlink you may have a pleasant implementation to work with.

Now on to the matter of proscribing your digicam to the world boundaries.

The algorithm seems to be like this:

  1. Take the digicam boundaries (place + viewport) and rework them into world coordinates.
  2. Restrict these coordinates in order that they’re contained in the world boundaries.
  3. Transform the restricted coordinates again into digicam coordinates.

Note: I’m assuming the digicam place is meant to align with the middle of the point of view.

In order to search out the world house coordinates you will have an inverse rework which you will get from the rework like this: var inverseTransform = Matrix.Invert(rework);

//Requirements
Vector2 cameraPosition;
Viewport viewport;
Matrix rework;
Matrix inverseTransform;

//Get the highest left and backside proper place of the digicam in display house.
var cameraTopLeft = cameraPosition - new Vector2(viewport.Width / 2f, viewport.Height / 2f);
var digicamBottomRight = cameraPosition + new Vector2(viewport.Width / 2f, viewport.Height / 2f);

//Transform the screenspace coordinates into world house coordinates.
var cameraTopLeftWorld = Vector2.Transform(cameraTopLeft, inverseTransform);
var digicamBottomRightWorld = Vector2.Transform(digicamBottomRight, inverseTransform);

//Create the world house boundary rectangle of the digicam
var width = digicamBottomRightWorld.X - cameraTopLeftWorld.X;
var top = digicamBottomRightWorld.Y - cameraTopLeftWorld.Y;
var bounds = new Rectangle(cameraTopLeftWorld.X, cameraTopLeftWorld.Y, width, top);

//Use the bounds rectangle to limit the digicam place.
if(bounds.X < worldBounds.X) bounds.X = worldBounds.X; //Off the left facet
if(bounds.Y < worldBounds.Y) bounds.Y = worldBounds.Y; //Off the highest facet
if(bounds.Right > worldBounds.Right) bounds.X = worldBounds.Right - bounds.Width; //Off the best facet
if(bounds.Bottom > worldBounds.Bottom) bounds.Y = worldBounds.Bottom - bounds.Height; //Off the underside facet

//Now take the restricted boundaries and rework them again right into a place for the digicam
var boundsCenter = new Vector2(bounds.X + bounds.Width / 2f, bounds.Y + bounds.Height / 2f);
var cameraCenterPosition = Vector2.Transform(boundsCenter, rework);
cameraPosition = cameraCenterPosition;

This algorithm is sort of primary however works effectively. However, if you happen to embrace rotation into your digicam then this may fail.

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here