## CAMERA: scrolling and scaling

osuf oboys
Party member
Posts: 215
Joined: Sun Jan 18, 2009 8:03 pm

### CAMERA: scrolling and scaling

Data
module name: CAMERA
version: 3e
last updated: April 24, 2009

Introduction
CAMERA is a softlöve module that basically keeps track of what in the world the screen is currently centered on, how large everything should be drawn, and where on the screen the center is. The objects that keep track of this information are called cameras (although transformations would be more accurate) and are set in the same way as colors: call setCamera(cam) for an appropriate cam object before you draw. The default camera leaves the world just as it is and hence you may include this module without any visual change.

You can use cameras to achieve effects such as: sidescrollers and RTS's, zooming, seamlessly making your game work for any resolution (that has a fairly normal width-to-height w.r.t. how your game was designed), changing the drawing origin, minimaps, splitscreen, and the uncoupling of pixels from box2d engine. Some of these features can be applied to an already completed game merely by changing a few lines, other features go deeper and can take a few days to fully integrate. For newer projects, integrating the system should bring a number of benefits not covered above, such as the separation of different drawing layers.

Wiki
For more information, see the CAMERA wiki: http://love2d.org/wiki/index.php?title=CAMERA . You are encouraged to be bold and update the wiki and the module as you believe would benefit the community. If you find a better way to do something or find awesome missing features, then by all means implement
the changes and write a post about it. I would much rather see that someone takes this project, updates it, and calls it their own, than to do make a new project from scratch. This is a middle-way solution: we craft a scene-altering library for the needs of the community.

LPCL 1.0 - the LÖVE Community.

See http://love2d.org/wiki/index.php?title= ... ERA_Source for a discussion about things to be implemented.
Attachments
camera.lua
Version 3e. April 24, 2009.
Last edited by osuf oboys on Fri Apr 24, 2009 9:17 pm, edited 26 times in total.
If I haven't written anything else, you may assume that my work is released under the LPC License - the LÖVE Community. See http://love2d.org/wiki/index.php?title=LPC_License.
osuf oboys
Party member
Posts: 215
Joined: Sun Jan 18, 2009 8:03 pm

### Re: CAMERA: scrolling and scaling

Demos and applications
ballForAStroll.love
Demo for CAMERA
Controls:
Left mouse to place balls, right for a ball explosion
Mouse wheel, a, z, +, and - to zoom in and out. Also GUI buttons.
Spacebar for shaking.
u,i,o,j,k,l,m,f to change screen mode.
g and h for gravity.

Noteworthy features:
Only balls visible on the screen are drawn. The statistics text moves itself when there are 10 more more bodies. The world follows a ball; GUI remains stationary. Can zoom in and out arbitrarily; GUI remains stationary. Clouds in the background also change through your actions but to a lesser extent. A smaller version of the world is drawn on a minimap in the upper right corner with separate scaling. Shaking when the ball collides. You can change the config file (or use the mode keys) to play with any resolution that your machine supports. You can propel the ball by repeatedly placing new balls on top of / just below the ball.

Code Snippets with Examples
See for a number of example applications.
snippets.txt
Examples and common uses.
Last edited by osuf oboys on Mon Feb 02, 2009 10:17 am, edited 3 times in total.
If I haven't written anything else, you may assume that my work is released under the LPC License - the LÖVE Community. See http://love2d.org/wiki/index.php?title=LPC_License.
osuf oboys
Party member
Posts: 215
Joined: Sun Jan 18, 2009 8:03 pm

### Re: CAMERA: scrolling and scaling

Version 2 posted. Demo updated.

Code: Select all

