• Stars
    star
    248
  • Rank 163,495 (Top 4 %)
  • Language
    Lua
  • License
    MIT License
  • Created almost 12 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A camera system for LÖVE

gamera.lua

A camera for LÖVE.

Initial setup

The first thing one needs to do to use gamera is to create it. You do so with gamera.new. This function requires 4 numbers (left, top, width and height) defining the "world boundaries" for the camera.

local cam = gamera.new(0,0,2000,2000)

The left and top parameters are usually 0,0, but they can be anything, even negative numbers (see below).

You can update the world definition later on with setWorld:

cam:setWorld(0,0,2000,2000)

By default gamera will use the whole screen to display graphics. You can restrict the amount of screen used with setWindow:

cam:setWindow(0,0,800,600)

Moving the camera around

You can move the camera around by using setPosition:

cam:setPosition(100, 200)

setPosition takes into account the current window boundaries and world boundaries, and will keep the view inside the world. This means that if you try to look at something very close to the left border of the world, for example, the camera will not "scroll" to show empty space.

You can also zoom in and zoom out. This is done using the setScale method. It's got a single parameter, which is used for increasing and decreasing the height and width. The default scale is 1.0.

cam:setScale(2.0) -- make everything twice as bigger. By default, scale is 1 (no change)

Take notice that gamera limits the amount of zoom out you can make; you can not "zoom out" to see the world edges. If you want to do this, make the world bigger first. For example, to give a 100-pixels border to a world defined as 0,0,2000,, you can define it like -100,100,2100,2100 instead.

Finally, you can modify the angle with setAngle:

cam:setAngle(newAngle) -- newAngle is in radians, by default the angle is 0

setAngle will change both the scale and position of the camera to force you not to see the world borders. If you don't want this to happen, expand the world borders as mentioned above.

Drawing

The camera has one method called "draw". It takes one function as a parameter. Here's one example:

local function drawCameraStuff(l,t,w,h)
  -- draw stuff that will be affected by the camera, for example:
  drawBackground(l,t,w,h)
  drawTiles(l,t,w,h)
  drawEntities(l,t,w,h)
end

...

-- pass your custom function to cam:draw when you want to draw stuff
cam:draw(drawCameraStuff)

Alternatively, you could create a custom anonymous function and pass it to cam:draw, but in some extreme cases this could have a non-negligible performance impact.

cam:draw(function(l,t,w,h)
  -- draw camera stuff here
end)

Anything drawn inside the custom function you pass to cam:draw be modified by the camera (scaled, rotated, translated and cut) so that it appears as it should in the screen window.

Notice that the function takes 4 optional parameters. These parameters represent the area that the camera "sees" (same as calling cam:getVisible()). They can be used to optimize the drawing, and not draw anything outside of those borders. Those borders are always axis-aligned. This means that when the camera is rotated, the area might include elements that are not strictly visible.

Querying the camera

  • cam:getWorld() returns the l,t,w,h of the world

  • cam:getWindow() returns the l,t,w,h of the screen window

  • cam:getVisible() returns the l,t,w,h of what is currently visible in the world, taking into account rotation, scale and translation. It coincides with the parameters of the callback function in gamera.draw. It can contain more than what is necessary due to rotation.

  • cam:getVisibleCorners() returns the corners of the rotated rectangle that represent the exact region being seen by the camera, in the form x1,y1,x2,y2,x3,y3,x4,y4

  • cam:getPosition() returns the coordinates the camera is currently "looking at", after it has been corrected so that the world boundaries are not visible, if possible.

  • cam:getScale() returns the current scaleX and scaleY parameters

  • cam:getAngle() returns the current rotation angle, in radians

Coordinate transformations

  • cam:toWorld(x,y) transforms screen coordinates into world coordinates, taking into account the window, scale, rotation and translation. Useful for mouse interaction, for example.
  • cam:toScreen(x,y) transforms given a coordinate in the world, return the real coords it has on the screen. Useul to represent icons in minimaps, for example.

FAQ

Everything looks kindof "blurry" when I do zooms/rotations with this library. How do I prevent it?

