Moving Camera and gameUI.dragBody Issues?

Hi, I’m having a strange problem with my camera (localGroup) and a certain pesky object (puck, inside localGroup). I’m trying to have my camera follow an object (ball, inside localGroup) and have the ability to also drag other objects using the gameUI file (puck). I can drag the ‘puck’ object perfectly before the camera begins moving along with ‘ball’, but as soon as the camera begins to move, the ‘puck’ object no longer follows my mouse cursor; it seems to lose all correlation between itself and where I try to drag it. Although, the ‘puck’ object still collides correctly with all other physics objects.
Snippet from my code:

[code]local localGroup = display.newGroup()
application.LevelHelperSettings.directorGroup = localGroup

loader = LevelHelperLoader:initWithContentOfFile(“my_level.plhs”)
loader:instantiateObjects(physics)

–ball object
local ball = display.newImage(“moon.png”)
ball.x = 90
ball.xScale = 3
ball.yScale = 3
localGroup:insert(ball)
physics.addBody( ball, { density = 1.0, friction = 0.3, bounce = 0.2, radius = 100 } )

–camera movement
local function moveCamera()
if (ball.x > 580 and ball.x < 1100) then
localGroup.x = -ball.x + 580
localGroup.y = -ball.y + 580
end
end
Runtime:addEventListener( “enterFrame”, moveCamera )

–drag function
local function dragBody( event )
return gameUI.dragBody( event )
end

–puck object
local puck = display.newImage( “moon.png” )
puck.x = 300
puck.y = 100
localGroup:insert(puck)

physics.addBody( puck, { density=0.3, friction=0.6, radius=66.0 } )

puck.linearDamping = 0.4
puck.angularDamping = 0.6

puck:addEventListener( “touch”, dragBody ) – make object draggable
[import]uid: 66329 topic_id: 21892 reply_id: 321892[/import]

Hey,
did you ever find a solution?
i just ran into the same problem.

anyone?
[import]uid: 11133 topic_id: 21892 reply_id: 130836[/import]

ok, i just had a look into gameui.lua.
as you posted this a while ago i guess you’re not interested in a solution anymore. but just in case someone else finds this thread because of having the same issue, i’ll post my little tweak.

first of all you need to have the moving group, in your case “localGroup” global, so that the modified gameUI can access it’s x and y properties.

now open “gameUI.lua” and replace every “event.x” with “(event.x - localGroup.x)” and every “event.y” with “(event.x - localGroup.y)”

or just use my little modified
gameUI.lua :
[lua]-- gameUI library

– Version 2.0 (updated for new audio API)

– Copyright © 2010 ANSCA Inc. All Rights Reserved.

– Permission is hereby granted, free of charge, to any person obtaining a copy of
– this software and associated documentation files (the “Software”), to deal in the
– Software without restriction, including without limitation the rights to use, copy,
– modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
– and to permit persons to whom the Software is furnished to do so, subject to the
– following conditions:

– The above copyright notice and this permission notice shall be included in all copies
– or substantial portions of the Software.

– THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
– INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
– PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
– FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
– OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
– DEALINGS IN THE SOFTWARE.

– 2012-11-13 NEW: params includes now dispGroup for dragging objects inside a moved,
– or moving, displayGroup.
– how to use the addition:
– first, your moving displayGroup has to be Global, (sorry for that).
– next is adding the parameter to your return gameUI line:
– return gameUI.dragBody( event, {dispGroup=myMovingDisplaygroup} )
– thats all.

module(…, package.seeall)

– A general function for dragging physics bodies

– Simple example:
– local dragBody = gameUI.dragBody
– object:addEventListener( “touch”, dragBody )

function dragBody( event, params )
local body = event.target
local phase = event.phase
local stage = display.getCurrentStage()
–hero.controller.bodyType = “static”

if “began” == phase then
stage:setFocus( body, event.id )
body.isFocus = true

– Create a temporary touch joint and store it in the object for later reference
if params and params.center then
– drag the body from its center point
body.tempJoint = physics.newJoint( “touch”, body, body.x, body.y )
elseif params and params.dispGroup then
local gameworld = params.dispGroup
local x = (event.x - gameworld.x)
local y = (event.y - gameworld.y)
body.tempJoint = physics.newJoint( “touch”, body, x, y )

