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 wonder if the real fix for all this would be to kill _process altogether for user code and make it call _physics_process internally (and then use that for the camera again).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. The camera was updated in _physics_process before #15060, and is now updated inside _process as this yields a higher frame rate and should be smoother.īefore #15060, if you had your object positions update inside _process, you would get jitter and loss of sync as the camera lagged behind, only updating on _physics_process once in a while.Īfter #15060, the camera moves more frequently than the positions get updated in _physics_process, and that's probably the out-of-syncness that happens here. That said, there are systems and frame rates, where _process and _physics_process always coincide, but there are others where they don't. So, the calls for both might look like this (like seen from the Godot main event loop): I can't reproduce the problem from the code, but I thought about this a bit, so let me summarize some things.Īs noted above, _process gets called on every display frame, whereas _physics_process only gets called with a lower frame rate every once in a while (when a time delta, that gets summed up, hits a certain tolerance).
0 Comments
Leave a Reply. |