Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
camera — Gideros Forum

camera

MoKaLuxMoKaLux Member
edited March 2020 in Game & application design
hope you are all doing fine.

How can I control my camera to follow the player but not every time?
function Level1:onEnterFrame(e)
	self.world:step(1/60, 1, 1)
	-- camera follow player
	local playerposx, playerposy = self.player.body:getPosition()
	local offsetX = -(playerposx - 128) * self.gamelayer:getScaleX()
	self.gamelayer:setX(offsetX)
--	local offsetY = -(playerposy - myappheight / 2 + 0) * self.gamelayer:getScaleY() -- too much camera movements
	local offsetY = -myappheight -- fixed camera ok but doesn't follow player when too low or too high
	self.gamelayer:setY(offsetY)
end
To be more precise, I want the camera to be fixed on the Y axis unless the player goes down or up above a certain limit.
Do you have any camera scripts, a website to recommend?
How did you do it? :)
my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
Tagged:

Comments

  • hgy29hgy29 Maintainer
    I usually do it the other way: camera keeps the player cenetered on screen unless he reaches the edges of the level. I am not sure what you want to achieve
  • MoKaLuxMoKaLux Member
    edited March 2020
    @hgy29 thank you for your answer.
    I want my camera to follow the player but not when he jumps only when he goes up/down a hill/platform... I have read that you should reposition the camera only when the player lands on a platform by putting triggers on those platforms.
    I don't really know how to handle this :/
    I have seen this page for camera in 2d games and it's just too much for me!
    https://www.gamasutra.com/blogs/ItayKeren/20150511/243083/Scroll_Back_The_Theory_and_P ractice_of_Cameras_in_SideScrollers.php

    A compilation of camera scripts would be awesome so we could choose the one appropriate to our game genre. They would also go straight to the wiki :)
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • @hgy29 like you did in Arthur & Aziliz at the beginning of the game, when the player jumps the camera doesn't move. The camera moves only when the player reaches a certain point. How can I do this please?
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • hgy29hgy29 Maintainer
    Just had a try, no the camera moves when you jump in my game, even at the beginning of the game. However the camera starts following the playing horizontally only after a certain point, when there is enough level graphics around.
  • rrraptorrrraptor Member
    edited March 2020 Accepted Answer
    A bit complicated xD

    local random = math.random
    local function clamp(v, min, max)
    	return (v <> min) >< max
    end
     
    local function lerp(a,b,t)
    	return a + (b-a) * t
    end
     
     
    Camera = Core.class(Sprite)
     
    function Camera:init(w,h)
    	self.w = w
    	self.h = h
    	self.minY = -1
    	self.maxY = -1
     
    	-- camera center
    	self.dot = Pixel.new(0,0.1,64,64)
    	self:addChild(self.dot)
    end
     
    function Camera:limitY(min,max)
    	self.minY = min
    	self.maxY = max
    end
     
    function Camera:follow(obj)
    	local sx,sy = self:getPosition()
     
    	local x,y = obj:getPosition()
     
    	local cx = -x + self.w / 2
    	local cy = -y + self.h / 2
    	if self.minY >= 0 and self.maxY >= 0 then 
    		cy = clamp(cy, self.minY, self.maxY)
    	end
    	self.dot:setPosition(-sx + self.w / 2 - 16,-sy + self.h / 2 - 16)
    	self:setPosition(
    		lerp(sx,cx,0.1), 
    		lerp(sy,cy,0.1)
    	)
    end 
     
    Background = Core.class(Sprite)
     
    function Background:init(cam)
    	self.timer = 0
    	self.camera = cam
    end
     
    function Background:update(dt)
    	self.timer += dt
    	if self.timer >= 0.08 then 
    		local sz = random(4, 8)
    		local star = Pixel.new(random(0xffffff), 1, sz, sz)
    		local cx, cy = self.camera:getPosition()
    		star:setPosition(
    			self.camera.w - cx,
    			random(self.camera.h) - cy
    		)
    		star.speed = random(100,200)
    		star.timer = 0
    		self:addChild(star)
    		self.timer = 0
    	end
     
    	local n = self:getNumChildren()
    	for i = n, 1, -1 do
    		local child = self:getChildAt(i)
    		child.timer += dt
    		local x,y = child:getPosition()
    		x -= child.speed * dt
    		child:setPosition(x, y)
    		if child.timer >= 2 then 
    			self:removeChild(child)
    		end
    	end
    end
     
    local W = application:getLogicalWidth()
    local H = application:getLogicalHeight()
     
    local camera = Camera.new(W,H)
    camera:limitY(350, 500)
    stage:addChild(camera)
     
    local BG = Background.new(camera)
    camera:addChild(BG)
     
    local px = Pixel.new(0xff0000, 1, 32, 32)
    camera:addChild(px)
     
     
    local timer = 0
    stage:addEventListener("enterFrame", function(e)
    	local dt = e.deltaTime
    	BG:update(dt)
     
    	timer += dt
     
    	local x,y = px:getPosition()
     
    	x += 100 * dt
    	y = math.sin(timer) * 150
     
    	px:setPosition(x,y)
    	camera:follow(px)
    end)

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • MoKaLuxMoKaLux Member
    edited March 2020
    @rrraptor thank you will give it a try :)
    corona cameras:
    https://github.com/Zetosoft/dynacam
    https://github.com/roaminggamer/RGCamera
    https://gist.github.com/GymbylCoding/8675733

    and one that also looks awesome but for love2d:
    https://github.com/adnzzzzZ/STALKER-X

    @hgy29 do you think you can translate one of corona camera code above with your tool?
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • rrraptor said:

    A bit complicated xD

    @rrraptor MAGNIFICO you are my hero of the day :)
    Thank you very much for your help ;)
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • @rrraptor I need some more help :)
    How do you want to call your camera class? that's for the wiki :)
    A few suggestions (camera is to vague):
    -rrraptorCamera ? o:)
    -cameraCL (for Clamp and Lerp)?
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • MoKaLuxMoKaLux Member
    edited March 2020
    here is rrraptor's camera class:
    --[[
    -- rrraptor
    	A camera which can follow any sprite using Clamp and Lerp functions.
    	Ideal for platformers, RPG, ...
    -- USAGE
    self.camera = CameraCL.new(myappwidth, myappheight)
    self.camera:addChild(ground)
    self.camera:addChild(self.player)
    function Level1:onEnterFrame(e)
    	-- camera follow player
    	self.camera:follow(self.player)
    end
    ]]
     
    -- functions
    local function clamp(v, min, max)
    	return (v <> min) >< max
    end
     
    local function lerp(a,b,t)
    	return a + (b-a) * t
    end
     
    -- the class
    CameraCL = Core.class(Sprite)
     
    function CameraCL:init(w,h)
    	self.w = w
    	self.h = h
    	self.minY = -1
    	self.maxY = -1
    	-- camera center
    	self.dot = Pixel.new(0xffffff,0.5,64*2,64*2)
    	self:addChild(self.dot)
    end
     
    function CameraCL:limitY(min,max)
    	self.minY = min
    	self.maxY = max
    end
     
    function CameraCL:follow(obj)
    	local sx,sy = self:getPosition()
    	local x,y = obj:getPosition()
    	local cx = -x + self.w / 2
    	local cy = -y + self.h / 2
    	if self.minY >= 0 and self.maxY >= 0 then
    		cy = clamp(cy, self.minY, self.maxY)
    	end
    	self.dot:setPosition(-sx + self.w / 2 - 16,-sy + self.h / 2 - 16)
    	self:setPosition(lerp(sx,cx,0.1), lerp(sy,cy,0.1))
    end
    Hopefully we can build other cameras based on this class for different game genres. <3

    Likes: keszegh

    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
    +1 -1 (+1 / -0 )Share on Facebook
  • MoKaLuxMoKaLux Member
    edited April 2020
    you can see the camera class in action :) Camera uses triggers put in the tiled map.
    https://mokalux.itch.io/gideros-platformer-lf
    Using liquidfun, tiled.
    PS: liquidfun doesn't work the same on html5 (some collisions bug). The same code works fine on pc and android :*
    I suspect this line of code:
    local myleftshape = b2.PolygonShape.new()
    myleftshape:setAsBox(0.1, h / 3, 0 + w / 4 - 1, h / 2, 0) -- half w, half h, centerx, centery, angle
    Html5 may convert it to either 0 or 1 instead of keeping it 0.1!?
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
Sign In or Register to comment.