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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Johannes
Prole
Posts: 18
Joined: Sat Jun 08, 2013 6:51 pm

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

Post 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 236 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 :)
User avatar
Johannes
Prole
Posts: 18
Joined: Sat Jun 08, 2013 6:51 pm

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

Post by Johannes »

Minor Bump because it was moved from Projects to Support (thanks bartbes). Would still appreciate some feedback :)
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

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

Post by davisdude »

It looks like it works well to me. Nice work! :)
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
Johannes
Prole
Posts: 18
Joined: Sat Jun 08, 2013 6:51 pm

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

Post 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 389 times
It runs 30-50% faster now :)
Post Reply

Who is online

Users browsing this forum: Bing [Bot], slime and 8 guests