Share Your Code

Camera

Posted by GrahamRanson, Posted on September 14, 2011

GitHub URL: 
https://github.com/GrahamRanson/Camera

Just a simple Camera module that might be of use to some people. I wrote this pretty quickly so some more testing will still need to be done, please comment on here if you find anything.

Usage is very simple, just require the library, create a new Camera and then add some objects to it. You can then scale, rotate and move it as well as use a transitionTo function on it.

Parallax layers are also supported and are demonstrated in the main.lua file included in the repo.

Basic Usage

1
2
3
4
5
6
7
8
9
require( "camera" )
 
local camera = Camera:new()
 
camera:addObject( display.newImage( "player.png" ) )
 
camera:move( 50, 20 )
camera:rotate( 30 )
camera:transitionTo{ x = 100, alpha = 0.5, time = 1000 }


Replies

roottony
User offline. Last seen 26 weeks 5 days ago. Offline
Joined: 27 Jun 2011

Great module, but does not work properly with physics. The problem is described here:

http://developer.anscamobile.com/forum/2011/06/13/local-vs-content-coordinates

GrahamRanson
User offline. Last seen 9 weeks 3 days ago. Offline
Joined: 29 Mar 2010

Ah yea that is the issue about having physics objects in different groups. You should still be ok as long as you only add your physics objects to the same layer ( if using parallax ) or if you are not using parallax then just add them to the camera without specifying a layer.

diginutscorp
User offline. Last seen 1 year 51 weeks ago. Offline
Joined: 24 Feb 2011

how do I add physics object to the front layer or how I can make my character dynamic on a static platform without it falling off the platform

thank you

GrahamRanson
User offline. Last seen 9 weeks 3 days ago. Offline
Joined: 29 Mar 2010

You will need to add ALL physics objects to the same layer.

diginutscorp
User offline. Last seen 1 year 51 weeks ago. Offline
Joined: 24 Feb 2011

could you give me an example please.

if you don't mind I can send the zip file of what I have

GrahamRanson
User offline. Last seen 9 weeks 3 days ago. Offline
Joined: 29 Mar 2010

Feel free to send the zip to graham AT grahamranson DOT co DOT uk

I will look at it as soon as I can.

diginutscorp
User offline. Last seen 1 year 51 weeks ago. Offline
Joined: 24 Feb 2011

graham,
did you get my mail?

unboundminds
User offline. Last seen 2 years 4 weeks ago. Offline
Joined: 17 Sep 2011

Thanks, Graham. Works great!

dansuperxeo
User offline. Last seen 2 years 21 weeks ago. Offline
Joined: 26 Sep 2011

This is an awesome bit of code, thanks a lot for putting it out there for others to use. I found it at just the right time to save myself a bit of work on the prototype I'm working on.

GrahamRanson
User offline. Last seen 9 weeks 3 days ago. Offline
Joined: 29 Mar 2010

Awesome, glad to hear it!

__gamequad
User offline. Last seen 4 weeks 4 days ago. Offline
Joined: 24 Dec 2010

Bad work for negative values​​, eg:

setClampingBounds( -200, -200, width, height)

GrahamRanson
User offline. Last seen 9 weeks 3 days ago. Offline
Joined: 29 Mar 2010

True, it's by no means completely perfect but it is completely free and open source so you are welcome to contribute :-)

austinbuyer
User offline. Last seen 2 years 13 weeks ago. Offline
Joined: 27 Dec 2011

how do you code?

jkrassman
User offline. Last seen 1 year 11 weeks ago. Offline
Joined: 4 Aug 2011

Can this camera work like this:

I have a screenheight * 2. I want my camera to follow the player and center around him. But I don't want the camera to go off screen?

Joakim

GrahamRanson
User offline. Last seen 9 weeks 3 days ago. Offline
Joined: 29 Mar 2010

Yea it can do that.

jkrassman
User offline. Last seen 1 year 11 weeks ago. Offline
Joined: 4 Aug 2011

Thats great, I will try to implement it then!

Is there anything special I have to think of - or is it straight forward as in the sample code?

Joakim

GrahamRanson
User offline. Last seen 9 weeks 3 days ago. Offline
Joined: 29 Mar 2010

In theory it should all be pretty straight forward. Famous last words.

jkrassman
User offline. Last seen 1 year 11 weeks ago. Offline
Joined: 4 Aug 2011

Ok, I will give it a try :)

Joakim

jkrassman
User offline. Last seen 1 year 11 weeks ago. Offline
Joined: 4 Aug 2011

Hmmm, that was harder then I could imagine ;)

Problem is that I am using storyboard and the parallax class from levelhelper.org. I had big problems getting the basic camera to work so I got some help from the dude behind levelhelper to make it work. Somehow he is assigning the parallax to a new group inherited from the group storyboard uses - and I guess that this could be a problem?

Joakim

jkrassman
User offline. Last seen 1 year 11 weeks ago. Offline
Joined: 4 Aug 2011

Fixed, I tweaked my own camera function so now I am fine. Next project, I will try this from the beginning - it looks sweet.

Joakim

GrahamRanson
User offline. Last seen 9 weeks 3 days ago. Offline
Joined: 29 Mar 2010

I'm glad you got it working. I've never used LevelHelper or Storyboard so doubt I'd have been much use :-)

robmiracle
User offline. Last seen 8 weeks 1 day ago. Offline
Joined: 18 Jan 2011

I'm using this with a player that I touch to drag around the scene, but after a little bit of movement, the player graphic lags behind where the finger/mouse is in the scene.

Any suggestions for working with touch/drag?

GrahamRanson
User offline. Last seen 9 weeks 3 days ago. Offline
Joined: 29 Mar 2010

Is the player visual added to the camera view?

robmiracle
User offline. Last seen 8 weeks 1 day ago. Offline
Joined: 18 Jan 2011

Yes, the player is added to the camera. I'm using storyboard and here is the relevant add code:

1
2
3
4
camera:addObject(player)
camera:setFocus( player )
 
group:insert(camera:getView())

Then I have tried a camera:drag(event) inside my touch handler and without. I've tried the camera:drag(event) in my event handler as the only code too.

I do have a camera:update() in my enterFrame gameLoop function as well.

The camera works well except that I can't keep my player under my finger. I think I have a sample project that demonstrates it.

GrahamRanson
User offline. Last seen 9 weeks 3 days ago. Offline
Joined: 29 Mar 2010

A sample project would be great if you have it yea, please send it over to graham AT grahamranson DOT co DOT uk

thedavebaxter
User offline. Last seen 20 hours 37 min ago. Offline
Joined: 12 Jan 2012

Trying to use this in a game.

Basically I have a zoom button which changes the scale, so it starts at 1 and zooms to 6, then back to 1.

Added the camera and instead of changing all objects using xScale/yScale, am just using camera:setScale.

Everything is zooming but the camera doesn't work.

EDIT: Just to explain what I would like to do -

Race car on a track, top view.

Track image is same size as iPad 1 screen.
Config.lua is width = 768, height = 1024.

So when game starts you can see all of the track. Then if you zoom I want the track to scroll following the player (car).

Dave

GrahamRanson
User offline. Last seen 9 weeks 3 days ago. Offline
Joined: 29 Mar 2010

Are you able to make a simple demo to reproduce the issue and then send it to graham AT grahamranson DOT co DOT uk ?