Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Move camera example Help — Gideros Forum

Move camera example Help

zakkwyldezakkwylde Member
edited February 2014 in General questions
Hi,

I am trying to adapt the Box2d camera demo to make the ball follow the mouse when the button is down, but the ball movement is not very good (work ok when the screen is not moving, once the screen scrolls it awful). I am using a mousejoint.

Could an expert assist with a modified version that allows me to use the mouse\touch to drag the ball across the screen and remain smooth? Please?

Comments

  • @zakkwylde that seems interesting questions
    can not answer right now, will experiment more.
    But yes mouse movement will interfere with object centering, so you can't make object centered, but need some other approach to keep it on screen
  • @ar2rsawseen I'm trying also to achieve @zakkwylde asked.
    It's important for me to obtain the right movement.

    Would you give us a ray of light on the approach you are thinking or the solution itself?

    Thanks.
  • Hi @zakkwylde and @SimplesApps
    I don't know if I understood well your problem.
    I believe, you can try with @Niicodemus 's Camera (http://giderosmobile.com/forum/discussion/comment/30138#Comment_30138) and @ar2rsawseen 's Box2dEasy (again sawseen) as follows:
     
    ----------------
    -- in Box2dEasy
    ----------------
    function b2.World:makeDraggable(object)
    		--create empty box2d body for joint
    		--since mouse cursor is not a body
    		--we need dummy body to create joint
    		local ground = self:createBody({})
     
    		--joint with dummy body
    		local mouseJoint = nil
    		-- create a mouse joint on mouse down
    		object.onDragStart = function(self, event)
     
    			if object:hitTestPoint(event.touch.x, event.touch.y) then
    				-----------------------------------------------
    				object:dispatchEvent(Event.new("pauseCamera"))
    				-----------------------------------------------
    				object.x0 = event.touch.x
    				object.y0 = event.touch.y
    				local jointDef = b2.createMouseJointDef(ground, object.body, 
    				event.touch.x, event.touch.y, 100000000,1,0)
    				mouseJoint = self:createJoint(jointDef)
     
    			end
    		end
     
    		-- update the target of mouse joint on mouse move
    		object.onDragMove = function(self, event)
    			if mouseJoint ~= nil then
    				dx = event.touch.x - object.x0
    				dy = event.touch.y - object.y0
    				object.body:set("x",object.body:get("x")+dx)
    				object.body:set("y",object.body:get("y")+dy)
    				mouseJoint:setTarget(event.touch.x, event.touch.y)
    				object.x0 = event.touch.x
    				object.y0 = event.touch.y
    			end
    		end
     
     
    		-- destroy the mouse joint on mouse up
    		object.onDragEnd = function(self, event)
    			if mouseJoint ~= nil then
    				-----------------------------------------------
    				object:dispatchEvent(Event.new("startCamera"))
    				-----------------------------------------------
    				self:destroyJoint(mouseJoint)
    				mouseJoint = nil
    			end
    		end
     
    		-- register for mouse events
    		object:addEventListener(Event.TOUCHES_BEGIN, object.onDragStart, self)
    		object:addEventListener(Event.TOUCHES_MOVE, object.onDragMove, self)
    		object:addEventListener(Event.TOUCHES_END, object.onDragEnd, self)
    		return self
    	end
    and in your main.lua file
     
    ----------------
    -- main
    ----------------
    camera = Camera.new(...)
    local world = b2.World.new(...)
    local  ball = Bitmap.new(...)	
    world:createRectangle(ball, {type = ...})
     
    camera:addChild(world:getDebug()) -- if you need this
    camera:addEventListener(Event.ENTER_FRAME, function() world:update() end)
     
    ball:addEventListener("pauseCamera",
    	function()
    		print("pause Camera")
    		camera:pause()
    	end)
     
    ball:addEventListener("startCamera",
    	function()
    		print("start Camera")
    		camera:start()
    	end)
    camera:addChild(camera)
    stage:addChild(camera)
     
    --p.s. you'll need add background for camera works fine
    This was an idea but feel free to improve this code and showing your progress here. ;)

  • I've tried to adapt the code in order to achieve the correct ball movement with the camera but it's not working.

    Hey @ar2rsawseen I noticed you added a change to Box2dEasy (https://github.com/ar2rsawseen/GiderosCodingEasy) after @HubertRonald post his code here.

    Have you tried it?
  • ar2rsawseenar2rsawseen Maintainer
    edited March 2014
    @SimplesApps sorry, but no, it was other @HubertRonald code addition :)

    Well the main problem is, that when camera is moving, it moves so the object is centered.
    But then, in the same time, you want to take and drag that object.
    When dragging it you expect it to move relative to the current screen, but the screen is again, following the object, due to the camera move.

    For now I can see only one solution, start camera movement only when you reach the end of the screen while dragging an object.

    But if this solution will be helpful or not, I would like to understand more of a purpose of what you are trying to achieve (and I currently can't imagine efficient solution)

    Likes: Yan

    +1 -1 (+1 / -0 )Share on Facebook
  • Hi @SimplesApps
    Yesterday I can't upload sample because internet was failing :((

    P.S.
    1.- This file not include last version of Box2dEasy
    2.- How said @ar2rsawseen: I currently can't imagine efficient solution too
    3.- I don't know why I can't upload zip file :P
    ball.png
    80 x 80 - 10K
    lua
    lua
    Box2dEasy.lua
    17K
    lua
    lua
    Camera.lua
    14K
    crate.png
    90 x 90 - 15K
    lua
    lua
    main.lua
    2K
    sky_world_big.png
    640 x 960 - 480K
Sign In or Register to comment.