Version 2, Feb 2, 2009
- Overloaded draw.graphics.draw tests for errors and skips angle/sx/sy if an error occurred.
- love.graphics.draw now accepts particle systems as well, there's no need to use
love.graphics.drawParticleSystem. However, the latter is preferable and
no rotation or scaling is available.
- added a mouse camera that is used for mouse callbacks. camera.lateInit() should now always be
called. If you want the usual behavior with absolute mouse coordinates, call camera.setMouseCamera(camera.new()).
If you set the mouse camera to nil, then the current regular camera will also be used as
mouse camera. setPosition and getPosition returns coordinates w.r.t. the current regular camera.
- camera.getMouseCamera(): returns the current camera used for mouse callbacks.
- camera.setMouseCamera(): camera that is used for mouse callbacks. Can be changed.
- added camera.isShapeVisible(shape) which allows you to avoid drawing shapes that are not visible (you
have to do this check manually).
- added string.linecount(s): returns the number of lines in a text, e.g. "Who?\nThem!" has two lines.
- added camera.getTextWidth(text, font) which should be used instead of camera.getFontWidth(font, text).
- added camera.getTextHeight(text, limit, font) which returns the height of text in pixels using the given font.
- added camera.getTextTailHeight(text, limit, font) which returns the height of all text besides the first line.
If wish to draw a text next to the bottom of the screen so that everything is visible,
then you can use y coordinate love.graphics.getHeight() - camera.getTextTailHeight(text)
- camera.splitTextByTextWidth(font, text, limit) changed to camera.splitTextByTextWidth(text, limit, font).
- fixed a bug when calling the new love.graphics.getScissor when there is no scissor.

If I haven't written anything else, you may assume that my work is released under the LPC License - the LÖVE Community. See http://love2d.org/wiki/index.php?title=LPC_License.
osuf oboys
Party member
Posts: 215
Joined: Sun Jan 18, 2009 8:03 pm

### Re: CAMERA: scrolling and scaling

A simpler CAMERA demo - can you climb the boxes?

Attachments
boxlimber.love
If I haven't written anything else, you may assume that my work is released under the LPC License - the LÖVE Community. See http://love2d.org/wiki/index.php?title=LPC_License.
Evil Telephone
Prole
Posts: 36
Joined: Fri Jan 30, 2009 9:31 pm

### Re: CAMERA: scrolling and scaling

YOU WIN!

Very cool. I really like this functionality.
Peter Hickman
Prole
Posts: 15
Joined: Fri Jan 30, 2009 8:19 am

### Re: CAMERA: scrolling and scaling

Nice code, I'm sure it will come in useful. Thanks

However the second demo boxlimber.love crashes under love 0.5.0 on os x
osuf oboys
Party member
Posts: 215
Joined: Sun Jan 18, 2009 8:03 pm

### Re: CAMERA: scrolling and scaling

I have moved CAMERA to the LÖVE wiki. I encourage everyone to add features and change the system as they see fit for the community's best. Changes to how things are drawn is a rather central concept, I believe, and would be a central concept even if all of the features currently provided by CAMERA were hardlöve provided. Better then, I imagine, to provide an integrated open source library instead of having a host of independent and possibly compatible projects.
If I haven't written anything else, you may assume that my work is released under the LPC License - the LÖVE Community. See http://love2d.org/wiki/index.php?title=LPC_License.
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

### Re: CAMERA: scrolling and scaling

Peter Hickman wrote:However the second demo boxlimber.love crashes under love 0.5.0 on os x
Confirmed on Linux too, error:
box2d/Source/Collision/Shapes/b2PolygonShape.cpp:225: b2PolygonShape::b2PolygonShape(const b2ShapeDef*): Assertion `d.y >= 0.0f' failed.
justindz
Prole
Posts: 1
Joined: Mon Feb 02, 2009 2:31 am

### Re: CAMERA: scrolling and scaling

Hi. I'm new to Lua and Love (but not to programming) and I'm interested in doing some smooth-scrolling console-ish RPGs. It seems like Camera would be useful in this endeavor, but I'm wondering if you have some insight as to whether Camera could actually be used to produce the smooth-scrolling tile-based engines itself. I am currently trying to adjust the Love tutorial tile engine, but am having issues in my graphic noobiness getting it from tile-at-a-time to pixels-as-you-go scrolling and could use some direction/advice/assistance.
osuf oboys
Party member
Posts: 215
Joined: Sun Jan 18, 2009 8:03 pm

### Re: CAMERA: scrolling and scaling

Peter Hickman wrote:Nice code, I'm sure it will come in useful. Thanks

However the second demo boxlimber.love crashes under love 0.5.0 on os x
On linux as well. It seems that Box2d shapes cannot be made smaller than 0.08. I have posted comments about this here: http://love2d.org/forum/viewtopic.php?f ... 4002#p4002 . In my examples, I have made the screen 12 by 9 metres. In order to use detail shapes, one may want to use 24 times 18 or 48 times 36 metres instead, e.g. by using feet instead of metres as the unit.

I have attached a new version with slightly larger claws and without the firs few thin boxes of the hill.
Attachments
boxclimber.love
Another version, now with no claws and minimum box size 0.10