Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Camera class with kinetics and pinch-to-zoom — Gideros Forum

Camera class with kinetics and pinch-to-zoom

NiicodemusNiicodemus Member
edited February 2013 in Code snippets
Hello everyone. I'm new to Gideros and have been benefiting greatly from the posts and examples here on the forum. I was recently implementing some camera support in my game, and found information on the web to be a little lacking on how to implement a few details, such as kinetics and smooth scaling from something other than the origin point of a sprite. So I took what I learned and created an example project with my new Camera class, so that it might be helpful to someone else. The code is available at https://github.com/nshafer/KineticZoomCamera . Here is the rough documentation I included.

Camera class.

This implements a camera class that allows the user to drag and zoom a virtual camera. It works basically by having child elements that are bigger than the view size of the given device. There isn't really a camera that moves, rather it just moves itself and any child elements relative to the devices normal view. Further, when the user lifts their finger in the middle of a drag, the drag movement will continue with some kinetic energy and slow down based on simulated friction.

This has two distinct modes, DRAG and SCALE, based on how many touches are detected. It doesn't combine them, but could be modified to do so. It will change smoothly between the modes, however.

Usage:

local camera = Camera.new()
local camera = Camera.new({maxZoom=1.5,friction=.5})
stage:addChild(camera)

-- Add whatever you want as a child of the camera
local image = Bitmap.new(Texture.new("sky_world_big.png"))
camera:addChild(image)

-- If you want to center the camera on a child element, such as a player, you can do:
local player = Sprite.new() -- example player sprite
camera:centerPoint(player:getX(), player:getY())

-- If you want to process touch events relative to where the camera is, you can translate the event
function onTouchBegin(event)
local point = camera:translateEvent(event)
-- point.x = x position of the touch relative to the camera
-- point.y = y position of the touch relative to the camera
end

Hopefully it's helpful to someone.

Thanks,
Nathan Shafer
+1 -1 (+5 / -0 )Share on Facebook

Comments

Sign In or Register to comment.