Page 1 of 1

Feedback Request: 2d Camera with Pan/Rotate/Zoom

Posted: Thu Apr 24, 2014 8:53 pm
by Johannes
Overview

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.

Image
2d_camera_0.0.1.zip
2d camera 0.0.1 - windows and mac launch script included.
(7.24 MiB) Downloaded 237 times

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)
Basically the balls are only there as a point of reference. When you pan/rotate it's just the camera moving, not the 'box' containing the balls.

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
The resulting 3x3 matrix is then multiplied with any given 2d vector (which is first converted to a 3x1 matrix with the third value set to 1). Due to how matrix multiplication works, this is the same as taking the vector as a 1x3 matrix and then multiplying position, then zoom, then rotation, and finally offset.

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 :)

Re: Feedback Request: 2d Camera with Pan/Rotate/Zoom

Posted: Fri Apr 25, 2014 5:04 pm
by Johannes
Minor Bump because it was moved from Projects to Support (thanks bartbes). Would still appreciate some feedback :)

Re: Feedback Request: 2d Camera with Pan/Rotate/Zoom

Posted: Fri Apr 25, 2014 7:06 pm
by davisdude
It looks like it works well to me. Nice work! :)

Re: Feedback Request: 2d Camera with Pan/Rotate/Zoom

Posted: Mon Apr 28, 2014 3:10 pm
by Johannes
Just as an update, I changed my camera implementation and replaced my own calculations of the transformation matrix with just using love.graphics' Coordinate methods.
2d_camera_0.0.2.zip
(7.24 MiB) Downloaded 390 times
It runs 30-50% faster now :)