I have been wanting to work on a top-down space exploration/building game for a while and finally had a chance to get started on it.
As a first step I wanted to try and implement a 2d camera system with zoom and rotation. I think I did a reasonably good job. but would like some feedback on my implementation. For fun I also added a very quick bouncing ball physics simulation.
Controls
- Camera - Pan: WASD, Arrow Keys
- Camera - Zoom Out and In: ( 1 and 2 ), ( - and = ), ( NUM- and NUM+ )
- Camera - Rotate Left and Right: ( Q and E ), ( [ and ] )
- Gravity - Toggle: G
- Gravity - Up, Left, Down, Right: I,K,J,L
- Balls - Clear All: C
- Balls - Reset: R
- Balls - Add Ball: . (period)
- Balls - Remove Ball: , (comma)
The Camera
Basically the camera object generates a Transformation Matrix once per frame, which is then applied to the position vectors of all the objects on the screen. (in this case just the position of the balls.
I have 4 3x3 matrixes, which I multiply together in the following order:
Code: Select all
m_offset * m_rotation * m_zoom * m_position
This seems to be doing the trick for me; The camera behaves as I would like it to, but I'm wondering if there might be a more efficient way to handle the generation of the transformation matrix. I've looked and haven't found any resources online that explain this clearly step by step, they only ever talk about rotation alone, or zoom alone.
So I'd like to ask those with a bit more experience here, is this similar to how you might approach a 2d camera?
Other minor things of note
- I could easily implement smoothing/easing of the camera values, as well as a bounding box; just hasn't been a priority yet.
- Similarly, I don't really have a proper component/game object system in place yet.
- I'm quite happy with my the binding implementation (controls.lua); I hate it when games don't allow you to bind multiple keys to something. I took this idea from a small incomplete love2d game that I found on Klei Entertainment's Github.
- This was my first successful attempt at writing a simple lua-based Class system that allows for inheritance.
- Next I'm hoping to implement a Quad tree to try culling away the objects outside the edges of the camera.
- On a similar note, I want to get started on a chunk-based tile system that lets me save/load chunks dynamically (similar to what Minecraft does for example)
- As you can probably tell from the client/server files I would like to attempt to make this multiplayer-capable at some point.
- I know that there are plenty of class/physics/tiling solutions out there, but I prefer to learn by trying to implement things myself before I go the easy route