Return Vector2.Transform(screenClick - new Vector2(viewport.X, viewport.Y), Matrix.Invert(TranslationMatrix(1f, 1f))) Public Vector2 ScreenToWorld(Vector2 screenClick) Return Vector2.Transform(worldPosition, TranslationMatrix(1f, 1f)) Public Vector2 WorldToScreen(Vector2 worldPosition) Matrix.CreateTranslation(new Vector3(GetViewportCenter(), 0)) Matrix.CreateScale(new Vector3(zoom, zoom, 1)) * Return Matrix.CreateTranslation(-cameraPosition.X * scrollSpeedX, -cameraPosition.Y * scrollSpeedY, 0) * Public Matrix TranslationMatrix(float scrollSpeedX, float scrollSpeedY) Return new Vector2(viewport.Width * 0.5f, viewport.Height * 0.5f) If you want to start draw culling whats not on the screen you can easily get the top left and bottom right corner of what would be visible using ScreenToWorld and the coordinates of your viewport. If your using a viewport and not the top left of the screen adjust the size of it so ScreenToWorld is accurate. Zoom and rotation are pretty self explanatory. Use WorldToScreen or ScreenToWorld to get mouse positions etc. Clamping will clamp the camera to the cameraMax bounds. To move it around, call MoveCamera or CenterOn. If you were drawing background layers with parrelex give them smaller values, say 0.8f The 2 values you pass into translation matrix are an x and a y parralex scrolling offset. SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, null, camera.TranslationMatrix(1f, 1f)) To use, when starting a new spritebatch pass the camera.TranslationMatrix in. I’ve cut down one of my camera classes ive used. If you need to know anything else, do let me know. (SpriteSortMode.BackToFront, transformMatrix: ) Īnd here’s the result. Public MainMenu mainMenu = new MainMenu() Here’s the ‘Game1.cs’ code: public MainGame mainGame = new MainGame() If (Keyboard.GetState().IsKeyDown(playerInput.KBMoveRight)) If (Keyboard.GetState().IsKeyDown(playerInput.KBMoveLeft)) If (Keyboard.GetState().IsKeyDown(playerInput.KBMoveBackward)) ![]() If (Keyboard.GetState().IsKeyDown(playerInput.KBMoveForward)) ‘Player.cs’: public class Player : Entity Transform = (Position * Offset) * SmoothingValue Set the camera's transform to be the position value multiplied by the offset value. The offset value the position should be multiplied by, to ensure that the camera always keeps the entity at the center of the screen. ( / 2), // Y-position of the entity, minus half of its height. ( / 2), // X-position of the entity, minus half of its width. Create a "look-at", wherein the focus is on the center of entity. Here’s the code for the camera class, as well as the player class: I went ahead to get started on my camera class (‘Camera2D.cs’) and experienced some weird issues. But I am not sure of a good way to implement it.I recently got to work on a new project (for which I’ve chosen MonoGame, obviously), and thing is - I don’t really have much experience doing low-level stuff I’ve made games using Godot, Unity etc, where all the heavy lifting is done for you. ![]() I imagine I can achieve this by defining different rectangular shapes in my level and setting the camera limits when my player enters these shapes. For example, if the player is in a narrow corridor, the camera wouldn't scroll up or down. using the sides of the rectangle).īut what kind of node should I use? Area2D perhaps? Or maybe a ReferenceRect would be easier.Įventually, I'd like my level geometry to dictate the limits of the camera on a sub-level basis. When a level loads, I can access this node and set the limits of my camera using it (setting the camera's limit_left, limit_top, etc. I was thinking that I could have some kind of rectangular node per level. What is a good method for setting rectangular camera limits on a level-by-level basis? I want to set the limits of a Camera2D attached to my player on a level-by-level basis, but I am not sure of the best method to do this.
0 Comments
Leave a Reply. |