See how the camera works, and how to draw to the different coordinate systems in your program. Written by Andrew Cain on Aug 16 2018
This article provides information on how to use the camera in SplashKit to create games that exist within a world that is larger than the size viewable on the window. In these contexts you can work with game coordinates and move the camera around to determine what the user can see.
Coordinates and the Camera
The camera provides an easy way to move around a game world. It is set to 0,0 by default, however the offset can be changed using the different camera procedures in order to explore a larger game world. When the camera position changes it moves the window to another part of the game world, as you can see in the image below.
When drawing use ‘game coordinates’ for each of your entities, which describe where an entity is in relation to other entities in the game. Some of these may be on the screen or off the screen, but we can let SplashKit take care of that. In the above image the dancing frog is at 250,300 in the game world.
When you draw to the screen SplashKit adjusts the screen position based on the current camera offset. So the dancing frog in the above image is at 200,200 on the screen as the camera is at 50,100. The camera defines the coordinate of the top left of the window.
With the camera you can create the effect of moving past stationary background objects by moving the player and the camera at the same time. As you move the camera, stationary objects drawn to the screen will appear in different locations on the Window.
- Set Camera X - adjust the x value for the camera
- Set Camera Y - adjust the y value for the camera
- Set Camera Position - adjust the camera position
- Move Camera By - move the camera a specified amount
- Move Camera To - move the camera to a location
- Center Camera On - position the camera to focus on a sprite
Where is the mouse?
Once you use the camera, then you need to map any interactions with the screen from screen to world coordinates. For example, when you read mouse input the values are in ‘screen coordinates’. In this system the coordinates always have 0,0 as the top left-hand corner, regardless of the camera position.
- To Screen converts values from world coordinates to screen coordinates.
- To World converts a screen coordinate to a game world coordinate.
- To World X converts a screen x value to its location in the game world.
- To World Y converts a screen y value to its location in the game world.
- To Screen X converts a game world x value to its location on the screen.
- To Screen Y converts a game world y value to its location on the screen.
Drawing directly to the screen
Some game elements will need to remain in the same place, regardless of the camera position. To achieve this you need to use the Option To Screen. Drawing options allow you to customise each of the drawing instructions. Each drawing operation include a variation that accepts a Drawing Option value.
- Option To Screen ignore camera position when drawing
- Option To World adjust all positions based on the camera position
The following program code demonstrates the use of some of these camera operations.