LÖVE uses a default filter mode which makes images "blurry" when you make almost any transformation to them. To deactivate this behavior globally, you can add this at the beginning of your game, before you load anything (for example at the beginning of love.load):

love.graphics.setDefaultFilter( 'nearest', 'nearest' )

You can also set the filter of each image you load individually:

local img = love.graphics.newImage('imgs/player.png')
img:setFilter('nearest', 'nearest')

I see "seams" around my tiles when I use this library. Why?

It is due to a combination of factors: how OpenGL textures work, how floating point numbers work and how LÖVE stores texture information in memory.

The end result is that sometimes, when drawing an image, "parts of the area around it" are also drawn. So if you draw a Quad of "earth", and immediately above it in your image you have a "bright lava" tile, when you draw the earth tile sometimes "a bit of the lava" is drawn near the top. If you are using images instead of quads, you can get seams too (either black or from other colors, depending on how the image is stored in memory).

To prevent this:

  • Always use quads for tiles, never images.
  • Add a 2-pixel border to each of your quads (So for a 32x32 quad, you use 36x36 space, with the 32x32 quad in the center and a 2-pixel border)
  • Fill up the borders with the colors of the 32x32 quad. For example, if the earth quad is all brown on its left side, color the left border brown. If on its upper side is gray in the center and brown on the sides, color the upper border gray in the center and brown on the sides.
  • The quads will still be 32x32 - they will just have some border in the image now.
  • The "seams" will still appear, but now they will show the "colored borders" of the quads, so they will not be noticeable.
  • Calculating the coordinates of the quads is a bit more complex than before. You can use anim8's "Grids" to simplify getting them:
local anim8 = require 'anim8'

...

local tiles = love.graphics.newImage('tiles.png')
local w,h = tiles:getDimensions()

local g = anim8.newGrid(32, 32, w, h, 0, 0, 2) -- tileWidth, tileHeight, imageW, imageH, left, top, border

local earth = g(1,1)[1] -- Get the first column, first row 32x32 quad, including border
local water = g(2,1)[1] -- Get the second column, first row quad

You can combine this with the previous FAQ and use a "nearest" filter instead of a linear one.

The camera is "clamping". I don't want it to clamp

Yes, by default gamera cameras make sure that they always "stay in the world". They never "show black borders" around the scene.

There is no way to deactivate this behaviour. However, you can "mimic" it very well. It's actually very simple: give the camera a bigger world.

For example, if instead of doing this:

local cam = gamera.new(0,0,2000,2000)

You do this:

local cam = gamera.new(-200,-200,2200,2200)

You will give the world a 200 pixel "black border" which will be visible when the camera zooms out or moves to the left or right.

If you want to display the borders only sometimes, you can use setWorld to activate/deactivate the zoom:

local cam = gamera.new(0,0,2000,2000)

... -- the camera "clamps" when drawing

cam:setWorld(-200,-200,2200,2200)

... -- Now the camera has a 200px border

cam:setWorld(0,0,2000,2000)

... -- now it clamps again

Installation

Just copy the gamera.lua file wherever you want it. Then require it where you need it:

local gamera = require 'gamera'

