Page 1 of 6

CAMERA: scrolling and scaling

Posted: Sat Jan 31, 2009 11:15 am
by osuf oboys
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.

License
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.

Re: CAMERA: scrolling and scaling

Posted: Sat Jan 31, 2009 11:16 am
by osuf oboys
Demos and applications
ballForAStroll.love
Demo for CAMERA
(33.79 KiB) Downloaded 901 times
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.
(2.77 KiB) Downloaded 501 times
Image

Re: CAMERA: scrolling and scaling

Posted: Sun Feb 01, 2009 10:13 am
by osuf oboys
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.

Re: CAMERA: scrolling and scaling

Posted: Sun Feb 01, 2009 4:40 pm
by osuf oboys
A simpler CAMERA demo - can you climb the boxes?

Image

Re: CAMERA: scrolling and scaling

Posted: Sun Feb 01, 2009 8:51 pm
by Evil Telephone
YOU WIN! :ultrahappy:

Very cool. I really like this functionality.

Re: CAMERA: scrolling and scaling

Posted: Mon Feb 02, 2009 11:17 am
by Peter Hickman
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

Re: CAMERA: scrolling and scaling

Posted: Mon Feb 02, 2009 1:21 pm
by osuf oboys
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.

Re: CAMERA: scrolling and scaling

Posted: Mon Feb 02, 2009 1:54 pm
by bartbes
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.

Re: CAMERA: scrolling and scaling

Posted: Mon Feb 02, 2009 2:04 pm
by justindz
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.

Re: CAMERA: scrolling and scaling

Posted: Mon Feb 02, 2009 2:05 pm
by osuf oboys
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.