Dragging objects inside a scaled group.

Hi I was wondering if anyone has had any experience with dragging objects inside scaled groups? I have tried searching the forums and APIs and so on and have had no luck. What happens is that when you drag an object inside a scaled group it gradually moves away from the touch event. Here is some code, its a broken down version of the drag Platforms example.

[lua]–
– Abstract: DragPlatforms sample project
– Demonstrates one method for draggable physics objects

– Version: 1.1 (revised for Alpha 2)

– Sample code is MIT licensed, see http://developer.anscamobile.com/code/license
– Copyright © 2010 ANSCA Inc. All Rights Reserved.

local physics = require(“physics”)
physics.start()

display.setStatusBar( display.HiddenStatusBar )
local gameGroup = display.newGroup()

gameGroup.xScale = .65
gameGroup.yScale = .65

– A basic function for dragging physics objects
local function startDrag( event )
local t = event.target

local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true

– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y

– Make body type temporarily “kinematic” (to avoid gravitional forces)
event.target.bodyType = “kinematic”

– Stop current motion, if any
event.target:setLinearVelocity( 0, 0 )
event.target.angularVelocity = 0

elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false

– Switch body type back to “dynamic”, unless we’ve marked this sprite as a platform
if ( not event.target.isPlatform ) then
event.target.bodyType = “dynamic”
end

end
end

– Stop further propagation of touch event!
return true
end

local ground = display.newRect( 0,500,1000,10 )
ground:setFillColor(140, 140, 255)
physics.addBody( ground, “static”, { friction=0.6 } )
gameGroup:insert(ground)

local block = display.newRect( 50,50,50,50 )
block:setFillColor(140, 140, 255)
physics.addBody( block, { density=1.0, bounce=0.4 } )
gameGroup:insert(block)

– Add touch event listeners to objects

block:addEventListener( “touch”, startDrag )[/lua]

Thanks, JM [import]uid: 39088 topic_id: 10412 reply_id: 310412[/import]

Hey, I had a similar problem: https://developer.anscamobile.com/forum/2011/05/18/objectx-and-objecty-values-do-not-change-when-scaled. [import]uid: 54716 topic_id: 10412 reply_id: 37912[/import]

Sweet Thanks, I will check it out. [import]uid: 39088 topic_id: 10412 reply_id: 37918[/import]

I read the forum link and looked at the examples and I’m still lost. I cant figure out how to implement toLocalContent to the drag code. When ever I drag the object, it moves further and further away from my touch event. The farther I drag the farther it moves away. This is totally racking my noob brain.

Anyone any Ideas ?

@ir8Primates: what was your issue with scaling objects and relative touch events. Did you ever find a workaround? [import]uid: 39088 topic_id: 10412 reply_id: 38063[/import]

Check your target x and y formulas, they don’t look right. For my dragging functions, I am pretty sure I don’t use target.x0 unless there is some delay or physics involved.

Sorry, I am not with my code right now. Try logging all your variables in the terminal/console and work it out on paper first. That’s what I usually do.

My problem was that I have a scaling stage that has inner elements that needed to be rotated and dragged by touch. Actually, the same problem as you lol. I know for a fact that you have to use localToContent. It gives you the correct x and y value for the object you see and try to touch. When you scale the x and y values “remain” the same as if it wasn’t scaled. You have to check your equations.

I will try to post some code later. [import]uid: 54716 topic_id: 10412 reply_id: 38109[/import]

Will do, I will definately be checking my target coordinates. Well I guess they would be anscas, from the drag platforms sample. I think that might be part of my problem. I should have writtin my own drag code. Because I dont fully understand the code I am using. Really its the whole t.x0 and t.y0 thing. Whats up with the zeros? I understand that t.x, t.y is the targets x,y coordinates( the object catching the touch event) but the zeros have me baffeled. I did remove them and got some wierd results, definately not what I am trying to accomplish. Maybe because of the physics?

I understand that scaling is like a filter and everything stays the same. For say zooming out would in example be like looking through a magnifying backwards.

Today I am going to try and write my own drag code and hopefully figure out the math to bring my objects to the scaled position.

Thanks agian,
[import]uid: 39088 topic_id: 10412 reply_id: 38261[/import]