Please make sure that you read the license, too (for your convenience it's included at the beginning of the gamera.lua file).

More Repositories

1

middleclass

Object-orientation for Lua
Lua
1,629
star
2

inspect.lua

Human-readable representation of Lua tables
Lua
1,346
star
3

bump.lua

A collision detection library for Lua
Lua
914
star
4

anim8

An animation library for LÖVE
Lua
707
star
5

tween.lua

Tweening/Easing/Interpolating functions for lua. Inspired on jQuery's animate method.
Lua
568
star
6

lua_missions

Lua Koans, minus the Zen stuff
Lua
375
star
7

md5.lua

MD5 sum in pure Lua, with no C and no external dependencies
Lua
321
star
8

love-tile-tutorial

A tutorial for making tile-based games with LÖVE
Lua
272
star
9

i18n.lua

A very complete i18n lib for Lua
Lua
251
star
10

stateful.lua

Stateful classes for Lua
Lua
174
star
11

cron.lua

Time-related functions for Lua, inspired in javascript's setTimeout and setInterval
Lua
169
star
12

love-loader

Threaded resource loading for LÖVE
Lua
130
star
13

lua-sandbox

A lua sandbox for executing non-trusted code
Lua
128
star
14

beholder.lua

Minimal observer pattern for Lua, with a couple twists
Lua
105
star
15

semver.lua

Semantic versioning for Lua
Lua
103
star
16

memoize.lua

memoized functions in lua
Lua
88
star
17

sha1.lua

(Deprecated Repo) SHA-1 secure hash computation, and HMAC-SHA1 signature computation in Lua (5.1)
Lua
72
star
18

7-languages-in-7-weeks

My personal repo for 7LI7W exercises
Prolog
63
star
19

luv.js

Minimal HTML5 game development lib
JavaScript
43
star
20

passion

An object-oriented LÖVE game engine
Lua
36
star
21

middleclass-extras

A set of middleclass add-ons that make it easier to use in some cases
Lua
32
star
22

bresenham.lua

Lua
29
star
23

utf8_validator.lua

Easily validating UTF-8 strings in pure Lua
Lua
26
star
24

battle-cry

Lua
13
star
25

middleclass-commons

Interface between middleclass and Class-Commons
Lua
10
star
26

pulsar.lua

A-star algorithm implementation in Lua
Lua
10
star
27

ekrixion

simple game in LÖVE
Lua
9
star
28

fay

A small game for LÖVE, made for Ludum Dare #25
Lua
9
star
29

contact.php

Simple contact form. I really mean it. It's very simple.
PHP
9
star
30

lua-for-javascripters

A presentation about Lua, for people who are familiar with Javascript
CSS
7
star
31

pew-pew-boom

Explosions in 2D space. Ussing PÄSSION and LÖVE
Lua
6
star
32

middleclass-specs

Specs for testing middleclass
Lua
5
star
33

kongame

Lua
5
star
34

nvim

nvim custom config
Vim Script
5
star
35

missing_i18n

Rails mountable engine that finds missing i18n translations and displays them in a variety of formats.
Ruby
5
star
36

busted-stable

A simple rock to install a stable version of busted
4
star
37

middleclass-ai

Ai-related classes implemented in Lua
4
star
38

passion-demos

several demos of the PÄSSION game engine
Lua
4
star
39

rickshaw-vs-nvd3

Comparison of 2 popular js charting libs
CSS
3
star
40

adegan

my personal vim configuration
Vim Script
3
star
41

ci-with-lua

Presentation about continuous integration with Lua
CSS
3
star
42

love_open_chars

open_chars used on a love
Lua
3
star
43

us-4-es.keylayout

Mac keyboard layout for users of U.S. layout who need to write Spanish characters occasionally.
3
star
44

things-to-do-with-postgresql

A presentation about postgresql and rails
CSS
3
star
45

measuring-luas-performance

CSS
2
star
46

a-taste-of-lua

My talk about Lua in APIStrat Chicago 09-2014
CSS
2
star
47

stateful-demo

A simple demo of stateful.lua
Lua
2
star
48

rust-by-example

My activities & impressions while reading Rust by Example
Rust
2
star
49

modis.lua

Lua implementation of MongoDB query language over redis.
Lua
2
star
50

open_chars

My take on Silveira Neto's Open Charas project
2
star
51

jay

javascript object oriented game engine
JavaScript
2
star
52

ld-30

LD 30 - Earth, Hell & Space
Lua
2
star
53

kiki.to

CSS
1
star
54

S021

Interactive Fiction Collaborative Story
HTML
1
star
55

ood-with-ruby

A POODR-based presentation
CSS
1
star
56

stimulus-how-and-why

CSS
1
star
57

lua-for-rubyists

Slides for a talk about Lua for Ruby practicioners
CSS
1
star
58

hacking-madrid

Presentation for t3chfest 2016 about my work in decide.madrid.es
CSS
1
star
59

ci-with-ruby

Talk about continouous integration with ruby
CSS
1
star
60

popular-gemology

A talk about ruby gems: what to look for and to avoid
CSS
1
star
61

old-blog

Kikito's github page
JavaScript
1
star
62

api-addicts-2021-06-24

Companion repo for my API:Addicts talk of June 2021
1
star