Camera - Team-Innis/UtH-Engine GitHub Wiki
The camera class is a utility that allows you to transform the entire world without having to modify each object individually.
Camera
has a lot of the same functionality as GameObject
does, allowing you to easily move, rotate and zoom the view.
Usage
- Create a
Camera
object. - Set the camera to a
RenderTarget
. The target stores a pointer to the camera, so it must stay alive as long as it's being used. - Use the camera. Any changes you make will automatically affect the view when drawing to the same target.
Default Camera
The game also includes a default camera that is attached to the window.
Camera& camera = uthEngine.GetWindow().GetCamera();
Viewport scaling
If you would like to scale the game to always render at the same resolution you can set the size of the default camera.
// Note: you should probably do this after engine has been initialized
// (e.g. after uthEngine.Init() in main.cpp or in the init of your scene).
camera.SetSize(1280, 720);
If you do this you need to use PixelToCoords function found in RenderTarget
to handle input coordinates. You also need to use this function if the camera is not positioned so that the origin is in the top left corner(see below)
pmath::Vec2 worldPosition = uthEngine.GetWindow().PixelToCoords(someScreenPosition);
Or you need to use CoordsToPixel for the reverse
pmath::Vec2 screenPosition = uthEngine.GetWindow().CoordsToPixel(someWorldPosition);
Origin
You also most likely want to change the origin of the game so that (0, 0)
is in the top left corner (default is center of the screen). Do note that the mouse and touch input origins are in the top left corner
// It is probably a good idea to move by camera size so it remains independent
// of the window resolution and moves by the actualy size of the game world.
pmath::Vec2 cameraSize = camera .GetSize();
camera.Scroll(cameraSize.w / 2, cameraSize.h / 2);
// You could also use the window size but this only works
// if the camera size is the same as the window size
pmath::Vec2 windowSize = uthEngine.GetWindow().GetSize();
camera.Scroll(windowSize .w / 2, windowSize .h / 2);