Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
mouse joints after moving the stage/level(Sprite) with :setX() — Gideros Forum

mouse joints after moving the stage/level(Sprite) with :setX()

tpropertproper Member
edited January 2019 in General questions
I have a right scrolling game and my clicking and dragging using the sample code for the mouse joint works well, but as the player progresses to the right (stage/level:setX()), I get odd behavior.

the sample code:
-- create a mouse joint on mouse down
function launcher:grabber_display_on_down(event)
	if self.grabber_display:hitTestPoint(event.touch.x, event.touch.y) then
		local jointDef = b2.createMouseJointDef(self.ground, self.grabber_body, event.touch.x, event.touch.y, 5500, 10, 10)
		self.mouseJoint = world:createJoint(jointDef)
	end
end
 
-- update the target of mouse joint on mouse move
function launcher:grabber_display_on_move(event)
	if self.mouseJoint ~= nil then
		self.mouseJoint:setTarget(event.touch.x, event.touch.y)
	end
end
 
-- destroy the mouse joint on mouse update up
function launcher:grabber_display_on_up(event)
	if self.mouseJoint ~= nil then
		world:destroyJoint(self.mouseJoint)
		self.mouseJoint = nil
	end
end
--------------------

After I move the stage to the left, how can I get the mouse joint to work properly? I've tried keeping a variable of how far the stage has moved 'screen_current_x = 0' and when the stage moves I update that and changed the code above to:
function launcher:grabber_display_on_down(event)
	if self.grabber_display:hitTestPoint(event.touch.x, event.touch.y) then
                local x = event.touch.x + screen_current_x
		local jointDef = b2.createMouseJointDef(self.ground, self.grabber_body, x, event.touch.y, 5500, 10, 10)
		self.mouseJoint = world:createJoint(jointDef)
	end
end
-------------------

Not sure if that makes sense. Hopefully, I explained my problem well. Anyone know how to fix this?
Tagged:

Comments

  • keszeghkeszegh Member
    edited January 2019
    instead of
    self.grabber_display:hitTestPoint(event.touch.x, event.touch.y)
    try
    self.grabber_display:hitTestPoint(self.grabber_display:globalToLocal(event.touch.x, event.touch.y))
    EDIT: although checking the reference, hitTestPoint should already consider global coordinates, so this is not needed. sorry, i remembered wrong, then something else is the issue. (i never used box2d/liquidfun, so cannot help with that)
  • tpropertproper Member
    edited January 2019
    Odd observation: it seems that the mouse joint target is correct for the X while the Y is the problem. Pulling the grabber correctly moves left and right but not up and down.

    Here is the code for moving the level (if it's needed). The level is just a Sprite instance.
    -- Moving levels
    CURRENT_SCREEN_X = 0
    function move_level(level, move_by)
     
        local level_x, level_y, level_z = level:getPosition()
     
        -- Move stage
        level:setX(level_x - move_by)
        level:setY(level_y) -- Tried adding this, doesn't work with or without this
        CURRENT_SCREEN_X =  CURRENT_SCREEN_X + move_by
     
    end
    EDIT: I have also tried to move the ground body so it stays in the same screen place throughout the level but that changed nothing...
  • So...I was right with having to add the X amount of the screen to the touch.x. I just had to add it to the setTarget as well as in the jointDef. Both places are required. Here is the fix:
    -- create a mouse joint on mouse down
    function launcher:grabber_display_on_down(event)
    	if self.grabber_display:hitTestPoint(event.touch.x, event.touch.y) then
    		local jointDef = b2.createMouseJointDef(self.ground, self.grabber_body, event.touch.x + CURRENT_SCREEN_X, event.touch.y, 5500, 10, 10)
    		self.mouseJoint = world:createJoint(jointDef)
    	end
    end
     
    -- update the target of mouse joint on mouse move
    function launcher:grabber_display_on_move(event)
    	if self.mouseJoint ~= nil then
    		self.mouseJoint:setTarget(event.touch.x + CURRENT_SCREEN_X, event.touch.y)
    	end
    end
     
    -- destroy the mouse joint on mouse update up
    function launcher:grabber_display_on_up(event)
    	if self.mouseJoint ~= nil then
    		world:destroyJoint(self.mouseJoint)
    		self.mouseJoint = nil
    	end
    end

    Likes: Apollo14, antix

    +1 -1 (+2 / -0 )Share on Facebook
Sign In or Register to comment.