- Use the matrix to determine the reference frame of the camera.
- Use split space to split the level into screens / windows.
Think of your gaming sprite like any other object, such as enemies and interactive objects. Now you need a camera abstraction. You can define the camera as a 3x3 matrix with this layout:
[rotX_X, rotY_X, 0] [rotX_Y, rotY_Y, 0] [transX, transY, 1]
The 2x2 submatter in the upper left corner is the rotation matrix. transX and transY defines the part of the translation, that is, the beginning. You also get scaling for free. Just simply scale the rotation part with a scalar, and you have a zoom.
For this to work correctly with rotation, your sprites must be polygons / primitives, such as triangles or squares; you cannot just apply the matrix to the positions of the sprites when drawing. If you do not need rotation, just the center point transformation will work fine. If you want the camera to follow the player, use the player’s position as the source of the camera. This is the translation vector [transX, transY] So, how do you apply the matrix to the positions of the entity and the vertices of the model? You are doing vector matrix multiplication. v '= vM ^ -1 , where v' is the new vector, v is the old vector, and M ^ -1 is the inverse matrix. The camera must be an inverse transformation, since it defines a local coordinate system. The analogy may be: if you are in front of me, and I turn to the left of my frame of reference, I turn to the right. This applies to all affine and linear transformations, such as scaling, rotation, and translation.
Divide your level into parts so that you can select objects and decorations that do not need to be visualized. Your viewport has a specific size / resolution. Only visualize the scenery and objects that intersect with your viewport. Instead of checking each object at the borders of the viewing area, assign each object to a specific sub-screen and check the borders of the sub-screen relative to the borders of the view and camera. If your dividing your levels into parts that are the same size as your viewport, then the maximum number of screens at any given time is displayed:
- 2, if your camera only scrolls left and right.
- 4, if your camera scrolls left, right, up and down.
- 4, if your camera scrolls in any direction and can additionally be rotated.
Screen change is an event that you can use to activate objects belonging to this screen. It can be enemies, background animation, doors, or whatever you like.
Mads elvheim
source share