else
– drag the body from the point where it was touched
body.tempJoint = physics.newJoint( “touch”, body, event.x, event.y )
end

– Apply optional joint parameters
if params then
local maxForce, frequency, dampingRatio

if params.maxForce then
– Internal default is (1000 * mass), so set this fairly high if setting manually
body.tempJoint.maxForce = params.maxForce
end

if params.frequency then
– This is the response speed of the elastic joint: higher numbers = less lag/bounce
body.tempJoint.frequency = params.frequency
end

if params.dampingRatio then
– Possible values: 0 (no damping) to 1.0 (critical damping)
body.tempJoint.dampingRatio = params.dampingRatio
end
end

elseif body.isFocus then
if “moved” == phase then

if params and params.dispGroup then
local x = (event.x - gameworld.x)
local y = (event.y - gameworld.y)
– Update the joint to track the touch
body.tempJoint:setTarget( x, y )
else
body.tempJoint:setTarget( event.x, event.y )
end

elseif “ended” == phase or “cancelled” == phase then
stage:setFocus( body, nil )
body.isFocus = false

–hero.controller.bodyType = “dynamic”

– Remove the joint when the touch ends
body.tempJoint:removeSelf()

end
end

– Stop further propagation of touch event
return true
end
– A function for cross-platform event sounds

function loadSoundXP( params )
local isAndroid = “Android” == system.getInfo(“platformName”)

if isAndroid and params.android then
soundID = audio.loadSound( system.pathForFile( params.android ) ) – return sound file for Android
elseif params.ios then
soundID = audio.loadSound( params.ios ) – return sound file for iOS/MacOS
end

return soundID
end
– A function for cross-platform fonts

function newFontXP( params )
local isAndroid = “Android” == system.getInfo(“platformName”)

if isAndroid and params.android then
font = params.android – return font for Android
elseif params.ios then
font = params.ios – return font for iOS/MacOS
else
font = native.systemFont – default font (Helvetica on iOS, Android Sans on Android)
end

return font
end[/lua]

instructions:
[lua]-- 2012-11-13 NEW: params includes now dispGroup for dragging objects inside a moved,
– or moving, displayGroup.
– how to use the addition:
– first, your moving displayGroup has to be Global, (sorry for that).
– next is adding the parameter to your return gameUI line:
– return gameUI.dragBody( event, {dispGroup=myMovingDisplaygroup} )
– thats all.[/lua]

in your case that would be:
[lua]–actually not local anymore, but as you gave it that name, i kept it.
localGroup = display.newGroup()

–then all the lines in between (they keep unchanged)

–drag function
local function dragBody( event )
return gameUI.dragBody( event, {dispGroup=localGroup} )
end[/lua]

this is working, even tho i would really like to keep my group local. so if anyone got a better idea to fix this problem, please let me know.

cheers,
Michael [import]uid: 11133 topic_id: 21892 reply_id: 130841[/import]

oh, one thing, i just realised that you should not set center to true now (at the parameters of dragBody).
cause the function will never reach my elseif condition where it gets the coordinates of your moving diplayGroup then.
[import]uid: 11133 topic_id: 21892 reply_id: 130842[/import]

Hey,
did you ever find a solution?
i just ran into the same problem.

anyone?
[import]uid: 11133 topic_id: 21892 reply_id: 130836[/import]

ok, i just had a look into gameui.lua.
as you posted this a while ago i guess you’re not interested in a solution anymore. but just in case someone else finds this thread because of having the same issue, i’ll post my little tweak.

first of all you need to have the moving group, in your case “localGroup” global, so that the modified gameUI can access it’s x and y properties.

now open “gameUI.lua” and replace every “event.x” with “(event.x - localGroup.x)” and every “event.y” with “(event.x - localGroup.y)”

or just use my little modified
gameUI.lua :
[lua]-- gameUI library

– Version 2.0 (updated for new audio API)

– Copyright © 2010 ANSCA Inc. All Rights Reserved.

– Permission is hereby granted, free of charge, to any person obtaining a copy of
– this software and associated documentation files (the “Software”), to deal in the
– Software without restriction, including without limitation the rights to use, copy,
– modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
– and to permit persons to whom the Software is furnished to do so, subject to the
– following conditions:

– The above copyright notice and this permission notice shall be included in all copies
– or substantial portions of the Software.

– THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
– INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
– PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
– FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
– OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
– DEALINGS IN THE SOFTWARE.

