Var canvas_scale = get_viewport().canvas_transform.get_scale() # We can use viewport's canvas transform scale to which the camera has # When attention is given to the camera's zoom, we need to account for it. Var ofs := scroll_offset - layer.position Here's a workaround to offset the parallax layers to the positions they were in editor: extends ParallaxBackgroundįunc revert_offset(layer: ParallaxLayer) -> void: This is a bummer when, you know, your camera doesn't start at (0, 0). Vector2 positionOffset = cameraPosition - cameraWorldMin ĬameraPosition = Vector2.Clamp(cameraWorldMin, Vector2.The big offset jump is caused by the Parallax nodes calculating the offset when the Camera2D updates upon entering the scene tree or the next process frame. Vector2 cameraSize = new Vector2(viewport.Width, viewport.Height) / zoom Vector2 cameraWorldMin = Vector2.Transform(Vector2.Zero, Matrix.Invert(TranslationMatrix(1f, 1f))) Public void MoveCamera(Vector2 cameraMovement) 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. 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.
0 Comments
Leave a Reply. |