– 2012-11-13 NEW: params includes now dispGroup for dragging objects inside a moved,
– or moving, displayGroup.
– how to use the addition:
– first, your moving displayGroup has to be Global, (sorry for that).
– next is adding the parameter to your return gameUI line:
– return gameUI.dragBody( event, {dispGroup=myMovingDisplaygroup} )
– thats all.

module(…, package.seeall)

– A general function for dragging physics bodies

– Simple example:
– local dragBody = gameUI.dragBody
– object:addEventListener( “touch”, dragBody )

function dragBody( event, params )
local body = event.target
local phase = event.phase
local stage = display.getCurrentStage()
–hero.controller.bodyType = “static”

if “began” == phase then
stage:setFocus( body, event.id )
body.isFocus = true

– Create a temporary touch joint and store it in the object for later reference
if params and params.center then
– drag the body from its center point
body.tempJoint = physics.newJoint( “touch”, body, body.x, body.y )
elseif params and params.dispGroup then
local gameworld = params.dispGroup
local x = (event.x - gameworld.x)
local y = (event.y - gameworld.y)
body.tempJoint = physics.newJoint( “touch”, body, x, y )

else
– drag the body from the point where it was touched
body.tempJoint = physics.newJoint( “touch”, body, event.x, event.y )
end

– Apply optional joint parameters
if params then
local maxForce, frequency, dampingRatio

if params.maxForce then
– Internal default is (1000 * mass), so set this fairly high if setting manually
body.tempJoint.maxForce = params.maxForce
end

if params.frequency then
– This is the response speed of the elastic joint: higher numbers = less lag/bounce
body.tempJoint.frequency = params.frequency
end

if params.dampingRatio then
– Possible values: 0 (no damping) to 1.0 (critical damping)
body.tempJoint.dampingRatio = params.dampingRatio
end
end

elseif body.isFocus then
if “moved” == phase then

if params and params.dispGroup then
local x = (event.x - gameworld.x)
local y = (event.y - gameworld.y)
– Update the joint to track the touch
body.tempJoint:setTarget( x, y )
else
body.tempJoint:setTarget( event.x, event.y )
end

elseif “ended” == phase or “cancelled” == phase then
stage:setFocus( body, nil )
body.isFocus = false

–hero.controller.bodyType = “dynamic”

– Remove the joint when the touch ends
body.tempJoint:removeSelf()

end
end

– Stop further propagation of touch event
return true
end
– A function for cross-platform event sounds

function loadSoundXP( params )
local isAndroid = “Android” == system.getInfo(“platformName”)

if isAndroid and params.android then
soundID = audio.loadSound( system.pathForFile( params.android ) ) – return sound file for Android
elseif params.ios then
soundID = audio.loadSound( params.ios ) – return sound file for iOS/MacOS
end

return soundID
end
– A function for cross-platform fonts

function newFontXP( params )
local isAndroid = “Android” == system.getInfo(“platformName”)

if isAndroid and params.android then
font = params.android – return font for Android
elseif params.ios then
font = params.ios – return font for iOS/MacOS
else
font = native.systemFont – default font (Helvetica on iOS, Android Sans on Android)
end

return font
end[/lua]

instructions:
[lua]-- 2012-11-13 NEW: params includes now dispGroup for dragging objects inside a moved,
– or moving, displayGroup.
– how to use the addition:
– first, your moving displayGroup has to be Global, (sorry for that).
– next is adding the parameter to your return gameUI line:
– return gameUI.dragBody( event, {dispGroup=myMovingDisplaygroup} )
– thats all.[/lua]

in your case that would be:
[lua]–actually not local anymore, but as you gave it that name, i kept it.
localGroup = display.newGroup()

–then all the lines in between (they keep unchanged)

–drag function
local function dragBody( event )
return gameUI.dragBody( event, {dispGroup=localGroup} )
end[/lua]

this is working, even tho i would really like to keep my group local. so if anyone got a better idea to fix this problem, please let me know.

cheers,
Michael [import]uid: 11133 topic_id: 21892 reply_id: 130841[/import]

oh, one thing, i just realised that you should not set center to true now (at the parameters of dragBody).
cause the function will never reach my elseif condition where it gets the coordinates of your moving diplayGroup then.
[import]uid: 11133 topic_id: 21892 reply_id: 130842[/import]